iloom-flatten/migrations/20260604_132401_fix_data_integrity_issues.sql

38 lines
1.1 KiB
MySQL
Raw Normal View History

-- ============================================
-- 此文件由 Meoo Cloud 自动生成,请勿修改
-- This file is auto-generated by Meoo Cloud, DO NOT MODIFY
-- ============================================
-- 数据修复迁移:清理孤儿记录和修复状态不一致
-- 1. 删除 share_links 孤儿记录(引用的计划已不存在)
DELETE FROM share_links
WHERE plan_id NOT IN (SELECT id FROM production_plans);
-- 2. 修复计划状态不一致:将 pending 但 confirm 已完成的计划更新为 producing
UPDATE production_plans
SET status = 'producing', updated_at = NOW()
WHERE status = 'pending'
AND EXISTS (
SELECT 1 FROM plan_process_steps pps
WHERE pps.plan_id = production_plans.id
AND pps.step_type = 'confirm'
AND pps.status = 'completed'
);
-- 3. 标记已过期的 share_links 为 expired
UPDATE share_links
SET status = 'expired'
WHERE status != 'expired'
AND expires_at IS NOT NULL
AND expires_at < NOW();
-- 4. 补填已确认链接的 used_at 字段
UPDATE share_links
SET used_at = responded_at
WHERE status = 'confirmed'
AND used_at IS NULL
AND responded_at IS NOT NULL;