mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
fix(db): leave nothing behind when a first Postgres boot fails, so RESTORE_FROM_BACKUP still runs
createFromBaseline created the migration history's table through MikroORM's storage before, and outside, the transaction that runs the baseline. Without a master transaction MikroORM commits that table on its own, so a first boot that failed in the baseline (a server without ICU, a timeout, a dropped connection) left an empty mikro_orm_migrations behind. The next start with RESTORE_FROM_BACKUP took that for a database in use, skipped the restore with a warning and brought up an empty instance, after which the restore never ran. The table is now created on the baseline's own transaction, so a failed first boot leaves the database empty. The boot restore also treats a lone migration history that records nothing as no database, which a restore clears anyway; any other answer keeps it, so the check fails closed. Database-Postgres says both.
This commit is contained in:
@@ -243,7 +243,7 @@ async function dumpPluginSchemas(source: string, toc: string, binDir: string | u
|
||||
}
|
||||
|
||||
/** The tables in the current schema of the database at `url`, read before TREK opens it. */
|
||||
export async function pgTableNames(url: string, binDir: string | undefined): Promise<string[]> {
|
||||
async function pgTableNames(url: string, binDir: string | undefined): Promise<string[]> {
|
||||
const out = await run(
|
||||
'psql',
|
||||
binDir,
|
||||
@@ -253,6 +253,30 @@ export async function pgTableNames(url: string, binDir: string | undefined): Pro
|
||||
return out.split('\n').filter((line) => line !== '');
|
||||
}
|
||||
|
||||
/** The migration history's table, the first one a boot creates. */
|
||||
const HISTORY_TABLE = 'mikro_orm_migrations';
|
||||
|
||||
/**
|
||||
* The tables that make the current schema of the database at `url` a
|
||||
* database in use, read before TREK opens it: every table, except a
|
||||
* migration history that is the only table and records nothing. A first boot
|
||||
* that failed while it created the schema left such a table behind before
|
||||
* the bootstrap created it in the schema's own transaction, and it holds
|
||||
* nothing a restore, which clears the schema first, could lose. Any answer
|
||||
* but a count of 0 keeps the table, so the check fails closed.
|
||||
*/
|
||||
export async function pgTablesInUse(url: string, binDir: string | undefined): Promise<string[]> {
|
||||
const tables = await pgTableNames(url, binDir);
|
||||
if (tables.length !== 1 || tables[0] !== HISTORY_TABLE) return tables;
|
||||
const out = await run(
|
||||
'psql',
|
||||
binDir,
|
||||
['--no-psqlrc', '--no-align', '--tuples-only', `--command=select count(*) as history_rows from ${HISTORY_TABLE}`],
|
||||
pgConnectionEnv(url),
|
||||
);
|
||||
return out.trim() === '0' ? [] : tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* The migration names in a restore script's COPY block for
|
||||
* `mikro_orm_migrations`, read from its `name` column; none when the script
|
||||
|
||||
Reference in New Issue
Block a user