File tree Expand file tree Collapse file tree
packages/db/script-migrations Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -30,6 +30,18 @@ export async function runScriptMigrations(sql: Sql): Promise<void> {
3030 names . add ( migration . name )
3131 }
3232
33+ /**
34+ * The SQL-migration phase leaves `lock_timeout = '5s'` on this session (DDL
35+ * must fail fast rather than queue a table-wide stall). Script migrations
36+ * want the opposite: they block on app-held row/advisory locks (e.g. the
37+ * per-table insert lock in the order_key backfill) and must wait them out —
38+ * exactly like the standalone scripts they replace, which ran on fresh
39+ * connections with the Postgres defaults. `SET` cannot be parameterized,
40+ * hence `unsafe` with constants.
41+ */
42+ await sql . unsafe ( 'SET statement_timeout = 0' )
43+ await sql . unsafe ( 'SET lock_timeout = 0' )
44+
3345 await sql `
3446 CREATE TABLE IF NOT EXISTS script_migrations (
3547 name text PRIMARY KEY,
@@ -56,7 +68,10 @@ export async function runScriptMigrations(sql: Sql): Promise<void> {
5668 console . log ( `Applying script migration ${ migration . name } ...` )
5769 const startedAt = Date . now ( )
5870 await migration . up ( sql )
59- await sql `INSERT INTO script_migrations (name) VALUES (${ migration . name } )`
71+ await sql `
72+ INSERT INTO script_migrations (name) VALUES (${ migration . name } )
73+ ON CONFLICT (name) DO NOTHING
74+ `
6075 console . log ( `Script migration ${ migration . name } applied in ${ Date . now ( ) - startedAt } ms.` )
6176 }
6277}
Original file line number Diff line number Diff line change @@ -26,6 +26,10 @@ export interface ScriptMigration {
2626 name : string
2727 /** Env vars the migration needs; the runner throws before `up` if any is unset. */
2828 requiredEnv ?: readonly string [ ]
29- /** Applies the migration using the runner's session (statement_timeout is already 0). */
29+ /**
30+ * Applies the migration using the runner's session. The runner resets
31+ * `statement_timeout` and `lock_timeout` to 0 first, so long backfills and
32+ * blocking lock acquisitions wait instead of aborting with `55P03`.
33+ */
3034 up ( sql : Sql ) : Promise < void >
3135}
You can’t perform that action at this time.
0 commit comments