-- Community & Research: Basis/Pro (06.10.2026).
-- Hand-written on top of the drizzle-kit snapshot: the generated variant cast
-- ("variant"::"post_variant") would fail on the old FULL/SHORT values, so the
-- enum is swapped with an explicit mapping instead. No row is deleted.
-- Rollback: docs/MIGRATION_0011_community_basis_pro.md.
CREATE TYPE "public"."subscription_tier" AS ENUM('BASIS', 'PRO');--> statement-breakpoint
ALTER TABLE "users" ADD COLUMN "subscription_tier" "subscription_tier";--> statement-breakpoint
ALTER TABLE "community_channels" ADD COLUMN "min_tier" "subscription_tier";--> statement-breakpoint
CREATE TYPE "public"."post_variant_new" AS ENUM('PRO', 'BASIS');--> statement-breakpoint
ALTER TABLE "community_posts" ALTER COLUMN "variant" DROP DEFAULT;--> statement-breakpoint
ALTER TABLE "community_posts" ALTER COLUMN "variant" SET DATA TYPE "public"."post_variant_new" USING (CASE "variant"::text WHEN 'FULL' THEN 'PRO' WHEN 'SHORT' THEN 'BASIS' END)::"public"."post_variant_new";--> statement-breakpoint
DROP TYPE "public"."post_variant";--> statement-breakpoint
ALTER TYPE "public"."post_variant_new" RENAME TO "post_variant";--> statement-breakpoint
ALTER TABLE "community_posts" ALTER COLUMN "variant" SET DEFAULT 'PRO';--> statement-breakpoint
-- Channel minimum tier. Open to everyone (min_tier stays NULL): CK Daily (the
-- edition of each post decides what Basis sees) and CK Trading Info. Matched by
-- id first, otherwise by the name, case-insensitive. Everything else becomes
-- PRO, including a channel that is not recognised and the channels Sir is
-- removing himself (Market Data & News, On-Chain) should they still exist.
-- Rows that are missing simply match nothing; only rows still NULL are touched,
-- so a second run changes nothing.
UPDATE "community_channels"
SET "min_tier" = CASE
  WHEN "id" IN ('ck-daily', 'ck-trading-info') THEN NULL
  WHEN lower(btrim("name")) IN ('ck daily', 'ck trading info') THEN NULL
  ELSE 'PRO'::"subscription_tier"
END
WHERE "min_tier" IS NULL;--> statement-breakpoint
-- CK Trading Info is open to Basis, so its existing posts must be the Basis
-- edition; the plain FULL -> PRO mapping above would lock them again.
UPDATE "community_posts" SET "variant" = 'BASIS'
WHERE "channel_id" IN (
  SELECT "id" FROM "community_channels"
  WHERE "id" = 'ck-trading-info' OR lower(btrim("name")) = 'ck trading info'
);
