-- 房主 VIP 只作为房间列表读模型，不进入 Room Cell 核心状态。
-- 两列均不参与索引；MySQL 8 INSTANT DDL 只更新数据字典，不扫描或重写 room_list_entries。
SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 显式选择 Room owner 库，保证本地完整迁移脚本和生产单文件执行落到同一边界。
USE hyapp_room;

DROP PROCEDURE IF EXISTS hyapp_migrate_004_room_list_owner_vip_level;

DELIMITER $$

CREATE PROCEDURE hyapp_migrate_004_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_004_room_list_owner_vip_level()$$
DROP PROCEDURE IF EXISTS hyapp_migrate_004_room_list_owner_vip_level$$

DELIMITER ;
