@accounter/server
Version:
Accounter GraphQL server
55 lines • 2.03 kB
JavaScript
import { Pool } from 'pg';
import { LATEST_MIGRATION_NAME } from '../../../../migrations/src/run-pg-migrations.js';
export { LATEST_MIGRATION_NAME };
/**
* Check if the latest migration has been applied to the database.
* Returns result object with status - does not throw.
*/
export async function checkLatestMigration(clientOrPool, schema = 'accounter_schema') {
let client;
let shouldRelease = false;
if (clientOrPool instanceof Pool) {
const poolClient = await clientOrPool.connect();
client = poolClient;
shouldRelease = true;
}
else {
// It's already a Client or PoolClient
client = clientOrPool;
}
try {
const result = await client.query(`SELECT 1 FROM ${schema}.migration WHERE name = $1 LIMIT 1`, [LATEST_MIGRATION_NAME]);
const isLatest = result.rowCount === 1;
return {
isLatest,
latestMigrationName: LATEST_MIGRATION_NAME,
errorMessage: isLatest
? undefined
: `Latest migration "${LATEST_MIGRATION_NAME}" not found in database`,
};
}
catch (error) {
return {
isLatest: false,
latestMigrationName: LATEST_MIGRATION_NAME,
errorMessage: `Failed to check migrations: ${error instanceof Error ? error.message : String(error)}`,
};
}
finally {
if (shouldRelease && 'release' in client) {
await client.release();
}
}
}
/**
* Assert that the latest migration has been applied.
* Throws an error with helpful message if not.
*/
export async function assertLatestMigrationApplied(clientOrPool, schema = 'accounter_schema') {
const result = await checkLatestMigration(clientOrPool, schema);
if (!result.isLatest) {
throw new Error(result.errorMessage ||
`Migration check failed for ${result.latestMigrationName}. Run: yarn workspace @accounter/migrations migration:run`);
}
}
//# sourceMappingURL=migration-verification.js.map