feat: add RUN_SEED support and nullable tenantId migration

- New migration: make users.tenantId nullable for super_admin
- Dockerfile: RUN_SEED=true env var triggers seed after migrations
- Seed uses npx tsx (available in node_modules)
This commit is contained in:
kevin-asprec
2026-04-15 06:04:57 +08:00
parent 555bd4a9f8
commit bef320f32e
2 changed files with 13 additions and 1 deletions

View File

@@ -34,4 +34,4 @@ COPY --from=builder /app/packages/db/src ./packages/db/src
ENV NODE_ENV=production
EXPOSE 3001
ENTRYPOINT ["dumb-init", "--"]
CMD ["sh", "-c", "cd packages/db && npx prisma migrate deploy && cd /app && node dist/main"]
CMD ["sh", "-c", "cd packages/db && npx prisma migrate deploy && if [ \"$RUN_SEED\" = \"true\" ]; then echo 'Seeding database...' && npx tsx prisma/seed.ts; fi && cd /app && node dist/main"]

View File

@@ -0,0 +1,12 @@
-- AlterTable: make tenantId nullable for super_admin users
ALTER TABLE "users" ALTER COLUMN "tenantId" DROP NOT NULL;
-- Fix FK to allow NULL (super_admin has no tenant)
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;
-- Drop unique index that requires tenantId (email must be unique globally for super_admin)
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;