-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement using pg_try_advisory_lock for db-migrations to be able to …
…run multiple instances again So we now always do db-migrate at the startup of the program. This way, we are sure that the database in the program is compatible with the queries in the program. However, these migrations are not necessarily safe to run in parallel, so in this commit I added a setup to ensure that the db-migrations are not run in parallel even when we start multiple instances of the program.
- Loading branch information
Francis Duvivier
committed
Dec 4, 2024
1 parent
bde959a
commit a6af53e
Showing
4 changed files
with
37 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
import { exec } from "node:child_process"; | ||
import { getClient } from "@db/connectionPool"; | ||
|
||
const MIGRATION_LOCK_ID = 108; | ||
|
||
export async function runMigrations() { | ||
// This code runs the npm script 'db-migrate:up' to make sure all migrations are done | ||
console.log(`Waiting for db-migrations lock [${MIGRATION_LOCK_ID}]`); | ||
const dbClient = getClient(); | ||
await dbClient.connect(); | ||
await dbClient.query(`SELECT pg_advisory_lock($1)`, [MIGRATION_LOCK_ID]); | ||
console.log("Running migrations via child process"); | ||
return new Promise<void>((resolve, reject) => { | ||
exec("npm run db-migrate:up", (error, stdout, stderr) => { | ||
stdout && console.log(stdout); | ||
stderr && console.error(stderr); | ||
if (error) { | ||
console.error(`Error running migrations: ${error}`); | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
console.log("Migrations done"); | ||
try { | ||
return await new Promise<void>((resolve, reject) => { | ||
exec("npm run db-migrate:up", (error, stdout, stderr) => { | ||
stdout && console.log(stdout); | ||
stderr && console.error(stderr); | ||
if (error) { | ||
console.error(`Error running migrations: ${error}`); | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
console.log("Migrations done"); | ||
}); | ||
}); | ||
}); | ||
} finally { | ||
await dbClient.end(); | ||
console.log("released db-migrations"); | ||
} | ||
} |