-
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 mulpile instances again
- Loading branch information
Francis Duvivier
committed
Dec 3, 2024
1 parent
faf95a0
commit 1218118
Showing
4 changed files
with
38 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,32 @@ | ||
import { exec } from "node:child_process"; | ||
import { getClient, getPool } from "@db/connectionPool"; | ||
import sql from "sql-template-tag"; | ||
|
||
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(sql`SELECT pg_advisory_lock(${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"); | ||
} | ||
} |