fix(migration): 提升 000070 与 000071 迁移幂等性,兼容已存在的索引与字段
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user