From 7ee821d46e3e50f23db38f69a086b8345b3757f8 Mon Sep 17 00:00:00 2001 From: kevin-asprec Date: Wed, 15 Apr 2026 10:39:10 +0800 Subject: [PATCH] feat: add migration to make user tenantId nullable for platform-level superadmin --- .../migration.sql | 12 ++++++++++++ packages/db/prisma/seed.ts | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 packages/db/prisma/migrations/20260415000000_make_user_tenantid_nullable/migration.sql diff --git a/packages/db/prisma/migrations/20260415000000_make_user_tenantid_nullable/migration.sql b/packages/db/prisma/migrations/20260415000000_make_user_tenantid_nullable/migration.sql new file mode 100644 index 0000000..2683602 --- /dev/null +++ b/packages/db/prisma/migrations/20260415000000_make_user_tenantid_nullable/migration.sql @@ -0,0 +1,12 @@ +-- AlterColumn: make tenantId nullable for platform-level users (superadmin) +ALTER TABLE "users" ALTER COLUMN "tenantId" DROP NOT NULL; + +-- Drop existing FK and recreate with ON DELETE SET NULL +ALTER TABLE "users" DROP CONSTRAINT "users_tenantId_fkey"; +ALTER TABLE "users" ADD CONSTRAINT "users_tenantId_fkey" + FOREIGN KEY ("tenantId") REFERENCES "tenants"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- Recreate unique index to allow multiple NULL tenantId values +DROP INDEX IF EXISTS "users_tenantId_email_key"; +CREATE UNIQUE INDEX "users_tenantId_email_key" ON "users"("tenantId", "email") WHERE "tenantId" IS NOT NULL; +CREATE UNIQUE INDEX "users_email_key" ON "users"("email") WHERE "tenantId" IS NULL; diff --git a/packages/db/prisma/seed.ts b/packages/db/prisma/seed.ts index b5b712b..86b3d81 100644 --- a/packages/db/prisma/seed.ts +++ b/packages/db/prisma/seed.ts @@ -102,7 +102,7 @@ async function main() { // ─── Super Admin (platform-level, no tenant) ─────────── const superAdmin = await prisma.user.create({ - data: { tenantId: tenant.id, email: 'superadmin@fiberops.dev', password: await hashPassword('admin123!'), firstName: 'Super', lastName: 'Admin' }, + data: { tenantId: null, email: 'superadmin@fiberops.dev', password: await hashPassword('admin123!'), firstName: 'Super', lastName: 'Admin' }, }); await prisma.userRole.create({ data: { userId: superAdmin.id, role: 'super_admin' } }); console.log(`Super Admin: superadmin@fiberops.dev (super_admin)`);