-- 修复 signin_logs 外键：删除账号时级联删除签到记录
-- 用法: mysql -u weibo -p123456 weibo_hotsign < migrate_fix_signin_logs_fk.sql

-- 先查出旧外键名并删除，再重建
SET @fk_name = (
    SELECT CONSTRAINT_NAME FROM information_schema.KEY_COLUMN_USAGE
    WHERE TABLE_SCHEMA = 'weibo_hotsign'
      AND TABLE_NAME = 'signin_logs'
      AND COLUMN_NAME = 'account_id'
      AND REFERENCED_TABLE_NAME = 'accounts'
    LIMIT 1
);

SET @sql = CONCAT('ALTER TABLE signin_logs DROP FOREIGN KEY ', @fk_name);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

ALTER TABLE signin_logs
    ADD CONSTRAINT fk_signin_logs_account
    FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE;
