Skip to content

test: cover concurrent first install of the schema - #664

Open
mnevfel wants to merge 1 commit into
graphile:mainfrom
mnevfel:test/concurrent-schema-install
Open

mnevfel wants to merge 1 commit into
graphile:mainfrom
mnevfel:test/concurrent-schema-install

Conversation

@mnevfel

@mnevfel mnevfel commented Dec 1, 2026

Copy link
Copy Markdown

Refs #496

On current main I could not reproduce the race from #496. As far as I can tell both points in the issue are already handled (the clash from installSchema is caught and retried, and there is a randomized sleep), so this PR only adds tests to keep that in place. There are no source changes.

What I ran: a script that calls runMigrations() N times in parallel against a freshly created, empty database and then drops it, on PostgreSQL 16 over a unix socket. I varied N from 3 to 60 and ran several of these at once under full CPU load, about 7,700 calls in about 620 empty databases in total. Every call resolved and every database ended with all 20 migrations. No unique violation, deadlock or missing relation escaped migrate().

The existing test "multiple concurrent installs of the schema is fine" already covers this in spirit, but it depends on timing, so it cannot show that the losing path is actually taken.

New file __tests__/migrateConcurrent.test.ts:

  1. One connection starts the install inside an open transaction. Two more call migrate() and block behind it, and the test waits for that through pg_stat_activity. Then the first connection commits, and both blocked callers get the unique violation on the schema and must carry on. No luck involved.
  2. Ten parallel runMigrations() calls, three rounds, each round into a schema that does not exist yet, through the public API with one pool per call.

I broke migrate() three ways and ran the new file three times each. Rethrowing the clash error from installSchema failed the deterministic test in every run and most of the parallel rounds. Skipping the install when the schema is missing, and removing 23505 from the clash codes, each failed all four tests. Reducing the loop to a single attempt is not caught here or by the existing test; it still ends in the right state because the migrations tolerate the clash themselves.

Both migrate test files together passed 5 times in a row (9 tests), and tsc, eslint and prettier are clean. I added no changeset since nothing published changes; I can add an empty one if you prefer.

If you still see this with a particular setup (Postgres version, number of workers, a pooler in front), I am happy to try that exact case. If not, #496 may be ready to close.

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.

1 participant