-- 012_tenancy.sql — tenancy phase 1 (DESIGN_TENANCY_ENTITLEMENTS.md): -- Tenant / TenantMember / TenantEntitlement / QuotaUsage, User.tenantId, -- and the implicit-default-tenant backfill for existing installs. -- -- Additive and idempotent; schema.sql carries identical structures so this -- is a structural no-op on a fresh database (db-converge proves both paths -- land the same). The User.tenantId FK is re-created by NAME under a guard: -- converge pass 2 drops migration-created tables with CASCADE (which removes -- the constraint from the non-migration-created User table) and re-runs the -- chain, so an unguarded inline ADD COLUMN would leave the FK missing. -- -- Backfill policy: one implicit tenant (slug 'default', fixed id) adopts -- every existing user; ADMIN users become membership 'owner', others -- 'member'. Plan starts 'selfhosted'; the app reconciles the implicit -- tenant's plan with TIER at startup (core -> 'core', multi -> 'free') and -- never overwrites a sold plan ('standard'/'premium'). CREATE TABLE IF NOT EXISTS "Tenant" ( id text PRIMARY KEY, name text NOT NULL, slug text NOT NULL UNIQUE, plan text NOT NULL DEFAULT 'selfhosted' CHECK (plan IN ('free','standard','premium','core','selfhosted')), status text NOT NULL DEFAULT 'active' CHECK (status IN ('active','suspended','closed')), version integer NOT NULL DEFAULT 1, "createdAt" timestamptz NOT NULL DEFAULT now(), "updatedAt" timestamptz NOT NULL DEFAULT now() ); ALTER TABLE "User" ADD COLUMN IF NOT EXISTS "tenantId" text; DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_constraint WHERE conname = 'User_tenantId_fkey' AND conrelid = '"User"'::regclass ) THEN ALTER TABLE "User" ADD CONSTRAINT "User_tenantId_fkey" FOREIGN KEY ("tenantId") REFERENCES "Tenant"(id) ON DELETE SET NULL; END IF; END $$; CREATE TABLE IF NOT EXISTS "TenantMember" ( id text PRIMARY KEY, "tenantId" text NOT NULL REFERENCES "Tenant"(id) ON DELETE CASCADE, "userId" text NOT NULL UNIQUE REFERENCES "User"(id) ON DELETE CASCADE, role text NOT NULL DEFAULT 'member' CHECK (role IN ('owner','admin','member')), "createdAt" timestamptz NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS "TenantMember_tenantId_idx" ON "TenantMember"("tenantId"); CREATE TABLE IF NOT EXISTS "TenantEntitlement" ( id text PRIMARY KEY, "tenantId" text NOT NULL REFERENCES "Tenant"(id) ON DELETE CASCADE, key text NOT NULL, value text NOT NULL, "createdAt" timestamptz NOT NULL DEFAULT now(), UNIQUE ("tenantId", key) ); CREATE TABLE IF NOT EXISTS "QuotaUsage" ( "tenantId" text NOT NULL REFERENCES "Tenant"(id) ON DELETE CASCADE, key text NOT NULL, used bigint NOT NULL DEFAULT 0, "updatedAt" timestamptz NOT NULL DEFAULT now(), PRIMARY KEY ("tenantId", key) ); -- Backfill (idempotent): implicit tenant adopts every tenantless user. INSERT INTO "Tenant"(id, name, slug, plan) VALUES ('tenant-default', 'Default', 'default', 'selfhosted') ON CONFLICT DO NOTHING; UPDATE "User" SET "tenantId" = 'tenant-default' WHERE "tenantId" IS NULL; INSERT INTO "TenantMember"(id, "tenantId", "userId", role) SELECT 'tmem-' || u.id, 'tenant-default', u.id, CASE WHEN u.role = 'ADMIN' THEN 'owner' ELSE 'member' END FROM "User" u ON CONFLICT ("userId") DO NOTHING;