SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;
SET time_zone = '+00:00';

USE hyapp_wallet;

-- 绑定表是按区域和月份唯一的小型配置表；来源字段允许显式新月版本替换系统继承绑定，同时绝不覆盖显式历史版本。
SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'host_salary_policy_cycle_bindings' AND COLUMN_NAME = 'source_cycle_key') = 0,
  'ALTER TABLE host_salary_policy_cycle_bindings ADD COLUMN source_cycle_key VARCHAR(7) NOT NULL DEFAULT '''' COMMENT ''来源月份；等于 cycle_key 表示显式发布，否则表示继承'' AFTER policy_version, ALGORITHM=INPLACE, LOCK=NONE',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 迁移前的绑定均按显式版本处理；运行时继承代码会为之后的新绑定写入真实上月来源。
UPDATE host_salary_policy_cycle_bindings
SET source_cycle_key = cycle_key
WHERE source_cycle_key = '';
