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;