topfans/docs/migrations/002_reduce_slots_to_2.sql
2026-05-20 14:18:44 +08:00

43 lines
1.2 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 展位重构迁移:从 6 槽位缩减为 2 槽位
-- 执行时间2026-05-20
-- 执行人:待填写
-- 风险等级:高(删除数据)
-- ========== 备份(建议执行前手动备份)==========
-- 备份 booth_slots 表
-- CREATE TABLE booth_slots_backup_20260520 AS SELECT * FROM booth_slots;
-- 备份 exhibitions 表
-- CREATE TABLE exhibitions_backup_20260520 AS SELECT * FROM exhibitions;
-- ========== 迁移开始 ==========
-- 1. 先删除 slot_index > 2 的展览记录(引用完整性)
DELETE FROM exhibitions
WHERE slot_id IN (
SELECT slot_id FROM booth_slots
WHERE slot_index > 2
);
-- 2. 删除 slot_index > 2 的槽位记录
DELETE FROM booth_slots
WHERE slot_index > 2;
-- 3. 验证清理结果
SELECT user_id, star_id, COUNT(*) as slot_count
FROM booth_slots
GROUP BY user_id, star_id
HAVING COUNT(*) > 2;
-- 应该返回空结果,表示每个用户的每个明星展馆最多只有 2 个槽位
-- 4. 验证 exhibitions 没有残留(检查是否有关联到已删除槽位的展览)
SELECT COUNT(*) as orphaned_exhibitions
FROM exhibitions e
WHERE NOT EXISTS (
SELECT 1 FROM booth_slots bs WHERE bs.slot_id = e.slot_id
);
-- 应该返回 0
-- ========== 迁移完成 ==========