From d178619fc753793bc4b59ccafcf37af5871d9ec6 Mon Sep 17 00:00:00 2001 From: yml2213 Date: Tue, 8 Sep 2026 13:20:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(migration):=20=E6=8F=90=E5=8D=87=20000070?= =?UTF-8?q?=20=E4=B8=8E=20000071=20=E8=BF=81=E7=A7=BB=E5=B9=82=E7=AD=89?= =?UTF-8?q?=E6=80=A7=EF=BC=8C=E5=85=BC=E5=AE=B9=E5=B7=B2=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E7=9A=84=E7=B4=A2=E5=BC=95=E4=B8=8E=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...070_distribution_chat_wallet_isolation.sql | 182 +++++++++++++++--- ...71_distribution_notification_isolation.sql | 40 +++- 2 files changed, 189 insertions(+), 33 deletions(-) diff --git a/backend/migrations/000070_distribution_chat_wallet_isolation.sql b/backend/migrations/000070_distribution_chat_wallet_isolation.sql index 80921e0..be2d36e 100644 --- a/backend/migrations/000070_distribution_chat_wallet_isolation.sql +++ b/backend/migrations/000070_distribution_chat_wallet_isolation.sql @@ -1,41 +1,169 @@ -- +goose Up --- 1. chat_conversations 表新增 distribution_site_id -ALTER TABLE chat_conversations - ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER listing_id, - ADD KEY idx_chat_conversations_distribution_site (distribution_site_id, status); +-- 1. chat_conversations 表新增 distribution_site_id 与索引 +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'chat_conversations' AND COLUMN_NAME = 'distribution_site_id') = 0, + 'ALTER TABLE chat_conversations ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER listing_id', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'chat_conversations' AND INDEX_NAME = 'idx_chat_conversations_distribution_site') = 0, + 'ALTER TABLE chat_conversations ADD KEY idx_chat_conversations_distribution_site (distribution_site_id, status)', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; -- 2. wallet_accounts 表新增 distribution_site_id 并将唯一键变更为 (user_id, distribution_site_id) -ALTER TABLE wallet_accounts - ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id, - DROP KEY uk_wallet_accounts_user_id, - ADD UNIQUE KEY uk_wallet_accounts_user_site (user_id, distribution_site_id); +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND COLUMN_NAME = 'distribution_site_id') = 0, + 'ALTER TABLE wallet_accounts ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND INDEX_NAME = 'uk_wallet_accounts_user_id') > 0, + 'ALTER TABLE wallet_accounts DROP KEY uk_wallet_accounts_user_id', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND INDEX_NAME = 'uk_wallet_accounts_user_site') = 0, + 'ALTER TABLE wallet_accounts ADD UNIQUE KEY uk_wallet_accounts_user_site (user_id, distribution_site_id)', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; -- 3. wallet_ledger 表新增 distribution_site_id 列与联合索引 -ALTER TABLE wallet_ledger - ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id, - ADD KEY idx_wallet_ledger_user_site (user_id, distribution_site_id); +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_ledger' AND COLUMN_NAME = 'distribution_site_id') = 0, + 'ALTER TABLE wallet_ledger ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_ledger' AND INDEX_NAME = 'idx_wallet_ledger_user_site') = 0, + 'ALTER TABLE wallet_ledger ADD KEY idx_wallet_ledger_user_site (user_id, distribution_site_id)', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; -- 4. withdrawal_requests 表新增 distribution_site_id 列与联合索引 -ALTER TABLE withdrawal_requests - ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id, - ADD KEY idx_withdrawal_user_site (user_id, distribution_site_id); +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'withdrawal_requests' AND COLUMN_NAME = 'distribution_site_id') = 0, + 'ALTER TABLE withdrawal_requests ADD COLUMN distribution_site_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER user_id', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'withdrawal_requests' AND INDEX_NAME = 'idx_withdrawal_user_site') = 0, + 'ALTER TABLE withdrawal_requests ADD KEY idx_withdrawal_user_site (user_id, distribution_site_id)', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; -- +goose Down -ALTER TABLE withdrawal_requests - DROP KEY idx_withdrawal_user_site, - DROP COLUMN distribution_site_id; +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'withdrawal_requests' AND INDEX_NAME = 'idx_withdrawal_user_site') > 0, + 'ALTER TABLE withdrawal_requests DROP KEY idx_withdrawal_user_site', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; -ALTER TABLE wallet_ledger - DROP KEY idx_wallet_ledger_user_site, - DROP COLUMN distribution_site_id; +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'withdrawal_requests' AND COLUMN_NAME = 'distribution_site_id') > 0, + 'ALTER TABLE withdrawal_requests DROP COLUMN distribution_site_id', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; -ALTER TABLE wallet_accounts - DROP KEY uk_wallet_accounts_user_site, - ADD UNIQUE KEY uk_wallet_accounts_user_id (user_id), - DROP COLUMN distribution_site_id; +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_ledger' AND INDEX_NAME = 'idx_wallet_ledger_user_site') > 0, + 'ALTER TABLE wallet_ledger DROP KEY idx_wallet_ledger_user_site', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; -ALTER TABLE chat_conversations - DROP KEY idx_chat_conversations_distribution_site, - DROP COLUMN distribution_site_id; +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_ledger' AND COLUMN_NAME = 'distribution_site_id') > 0, + 'ALTER TABLE wallet_ledger DROP COLUMN distribution_site_id', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND INDEX_NAME = 'uk_wallet_accounts_user_site') > 0, + 'ALTER TABLE wallet_accounts DROP KEY uk_wallet_accounts_user_site', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND INDEX_NAME = 'uk_wallet_accounts_user_id') = 0, + 'ALTER TABLE wallet_accounts ADD UNIQUE KEY uk_wallet_accounts_user_id (user_id)', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_accounts' AND COLUMN_NAME = 'distribution_site_id') > 0, + 'ALTER TABLE wallet_accounts DROP COLUMN distribution_site_id', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'chat_conversations' AND INDEX_NAME = 'idx_chat_conversations_distribution_site') > 0, + 'ALTER TABLE chat_conversations DROP KEY idx_chat_conversations_distribution_site', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'chat_conversations' AND COLUMN_NAME = 'distribution_site_id') > 0, + 'ALTER TABLE chat_conversations DROP COLUMN distribution_site_id', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; diff --git a/backend/migrations/000071_distribution_notification_isolation.sql b/backend/migrations/000071_distribution_notification_isolation.sql index ebd2052..1ad240f 100644 --- a/backend/migrations/000071_distribution_notification_isolation.sql +++ b/backend/migrations/000071_distribution_notification_isolation.sql @@ -1,9 +1,23 @@ -- +goose Up -- 1. notifications 表新增 distribution_site_id 并添加联合索引 -ALTER TABLE notifications - ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER biz_id, - ADD KEY idx_notifications_user_site (user_id, distribution_site_id, id); +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'notifications' AND COLUMN_NAME = 'distribution_site_id') = 0, + 'ALTER TABLE notifications ADD COLUMN distribution_site_id BIGINT UNSIGNED NULL AFTER biz_id', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'notifications' AND INDEX_NAME = 'idx_notifications_user_site') = 0, + 'ALTER TABLE notifications ADD KEY idx_notifications_user_site (user_id, distribution_site_id, id)', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; -- 2. 回填历史分销订单的站内信通知归属 UPDATE notifications n @@ -13,6 +27,20 @@ WHERE n.biz_type = 'order' AND o.distribution_site_id IS NOT NULL AND o.distribu -- +goose Down -ALTER TABLE notifications - DROP KEY idx_notifications_user_site, - DROP COLUMN distribution_site_id; +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'notifications' AND INDEX_NAME = 'idx_notifications_user_site') > 0, + 'ALTER TABLE notifications DROP KEY idx_notifications_user_site', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = IF( + (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'notifications' AND COLUMN_NAME = 'distribution_site_id') > 0, + 'ALTER TABLE notifications DROP COLUMN distribution_site_id', + 'SELECT 1' +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt;