-- +goose Up -- 1. notifications 表新增 distribution_site_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 JOIN rental_orders o ON n.biz_id = o.id SET n.distribution_site_id = o.distribution_site_id WHERE n.biz_type = 'order' AND o.distribution_site_id IS NOT NULL AND o.distribution_site_id > 0; -- +goose Down 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;