-- 为 users 表添加订阅相关列（subscription_type, subscription_expires_at）
-- 若列已存在则跳过，可重复执行。
-- 执行方式: psql -U <user> -d <database> -f api/migrations/add_users_subscription_columns.sql

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_schema = 'public' AND table_name = 'users' AND column_name = 'subscription_type'
    ) THEN
        ALTER TABLE users ADD COLUMN subscription_type VARCHAR DEFAULT 'free';
        RAISE NOTICE '已添加 users.subscription_type 列';
    ELSE
        RAISE NOTICE 'users.subscription_type 列已存在，跳过';
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_schema = 'public' AND table_name = 'users' AND column_name = 'subscription_expires_at'
    ) THEN
        ALTER TABLE users ADD COLUMN subscription_expires_at TIMESTAMP WITH TIME ZONE DEFAULT NULL;
        RAISE NOTICE '已添加 users.subscription_expires_at 列';
    ELSE
        RAISE NOTICE 'users.subscription_expires_at 列已存在，跳过';
    END IF;
END $$;
