CREATE TABLE IF NOT EXISTS member_chat_sync ( member_id BIGINT NOT NULL, profile_json LONGTEXT NOT NULL, member_state TINYINT NOT NULL DEFAULT 0, version BIGINT NOT NULL DEFAULT 1, synced_version BIGINT NOT NULL DEFAULT 0, initialized BIT(1) NOT NULL DEFAULT b'0', retry_count INT NOT NULL DEFAULT 0, next_retry_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, last_error VARCHAR(1000) NOT NULL DEFAULT '', creator VARCHAR(64) DEFAULT '', create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updater VARCHAR(64) DEFAULT '', update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, deleted BIT(1) NOT NULL DEFAULT b'0', tenant_id BIGINT NOT NULL, PRIMARY KEY (member_id), KEY idx_chat_sync_retry (tenant_id, next_retry_time) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- MySQL 5.7 can reconstruct AUTO_INCREMENT after restart. Persist the reserved IM ID range. CREATE TABLE IF NOT EXISTS member_chat_sync_sequence ( id TINYINT NOT NULL PRIMARY KEY, last_id BIGINT NOT NULL ) ENGINE=InnoDB; INSERT INTO member_chat_sync_sequence (id, last_id) SELECT 1, COALESCE(MAX(id), 0) FROM member_user ON DUPLICATE KEY UPDATE last_id = GREATEST(last_id, VALUES(last_id)); -- Create the handler in a stopped state. Start it after data repair and service verification. INSERT INTO infra_job (name, status, handler_name, handler_param, cron_expression, retry_count, retry_interval, monitor_timeout) SELECT '会员聊天同步与对账', 2, 'memberChatSyncJob', '', '0 * * * * ?', 0, 0, 120000 WHERE NOT EXISTS (SELECT 1 FROM infra_job WHERE handler_name = 'memberChatSyncJob' AND deleted = b'0');