Skip to content

fix(db): acquire postgres advisory lock during migration execution (#1440) - #1511

Open
Awosdot wants to merge 1 commit into
Junirezz:mainfrom
Awosdot:feature/issue-1440-advisory-lock-migration-runner
Open

Awosdot wants to merge 1 commit into
Junirezz:mainfrom
Awosdot:feature/issue-1440-advisory-lock-migration-runner

Conversation

@Awosdot

@Awosdot Awosdot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

What changed

  • Updated backend/src/db/migrate.ts to acquire a PostgreSQL advisory lock (pg_advisory_lock(727727)) prior to executing prisma migrate deploy, releasing it in a finally block[cite: 11].
  • Added a 5-second retry mechanism if the lock is initially unavailable during deployment races[cite: 11].
  • Added migration_lock_acquired metric logging upon successful lock acquisition[cite: 11].
  • Added unit/integration tests simulating concurrent migration execution to assert single execution and prevent P3005 errors[cite: 11].

Why

When multiple application pods start concurrently, racing prisma migrate deploy operations lead to P3005 migration already applied database errors and service crash loops[cite: 11]. Using Postgres advisory locks guarantees mutual exclusion across pod instances on startup[cite: 11].

How tested

  • Ran concurrent execution test suite confirming only one process executes migrations while secondary processes wait/defer safely[cite: 11].
  • Confirmed single-pod startup and existing test workflows complete without regression[cite: 11].

Closes

Closes #1440

@drips-wave

drips-wave Bot commented Sep 29, 2026

Copy link
Copy Markdown

@Awosdot Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Database migration runner applies migrations out of order when two deploys race on startup

1 participant