-- +goose Up
-- +goose StatementBegin
ALTER TABLE media_assets
    ADD COLUMN IF NOT EXISTS variant varchar(32) NOT NULL DEFAULT 'main';

-- 回填历史数据：老数据一律视为 main。
UPDATE media_assets
SET variant = 'main'
WHERE variant IS NULL OR variant = '';

-- 约束：只允许 main/preview
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conname = 'ck_media_assets_variant'
    ) THEN
        ALTER TABLE media_assets
            ADD CONSTRAINT ck_media_assets_variant
                CHECK (variant IN ('main', 'preview'));
    END IF;
END
$$;

COMMENT ON COLUMN media_assets.variant IS '产物类型：main/preview；用于强制试看资源必须绑定独立产物，避免用正片绕过';

CREATE INDEX IF NOT EXISTS ix_media_assets_tenant_variant ON media_assets (tenant_id, variant);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP INDEX IF EXISTS ix_media_assets_tenant_variant;
ALTER TABLE media_assets DROP CONSTRAINT IF EXISTS ck_media_assets_variant;
ALTER TABLE media_assets DROP COLUMN IF EXISTS variant;
-- +goose StatementEnd
