mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +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
|
||||
|
||||
@@ -84,9 +84,10 @@ async function currentTables(connection: Connection): Promise<string[]> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the schema in an empty Postgres database: the baseline's statements
|
||||
* and the record of every migration it stands for, in one transaction (DDL is
|
||||
* transactional on Postgres, so the database ends with all of it or none). A
|
||||
* Creates the schema in an empty Postgres database: the migration history's
|
||||
* table, the baseline's statements and the record of every migration it
|
||||
* stands for, in one transaction (DDL is transactional on Postgres, so the
|
||||
* database ends with all of it or none, not even an empty history table). A
|
||||
* database holding tables beside no TREK history is refused before anything is
|
||||
* written.
|
||||
*/
|
||||
@@ -104,12 +105,21 @@ async function createFromBaseline(
|
||||
'migration history. Point TREK_DB_URL at an empty database.',
|
||||
);
|
||||
}
|
||||
await storage.ensureTable();
|
||||
const schema = baseline();
|
||||
logger.log(`Creating the schema in an empty Postgres database (through ${migrationLabel(schema.through)})`);
|
||||
await connection.transactional(async (trx) => {
|
||||
await connection.execute(schema.statements.join(';\n'), [], 'run', trx);
|
||||
await Promise.all(schema.migrations.map((name) => storage.logMigration({ name }, trx)));
|
||||
// The history table is created inside the transaction too: MikroORM
|
||||
// creates it on the storage's master transaction, and without one it would
|
||||
// commit on its own, so a baseline that fails would leave it behind and a
|
||||
// later RESTORE_FROM_BACKUP would take the database for one in use.
|
||||
storage.setMasterMigration(trx);
|
||||
try {
|
||||
await storage.ensureTable();
|
||||
await connection.execute(schema.statements.join(';\n'), [], 'run', trx);
|
||||
await Promise.all(schema.migrations.map((name) => storage.logMigration({ name }, trx)));
|
||||
} finally {
|
||||
storage.unsetMasterMigration();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user