0010_orgs.sql 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. -- Move from user-owned to org-owned data model.
  2. -- Runs in one transaction; rollback on any error leaves the DB untouched.
  3. BEGIN;
  4. -- 1. New tables
  5. CREATE TABLE "organizations" (
  6. "id" text PRIMARY KEY NOT NULL,
  7. "slug" text NOT NULL,
  8. "name" text NOT NULL,
  9. "personal" boolean DEFAULT false NOT NULL,
  10. "created_by" text NOT NULL REFERENCES "users"("id"),
  11. "created_at" timestamp with time zone DEFAULT now() NOT NULL,
  12. "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
  13. "deleted_at" timestamp with time zone
  14. );
  15. CREATE UNIQUE INDEX "organizations_slug_idx" ON "organizations" ("slug");
  16. CREATE TABLE "org_members" (
  17. "org_id" text NOT NULL REFERENCES "organizations"("id") ON DELETE CASCADE,
  18. "user_id" text NOT NULL REFERENCES "users"("id") ON DELETE CASCADE,
  19. "role" text DEFAULT 'developer' NOT NULL,
  20. "created_at" timestamp with time zone DEFAULT now() NOT NULL,
  21. "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
  22. PRIMARY KEY ("org_id", "user_id")
  23. );
  24. CREATE INDEX "org_members_user_id_idx" ON "org_members" ("user_id");
  25. -- 2. Seed one personal org per existing user.
  26. -- Slug derived from email local-part, lowercased, non-alnum → '-'.
  27. -- For today's single dev user there is no slug collision; for any future
  28. -- pre-migration collision the INSERT will fail and the whole transaction
  29. -- rolls back — safer than silently salting.
  30. INSERT INTO "organizations" ("id", "slug", "name", "personal", "created_by", "created_at", "updated_at")
  31. SELECT
  32. 'org_' || u."id",
  33. lower(regexp_replace(split_part(u."email", '@', 1), '[^a-z0-9]+', '-', 'gi')),
  34. COALESCE(NULLIF(u."name", ''), split_part(u."email", '@', 1)),
  35. true,
  36. u."id",
  37. now(),
  38. now()
  39. FROM "users" u;
  40. -- 3. Make each user the owner of their personal org.
  41. INSERT INTO "org_members" ("org_id", "user_id", "role", "created_at", "updated_at")
  42. SELECT 'org_' || u."id", u."id", 'owner', now(), now()
  43. FROM "users" u;
  44. -- 4. Add nullable org_id FK columns.
  45. ALTER TABLE "subscriptions" ADD COLUMN "org_id" text REFERENCES "organizations"("id") ON DELETE CASCADE;
  46. ALTER TABLE "projects" ADD COLUMN "org_id" text REFERENCES "organizations"("id") ON DELETE CASCADE;
  47. -- 5. Backfill — each row points to its owner's personal org.
  48. UPDATE "subscriptions" SET "org_id" = 'org_' || "owner_id";
  49. UPDATE "projects" SET "org_id" = 'org_' || "owner_id";
  50. -- 6. Lock it down.
  51. ALTER TABLE "subscriptions" ALTER COLUMN "org_id" SET NOT NULL;
  52. ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_org_idx" UNIQUE ("org_id");
  53. ALTER TABLE "projects" ALTER COLUMN "org_id" SET NOT NULL;
  54. CREATE INDEX "projects_org_idx" ON "projects" ("org_id");
  55. -- 7. Drop the old unique-per-owner constraint on subscriptions, and the old owner_id columns.
  56. DROP INDEX IF EXISTS "subscriptions_owner_idx";
  57. DROP INDEX IF EXISTS "projects_owner_idx";
  58. ALTER TABLE "subscriptions" DROP COLUMN "owner_id";
  59. ALTER TABLE "projects" DROP COLUMN "owner_id";
  60. COMMIT;