Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(dev): retry migrations on deadlock (#960)

Carbon Agentf15ea3856475
diff --git a/packages/dev/src/services/migrations.ts b/packages/dev/src/services/migrations.ts
index 7f62a6227c..8f75bf91c4 100644
--- a/packages/dev/src/services/migrations.ts
+++ b/packages/dev/src/services/migrations.ts
@@ -135,11 +135,29 @@ export async function applyMigrations(
const dbUrl = `postgresql://supabase_admin:postgres@localhost:${dbPort}/postgres`;
const args = ["migration", "up", "--include-all", "--db-url", dbUrl];
const cwd = join(root, "packages/database");
- const r = await execa("supabase", args, {
- cwd,
- reject: false,
- preferLocal: true
- });
+
+ // Retry up to 3 times on deadlock — background services (PostgREST,
+ // Realtime) hold catalog locks that race with CREATE POLICY / ALTER TABLE.
+ const MAX_RETRIES = 3;
+ let r: Awaited<ReturnType<typeof execa>>;
+ for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
+ r = await execa("supabase", args, {
+ cwd,
+ reject: false,
+ preferLocal: true
+ });
+ if (r.exitCode === 0) break;
+ const output = `${r.stderr ?? ""}\n${r.stdout ?? ""}`;
+ if (/deadlock detected/i.test(output) && attempt < MAX_RETRIES) {
+ log.warn(
+ `deadlock during migration (attempt ${attempt}/${MAX_RETRIES}) — retrying in 3s`
+ );
+ await sleep(3000);
+ continue;
+ }
+ break;
+ }
+ // @ts-expect-error — r is always assigned after the loop
if (r.exitCode !== 0) {
const output = `${r.stderr ?? ""}\n${r.stdout ?? ""}`;
// Auto-repair: DB has migration versions not present locally (stale from