-- 与 room-service/deploy/mysql/migrations/004_room_list_owner_vip_level.sql 保持同一可重入结构。
-- owner 复合索引已经覆盖异步更新条件，本迁移不新增索引，也不改变列表排序执行计划。
DROP PROCEDURE IF EXISTS hyapp_migrate_072_room_list_owner_vip_level;

DELIMITER $$

CREATE PROCEDURE hyapp_migrate_072_room_list_owner_vip_level()
BEGIN
  IF EXISTS (
    SELECT 1 FROM information_schema.tables
    WHERE table_schema = DATABASE() AND table_name = 'room_list_entries'
  ) THEN
    IF NOT EXISTS (
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE() AND table_name = 'room_list_entries' AND column_name = 'owner_vip_level'
    ) THEN
      ALTER TABLE room_list_entries
        ADD COLUMN owner_vip_level INT NOT NULL DEFAULT 0 COMMENT '房主当前 effective VIP 等级异步投影' AFTER owner_user_id,
        ALGORITHM=INSTANT;
    END IF;
    IF NOT EXISTS (
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE() AND table_name = 'room_list_entries' AND column_name = 'owner_vip_expires_at_ms'
    ) THEN
      ALTER TABLE room_list_entries
        ADD COLUMN owner_vip_expires_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '房主当前 effective VIP 到期时间，UTC epoch ms' AFTER owner_vip_level,
        ALGORITHM=INSTANT;
    END IF;
  END IF;
END$$

CALL hyapp_migrate_072_room_list_owner_vip_level()$$
DROP PROCEDURE IF EXISTS hyapp_migrate_072_room_list_owner_vip_level$$

DELIMITER ;
