spark-server/migrations/0003_table_comments.sql
2026-09-23 17:11:56 +08:00

79 lines
6.1 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Add table/column comments for databases created before 0001/0002 carried COMMENT clauses.
ALTER TABLE t_conversations COMMENT = '会话。单聊/群聊共用一张表;列表与未读在 Redis,不在此表热更新';
ALTER TABLE t_conversations
MODIFY COLUMN id BIGINT NOT NULL COMMENT 'Snowflake 会话 id,时间有序主键',
MODIFY COLUMN type TINYINT NOT NULL COMMENT '会话类型:1=单聊 2=群聊',
MODIFY COLUMN last_msg_id BIGINT NOT NULL DEFAULT 0 COMMENT '最新消息 Snowflake id,0 表示尚无消息',
MODIFY COLUMN last_msg_at DATETIME(3) NOT NULL COMMENT '最新消息时间(毫秒精度)',
MODIFY COLUMN created_at DATETIME(3) NOT NULL COMMENT '会话创建时间';
ALTER TABLE t_p2p_conversations COMMENT = '单聊唯一索引。按 (较小 uid, 较大 uid) 定位会话,避免 A-B / B-A 两条记录';
ALTER TABLE t_p2p_conversations
MODIFY COLUMN user_small BIGINT NOT NULL COMMENT '两人中较小的 user_id,保证同一对用户只有一行',
MODIFY COLUMN user_big BIGINT NOT NULL COMMENT '两人中较大的 user_id',
MODIFY COLUMN conversation_id BIGINT NOT NULL COMMENT '对应 t_conversations.id';
ALTER TABLE t_conversation_members COMMENT = '会话成员。热路径会话列表不扫此表;last_read_msg_id 是已读持久化';
ALTER TABLE t_conversation_members
MODIFY COLUMN conversation_id BIGINT NOT NULL COMMENT '会话 id',
MODIFY COLUMN user_id BIGINT NOT NULL COMMENT '成员 user_id',
MODIFY COLUMN last_read_msg_id BIGINT NOT NULL DEFAULT 0 COMMENT '该成员已读到的最大消息 id,未读真相;Redis 丢了可从此重建',
MODIFY COLUMN joined_at DATETIME(3) NOT NULL COMMENT '加入会话时间',
MODIFY COLUMN muted TINYINT NOT NULL DEFAULT 0 COMMENT '0=未静音 1=免打扰';
ALTER TABLE t_messages COMMENT = '消息正文。PK=MsgId;content 为序列化 BLOB,不存媒体二进制';
ALTER TABLE t_messages
MODIFY COLUMN id BIGINT NOT NULL COMMENT 'Snowflake MsgId,主键、排序键、同步游标',
MODIFY COLUMN conversation_id BIGINT NOT NULL COMMENT '所属会话 id',
MODIFY COLUMN sender_id BIGINT NOT NULL COMMENT '发送者 user_id',
MODIFY COLUMN client_msg_id VARCHAR(64) NOT NULL COMMENT '客户端消息 id,与 sender+会话 组成幂等键',
MODIFY COLUMN message_type SMALLINT NOT NULL COMMENT '消息类型:1文本 2图片 3语音 4视频 5表情 6GIF 7文件 8位置 9系统',
MODIFY COLUMN content BLOB NOT NULL COMMENT '序列化后的消息体(JSON 字节),禁止存原始 string;媒体只存对象存储 key',
MODIFY COLUMN status TINYINT NOT NULL DEFAULT 1 COMMENT '1=正常 2=已撤回 3=审核隐藏',
MODIFY COLUMN created_at DATETIME(3) NOT NULL COMMENT '服务端入库时间';
ALTER TABLE t_mailbox_events COMMENT = '失败邮箱与控制事件。成功在线推送的新消息不写此表';
ALTER TABLE t_mailbox_events
MODIFY COLUMN id BIGINT NOT NULL COMMENT 'Snowflake 事件 id',
MODIFY COLUMN user_id BIGINT NOT NULL COMMENT '接收用户',
MODIFY COLUMN device_id BIGINT NOT NULL COMMENT '目标设备;0 表示该用户全部设备',
MODIFY COLUMN seq BIGINT NOT NULL COMMENT '该 user+device 上单调递增的邮箱序号',
MODIFY COLUMN event_type SMALLINT NOT NULL COMMENT '事件类型:撤回/踢人/成员变更/好友/漏推消息等,不含成功在线推送的新消息',
MODIFY COLUMN conversation_id BIGINT NULL COMMENT '相关会话,可空',
MODIFY COLUMN payload VARBINARY(4096) COMMENT '控制事件载荷(会话 id+消息 id 等),不含消息正文',
MODIFY COLUMN created_at DATETIME(3) NOT NULL COMMENT '写入时间';
ALTER TABLE t_device_sync_cursors COMMENT = '每设备同步游标。mailbox_seq 对邮箱,dialog_score 对会话列表';
ALTER TABLE t_device_sync_cursors
MODIFY COLUMN user_id BIGINT NOT NULL COMMENT '用户 id',
MODIFY COLUMN device_id BIGINT NOT NULL COMMENT '设备 id',
MODIFY COLUMN mailbox_seq BIGINT NOT NULL DEFAULT 0 COMMENT '该设备已同步到的 t_mailbox_events.seq',
MODIFY COLUMN dialog_score BIGINT NOT NULL DEFAULT 0 COMMENT '会话列表 Redis ZSET 游标',
MODIFY COLUMN updated_at DATETIME(3) NOT NULL COMMENT '游标更新时间';
ALTER TABLE t_users COMMENT = '用户账号。主键 Snowflake;登录身份在 t_user_emails,设备在 t_user_devices';
ALTER TABLE t_users
MODIFY COLUMN id BIGINT NOT NULL COMMENT 'Snowflake 用户 id(uid),时间有序,非自增',
MODIFY COLUMN spark_id VARCHAR(32) NOT NULL COMMENT '对外展示的账号,用户可改,全局唯一',
MODIFY COLUMN nickname VARCHAR(64) NOT NULL DEFAULT '' COMMENT '昵称',
MODIFY COLUMN avatar VARCHAR(512) NOT NULL DEFAULT '' COMMENT '头像对象存储 key 或 URL,不存图片二进制',
MODIFY COLUMN status TINYINT NOT NULL DEFAULT 1 COMMENT '1=正常 0=禁用',
MODIFY COLUMN created_at DATETIME(3) NOT NULL COMMENT '注册时间',
MODIFY COLUMN updated_at DATETIME(3) NOT NULL COMMENT '资料最近更新时间';
ALTER TABLE t_user_emails COMMENT = '邮箱与用户的一对一映射。登录用邮箱查此表再读 t_users';
ALTER TABLE t_user_emails
MODIFY COLUMN email VARCHAR(255) NOT NULL COMMENT '规范化后的小写邮箱,登录账号',
MODIFY COLUMN user_id BIGINT NOT NULL COMMENT '绑定的 t_users.id,一对一',
MODIFY COLUMN created_at DATETIME(3) NOT NULL COMMENT '绑定时间';
ALTER TABLE t_user_devices COMMENT = '用户与设备关系;设备属性见 t_devices,当前 App 见 JWT aid';
ALTER TABLE t_user_devices
MODIFY COLUMN id BIGINT NOT NULL AUTO_INCREMENT COMMENT '行 id,表内自增,仅内部用;业务唯一键是 (user_id, device_id)',
MODIFY COLUMN user_id BIGINT NOT NULL COMMENT '所属用户',
MODIFY COLUMN device_id BIGINT UNSIGNED NOT NULL COMMENT 't_devices.device_id,JWT did',
MODIFY COLUMN last_login_at DATETIME(3) NOT NULL COMMENT '该用户在此设备最近登录成功',
MODIFY COLUMN last_active_at DATETIME(3) NULL COMMENT '该用户在此设备最近活跃',
MODIFY COLUMN created_at DATETIME(3) NOT NULL COMMENT '该用户首次在此设备绑定';