From 33e9f85f41e16109007f6a5296cff4eedb7f3cb9 Mon Sep 17 00:00:00 2001 From: mahmood Date: Thu, 26 Nov 2026 14:47:41 +0330 Subject: [PATCH] fix(prisma): skip migrations already applied by prisma migrate deploy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The custom runner only tracks its own table, so a database brought up with `prisma migrate deploy` looked empty to it and the baseline was replayed — failing on "type AdminRole already exists". Read applied names from `_prisma_migrations` too (a no-op when that table does not exist). Co-Authored-By: Claude Haiku 5.5 --- apps/api/src/prisma/migrate.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/apps/api/src/prisma/migrate.ts b/apps/api/src/prisma/migrate.ts index b989d3bc..4dcc2bb6 100644 --- a/apps/api/src/prisma/migrate.ts +++ b/apps/api/src/prisma/migrate.ts @@ -196,6 +196,19 @@ export async function runMigrations(prisma: PrismaClient): Promise row.name)); + // A database brought up with `prisma migrate deploy` only has rows in + // `_prisma_migrations`. Without this, our runner replays the baseline and + // dies on `type "AdminRole" already exists`. Fresh installs that only use + // this runner never create that table — the query simply fails and we move on. + try { + const prismaApplied = await prisma.$queryRawUnsafe<{ migration_name: string }[]>( + `SELECT migration_name FROM "_prisma_migrations" WHERE finished_at IS NOT NULL`, + ); + for (const row of prismaApplied) appliedNames.add(row.migration_name); + } catch { + // no _prisma_migrations table + } + const applied: string[] = []; for (const file of files) { const legacyName = LEGACY_NAME_ALIASES[file.name];