mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-13 07:40:59 +00:00
71a0af02b0
Rolling an image back, or restoring a backup a newer release made, ran older code against a schema it does not know and failed later, far from the cause. Boot now refuses a database that records migrations this build does not ship, and the restore check refuses such a backup while the current database is still in place. A restore also keeps the database it replaces as data/pre-restore-<time>.db.
28 lines
1012 B
TypeScript
28 lines
1012 B
TypeScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
const MIGRATION_FILE = /^(Migration\d+_\w+)\.(?:js|ts)$/;
|
|
|
|
/**
|
|
* The migrations this build ships, by the name the migrator records in
|
|
* `mikro_orm_migrations`. Read from the directory the migrator itself reads:
|
|
* `src/db/migrations` under tsx and vitest, `dist/db/migrations` in a build.
|
|
*/
|
|
export function knownMigrationNames(dir = path.join(__dirname, 'migrations')): Set<string> {
|
|
const names = new Set<string>();
|
|
for (const entry of fs.readdirSync(dir)) {
|
|
const match = MIGRATION_FILE.exec(entry);
|
|
if (match) names.add(match[1]);
|
|
}
|
|
return names;
|
|
}
|
|
|
|
/**
|
|
* Recorded migrations this build does not ship: the database was migrated by
|
|
* a newer TREK. Running older code against it fails later and far from the
|
|
* cause, so the callers refuse instead.
|
|
*/
|
|
export function unknownMigrations(executed: Iterable<string>, known: Set<string> = knownMigrationNames()): string[] {
|
|
return [...executed].filter((name) => !known.has(name));
|
|
}
|