-- 与 room-service/deploy/mysql/migrations/006_room_list_owner_vip_fallback.sql 保持同一可重入结构。
SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

DROP PROCEDURE IF EXISTS hyapp_migrate_075_room_list_owner_vip_fallback;

DELIMITER $$

CREATE PROCEDURE hyapp_migrate_075_room_list_owner_vip_fallback()
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_fallback_level'
    ) THEN
      ALTER TABLE room_list_entries
        ADD COLUMN owner_vip_fallback_level INT NOT NULL DEFAULT 0 COMMENT '体验卡到期后 wallet paid VIP 回落等级' AFTER owner_vip_expires_at_ms,
        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_fallback_expires_at_ms'
    ) THEN
      ALTER TABLE room_list_entries
        ADD COLUMN owner_vip_fallback_expires_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '回落 paid VIP 到期时间，UTC epoch ms' AFTER owner_vip_fallback_level,
        ALGORITHM=INSTANT;
    END IF;
  END IF;
END$$

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

DELIMITER ;
