games_app/backend/dist/tests/helpers/testDb.js
2026-07-29 15:35:11 +02:00

41 lines
1.5 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDbTestEnabled = isDbTestEnabled;
exports.ensureTestMigrations = ensureTestMigrations;
const config_1 = require("../../config");
const connection_1 = require("../../db/connection");
/** Set RUN_DB_TESTS=1 and DATABASE_URL to run database integration tests. */
function isDbTestEnabled() {
return process.env.RUN_DB_TESTS === '1' && Boolean(config_1.config.database.url?.trim());
}
/** Stable lock key for cross-process migration serialization. */
const MIGRATION_LOCK_KEY = 872364101;
let migrationsPromise = null;
/**
* Run migrations once per process, and serialize across parallel test workers
* via a Postgres advisory lock (prevents 40P01 deadlocks on schema.sql).
*/
async function ensureTestMigrations() {
if (!migrationsPromise) {
migrationsPromise = (async () => {
const client = await (0, connection_1.getClient)();
try {
await client.query('SELECT pg_advisory_lock($1)', [MIGRATION_LOCK_KEY]);
try {
await (0, connection_1.runMigrations)();
}
finally {
await client.query('SELECT pg_advisory_unlock($1)', [MIGRATION_LOCK_KEY]);
}
}
finally {
client.release();
}
})().catch((err) => {
migrationsPromise = null;
throw err;
});
}
await migrationsPromise;
}
//# sourceMappingURL=testDb.js.map