Skip to content

Commit

Permalink
Implement using pg_try_advisory_lock for db-migrations to be able to …
Browse files Browse the repository at this point in the history
…run mulpile instances again
  • Loading branch information
Francis Duvivier committed Dec 3, 2024
1 parent faf95a0 commit 1218118
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"build": "tsc",
"swagger": "tsoa spec-and-routes",
"dev": "node --import tsx --watch src/index.ts",
"docker-compose-test:up": "docker compose -f compose.test.yml up -d",
"docker-compose-test:down": "docker compose -f compose.test.yml down",
"backup": "docker exec -it badgehub-api-db-1 /usr/bin/pg_dump --username badgehub badgehub -f /var/backup/data-backup-`date +\"%Y-%m-%dT%H:%m\"`.sql",
"overwrite-mockup-data": "docker exec -it badgehub-api-db-1 /usr/bin/pg_dump --username badgehub --schema badgehub badgehub > mockup-data.sql",
"test": "vitest --coverage.enabled true",
Expand Down
2 changes: 1 addition & 1 deletion process.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"interpreter_args": "--import tsx",
"name": "badgehub",
"exec_mode": "cluster",
"instances": 1,
"instances": 4,
"error_file": "./logs/error.log",
"out_file": "./logs/access.log",
"combine_logs": true
Expand Down
10 changes: 10 additions & 0 deletions src/db/connectionPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ export const getPool = () => {
}
return pool;
};

export const getClient = () => {
return new pg.Client({
host: POSTGRES_HOST,
database: POSTGRES_DB,
user: POSTGRES_USER,
password: POSTGRES_PASSWORD,
port: POSTGRES_PORT,
});
};
37 changes: 25 additions & 12 deletions src/db/migrations.ts
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");
}
}

0 comments on commit 1218118

Please sign in to comment.