spark-server/migrations/0001_init.sql
2026-09-16 19:08:06 +08:00

63 lines
2.2 KiB
SQL

-- Spark IM V3 initial schema. Message body lives in MySQL; dialog list lives in Redis.
CREATE TABLE IF NOT EXISTS conversations (
id BIGINT PRIMARY KEY,
type TINYINT NOT NULL,
last_msg_id BIGINT NOT NULL DEFAULT 0,
last_msg_at DATETIME(3) NOT NULL,
created_at DATETIME(3) NOT NULL
);
CREATE TABLE IF NOT EXISTS p2p_conversations (
user_small BIGINT NOT NULL,
user_big BIGINT NOT NULL,
conversation_id BIGINT NOT NULL,
PRIMARY KEY (user_small, user_big),
UNIQUE KEY uk_conversation (conversation_id)
);
CREATE TABLE IF NOT EXISTS conversation_members (
conversation_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
last_read_msg_id BIGINT NOT NULL DEFAULT 0,
joined_at DATETIME(3) NOT NULL,
muted TINYINT NOT NULL DEFAULT 0,
PRIMARY KEY (conversation_id, user_id),
KEY idx_user_id (user_id)
);
CREATE TABLE IF NOT EXISTS messages (
id BIGINT PRIMARY KEY,
conversation_id BIGINT NOT NULL,
sender_id BIGINT NOT NULL,
client_msg_id VARCHAR(64) NOT NULL,
message_type SMALLINT NOT NULL,
content VARBINARY(65535) NOT NULL,
status TINYINT NOT NULL DEFAULT 1,
created_at DATETIME(3) NOT NULL,
UNIQUE KEY uk_sender_conv_client_msg (sender_id, conversation_id, client_msg_id),
KEY idx_conversation_id (conversation_id, id)
);
CREATE TABLE IF NOT EXISTS mailbox_events (
id BIGINT PRIMARY KEY,
user_id BIGINT NOT NULL,
device_id BIGINT NOT NULL,
seq BIGINT NOT NULL,
event_type SMALLINT NOT NULL,
conversation_id BIGINT NULL,
payload VARBINARY(4096),
created_at DATETIME(3) NOT NULL,
UNIQUE KEY uk_user_device_seq (user_id, device_id, seq),
KEY idx_user_device (user_id, device_id, seq)
);
CREATE TABLE IF NOT EXISTS device_sync_cursors (
user_id BIGINT NOT NULL,
device_id BIGINT NOT NULL,
mailbox_seq BIGINT NOT NULL DEFAULT 0,
dialog_score BIGINT NOT NULL DEFAULT 0,
updated_at DATETIME(3) NOT NULL,
PRIMARY KEY (user_id, device_id)
);