diff --git a/mini-program/database/sql/001_init_chat.sql b/mini-program/database/sql/001_init_chat.sql index 6ebf6a7..a8c6a02 100644 --- a/mini-program/database/sql/001_init_chat.sql +++ b/mini-program/database/sql/001_init_chat.sql @@ -51,9 +51,8 @@ CREATE TABLE IF NOT EXISTS chat_user_auth ( CREATE TABLE IF NOT EXISTS chat_conversation ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, - title VARCHAR(128) NULL, - conversation_type TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '1-direct 2-group', - member_signature CHAR(64) NOT NULL COMMENT 'sha256 of sorted member user_ids, e.g. sha256("1,2")', + user_low_id BIGINT UNSIGNED NOT NULL COMMENT 'Smaller user_id in direct chat pair', + user_high_id BIGINT UNSIGNED NOT NULL COMMENT 'Larger user_id in direct chat pair', status TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '1-active 2-archived', last_seq BIGINT UNSIGNED NOT NULL DEFAULT 0, message_count BIGINT UNSIGNED NOT NULL DEFAULT 0, @@ -62,23 +61,15 @@ CREATE TABLE IF NOT EXISTS chat_conversation ( created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), PRIMARY KEY (id), - UNIQUE KEY uk_type_member_signature (conversation_type, member_signature), - KEY idx_last_message_at (last_message_at) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; - -CREATE TABLE IF NOT EXISTS chat_conversation_member ( - conversation_id BIGINT UNSIGNED NOT NULL, - user_id BIGINT UNSIGNED NOT NULL, - member_role TINYINT UNSIGNED NOT NULL DEFAULT 2 COMMENT '1-owner 2-member', - joined_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), - left_at DATETIME(3) NULL, - PRIMARY KEY (conversation_id, user_id), - KEY idx_user_conversation (user_id, conversation_id), - CONSTRAINT fk_member_conv - FOREIGN KEY (conversation_id) REFERENCES chat_conversation(id) + UNIQUE KEY uk_direct_pair (user_low_id, user_high_id), + KEY idx_last_message_at (last_message_at), + KEY idx_user_low_id (user_low_id), + KEY idx_user_high_id (user_high_id), + CONSTRAINT fk_conv_user_low + FOREIGN KEY (user_low_id) REFERENCES chat_user(id) ON DELETE CASCADE, - CONSTRAINT fk_member_user - FOREIGN KEY (user_id) REFERENCES chat_user(id) + CONSTRAINT fk_conv_user_high + FOREIGN KEY (user_high_id) REFERENCES chat_user(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; @@ -203,9 +194,8 @@ VALUES INSERT INTO chat_conversation ( id, - title, - conversation_type, - member_signature, + user_low_id, + user_high_id, status, last_seq, message_count, @@ -217,45 +207,17 @@ INSERT INTO chat_conversation ( VALUES ( 1, - 'Direct Chat', 1, - '17f8af97ad4a7f7639a4c9171d5185cbafb85462877a4746c21bdb0a4f940ca0', + 2, 1, - 4, - 4, - 'user: Great, explain how to solve it.', - '2026-03-25 10:00:30.000', + 5, + 5, + 'user: Thanks, I understand now.', + '2026-03-25 10:00:40.000', '2026-03-25 10:00:00.000', - '2026-03-25 10:00:30.000' - ), - ( - 2, - 'Family Group', - 2, - '8a6ae15122001229edb8866f56e342af12ae8187203c3e3b33931743e7c0c48d', - 1, - 3, - 3, - 'user: Okay, we will wait in the living room.', - '2026-03-25 21:16:10.000', - '2026-03-25 21:15:00.000', - '2026-03-25 21:16:10.000' + '2026-03-25 10:00:40.000' ); -INSERT INTO chat_conversation_member ( - conversation_id, - user_id, - member_role, - joined_at, - left_at -) -VALUES - (1, 1, 1, '2026-03-25 10:00:00.000', NULL), - (1, 2, 2, '2026-03-25 10:00:00.000', NULL), - (2, 1, 1, '2026-03-25 21:15:00.000', NULL), - (2, 2, 2, '2026-03-25 21:15:00.000', NULL), - (2, 3, 2, '2026-03-25 21:15:00.000', NULL); - INSERT INTO chat_message ( id, conversation_id, @@ -328,44 +290,16 @@ VALUES '2026-03-25 10:00:30.000' ), ( - 2001, + 1005, + 1, + 5, 2, 1, 1, - 1, - 1, - 'Dinner at 7?', + 'Thanks, I understand now.', JSON_OBJECT('lang', 'en'), NULL, NULL, - 'c2-m1', - '2026-03-25 21:15:08.000' - ), - ( - 2002, - 2, - 2, - 3, - 1, - 2, - NULL, - JSON_OBJECT('note', 'voice message'), - 'audios/family-group-001.mp3', - 95000, - 'c2-m2', - '2026-03-25 21:15:30.000' - ), - ( - 2003, - 2, - 3, - 2, - 1, - 1, - 'Okay, we will wait in the living room.', - JSON_OBJECT('lang', 'en'), - NULL, - NULL, - 'c2-m3', - '2026-03-25 21:16:10.000' + 'c1-m5', + '2026-03-25 10:00:40.000' );