-- 订阅排队系统升级迁移脚本
-- 执行前请备份数据

-- 1. 新增字段
ALTER TABLE subscriptions
ADD COLUMN IF NOT EXISTS feishu_name VARCHAR(64) DEFAULT '' COMMENT '飞书花名' AFTER email,
ADD COLUMN IF NOT EXISTS notified_at DATETIME NULL COMMENT '通知发送时间' AFTER created_at,
ADD COLUMN IF NOT EXISTS is_priority TINYINT(1) DEFAULT 0 COMMENT 'VIP优先级' AFTER notified_at;

-- 2. 新增索引
CREATE INDEX IF NOT EXISTS idx_notified ON subscriptions(notified_at);
CREATE INDEX IF NOT EXISTS idx_priority ON subscriptions(is_priority, created_at);

-- 3. 将旧的 triggered_at 数据迁移到 notified_at（如果存在）
UPDATE subscriptions SET notified_at = triggered_at WHERE triggered_at IS NOT NULL AND notified_at IS NULL;

-- 4. 删除旧字段（可选，建议先保留）
-- ALTER TABLE subscriptions DROP COLUMN triggered_at;
