From 38a86555bbbb8e178fa30a4184464339d936e885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=9B=E5=85=AE?= Date: Tue, 27 Oct 2026 18:58:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(users):=20=E7=BB=9F=E4=B8=80=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E4=B8=AD=E7=9A=84=E8=B4=A6=E5=8F=B7=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 017迁移: 显式统一临时表和 JSON_TABLE 的 utf8mb4 排序规则 - 验证: 在服务器以部署相同的 mysql2 连接执行副本迁移,保留2个用户、3个会话和57条消息 --- migrations/017_independent_digital_users.sql | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/migrations/017_independent_digital_users.sql b/migrations/017_independent_digital_users.sql index b5a78af..0a202e1 100644 --- a/migrations/017_independent_digital_users.sql +++ b/migrations/017_independent_digital_users.sql @@ -12,13 +12,14 @@ CREATE TABLE IF NOT EXISTS dh_users ( 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); +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; +-- mysql2 与命令行连接的默认排序规则可能不同,临时列与 JSON_TABLE 显式统一。 +CREATE TEMPORARY TABLE dh_migration_owners (account VARCHAR(80) COLLATE utf8mb4_0900_ai_ci 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; + SELECT account COLLATE utf8mb4_0900_ai_ci FROM dh_conversations UNION SELECT account COLLATE utf8mb4_0900_ai_ci FROM dh_profiles + UNION SELECT account COLLATE utf8mb4_0900_ai_ci FROM dh_assignments UNION SELECT account COLLATE utf8mb4_0900_ai_ci FROM dh_overrides + UNION SELECT account COLLATE utf8mb4_0900_ai_ci FROM dh_decisions UNION SELECT account COLLATE utf8mb4_0900_ai_ci FROM dh_events + UNION SELECT j.account COLLATE utf8mb4_0900_ai_ci 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 @@ -40,7 +41,7 @@ UPDATE dh_events d JOIN dh_users u ON u.legacy_account=d.account SET d.account=C 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 + LEFT JOIN dh_users u ON u.legacy_account=j.account COLLATE utf8mb4_0900_ai_ci 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;