-- Fix: FocusItem.source 列通过 prisma db push 添加到生产但未在 migration 中记录
-- 使用条件逻辑确保可重复执行（列可能已存在）
SET @col_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'FocusItem' AND COLUMN_NAME = 'source');

SET @sql = IF(@col_exists = 0,
  'ALTER TABLE `FocusItem` ADD COLUMN `source` VARCHAR(32) NULL AFTER `status`',
  'SELECT "FocusItem.source already exists, skipping" AS info');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 同样条件添加 createdAt 索引
SET @idx_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'FocusItem' AND INDEX_NAME = 'FocusItem_createdAt_idx');

SET @sql2 = IF(@idx_exists = 0,
  'ALTER TABLE `FocusItem` ADD INDEX `FocusItem_createdAt_idx` (`createdAt`)',
  'SELECT "FocusItem.createdAt_idx already exists, skipping" AS info');

PREPARE stmt2 FROM @sql2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
