feat(users): 拆分 C 端身份并迁移历史聊天

- digital.users: 独立开户登录与凭据版本校验,后台查看用户及原始会话
- 017迁移: 保留聊天消息、实验分组和人工指定期限,迁移历史账号归属
- tests: 覆盖身份隔离、凭据失效、会话归属和长账号登录,同步 README
This commit is contained in:
湛兮
2026-10-27 18:56:19 +08:00
parent 57c89f4f32
commit b9d1a7f6bd
14 changed files with 747 additions and 32 deletions
@@ -0,0 +1,46 @@
-- C 端身份独立于后台管理员/员工。只迁移实际参与数字人业务的历史账号,不创建演示用户。
CREATE TABLE IF NOT EXISTS dh_users (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(120) NOT NULL,
display_name VARCHAR(120) NOT NULL,
password_hash VARCHAR(255) NOT NULL,
status ENUM('ACTIVE','INACTIVE') NOT NULL DEFAULT 'ACTIVE',
token_version INT NOT NULL DEFAULT 1,
legacy_account VARCHAR(80) NULL,
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),
last_login_at DATETIME(3) NULL,
UNIQUE KEY uk_dh_user_username(username), UNIQUE KEY uk_dh_user_legacy(legacy_account),
KEY idx_dh_users_status(status,id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TEMPORARY TABLE dh_migration_owners (account VARCHAR(80) PRIMARY KEY);
INSERT IGNORE INTO dh_migration_owners
SELECT account FROM dh_conversations UNION SELECT account FROM dh_profiles
UNION SELECT account FROM dh_assignments UNION SELECT account FROM dh_overrides
UNION SELECT account FROM dh_decisions UNION SELECT account FROM dh_events
UNION SELECT j.account FROM dh_experiments e JOIN JSON_TABLE(e.config,'$.accounts[*]' COLUMNS(account VARCHAR(80) PATH '$')) j ON TRUE;
START TRANSACTION;
INSERT IGNORE INTO dh_users(username,display_name,password_hash,status,legacy_account,created_at)
SELECT e.phone,e.name,e.password_hash,IF(e.deleted_at IS NULL,e.status,'INACTIVE'),o.account,e.created_at
FROM employees e JOIN dh_migration_owners o ON o.account=CONCAT('employee:',e.id);
INSERT IGNORE INTO dh_users(username,display_name,password_hash,status,legacy_account,created_at)
SELECT IF(EXISTS(SELECT 1 FROM dh_users u WHERE u.username=a.username),CONCAT('admin-',a.id,'-',LEFT(a.username,90)),a.username),a.display_name,a.password_hash,a.status,o.account,a.created_at
FROM super_admins a JOIN dh_migration_owners o ON o.account=CONCAT('super_admin:',a.id);
INSERT IGNORE INTO dh_users(username,display_name,password_hash,status,legacy_account)
SELECT CONCAT('legacy-',REPLACE(o.account,':','-')),CONCAT('历史用户 ',o.account),'','INACTIVE',o.account
FROM dh_migration_owners o LEFT JOIN dh_users u ON u.legacy_account=o.account
WHERE u.id IS NULL AND o.account REGEXP '^(employee|super_admin):[0-9]+$';
-- 会话/消息主键不变,只迁移归属;已有消息、分组和人工指定有效期全部保留。
UPDATE dh_profiles d JOIN dh_users u ON u.legacy_account=d.account SET d.account=CONCAT('user:',u.id);
UPDATE dh_conversations d JOIN dh_users u ON u.legacy_account=d.account SET d.account=CONCAT('user:',u.id);
UPDATE dh_assignments d JOIN dh_users u ON u.legacy_account=d.account SET d.account=CONCAT('user:',u.id);
UPDATE dh_overrides d JOIN dh_users u ON u.legacy_account=d.account SET d.account=CONCAT('user:',u.id);
UPDATE dh_decisions d JOIN dh_users u ON u.legacy_account=d.account SET d.account=CONCAT('user:',u.id);
UPDATE dh_events d JOIN dh_users u ON u.legacy_account=d.account SET d.account=CONCAT('user:',u.id);
UPDATE dh_experiments e JOIN (
SELECT p.id,JSON_ARRAYAGG(COALESCE(CONCAT('user:',u.id),j.account)) accounts
FROM dh_experiments p JOIN JSON_TABLE(p.config,'$.accounts[*]' COLUMNS(account VARCHAR(80) PATH '$')) j ON TRUE
LEFT JOIN dh_users u ON u.legacy_account=j.account GROUP BY p.id
) mapped ON mapped.id=e.id SET e.config=JSON_SET(e.config,'$.accounts',mapped.accounts);
COMMIT;
DROP TEMPORARY TABLE dh_migration_owners;