SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

USE hyapp_lucky_gift;

-- 性能边界：规则版本表只增加常量默认列，强制 ALGORITHM=INSTANT，迁移不扫描或回写历史版本。
-- 若目标 MySQL 不支持即时 DDL 则直接失败，不能退化为 COPY；执行前仍需检查长事务，避免元数据锁等待。
SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_rule_versions'
     AND COLUMN_NAME = 'user_24h_rtp_threshold_ppm') = 0,
  'ALTER TABLE lucky_gift_rule_versions ADD COLUMN user_24h_rtp_threshold_ppm BIGINT NOT NULL DEFAULT 0 COMMENT ''用户滚动24小时RTP降权触发线，ppm；0表示关闭'' AFTER recharge_boost_factor_ppm, ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_rule_versions'
     AND COLUMN_NAME = 'user_24h_ordinary_win_factor_ppm') = 0,
  'ALTER TABLE lucky_gift_rule_versions ADD COLUMN user_24h_ordinary_win_factor_ppm BIGINT NOT NULL DEFAULT 1000000 COMMENT ''触发后普通非零奖档保留权重，ppm；1000000表示不降权'' AFTER user_24h_rtp_threshold_ppm, ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE stmt FROM @ddl; EXECUTE stmt; DEALLOCATE PREPARE stmt;
