mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
fix(db): refuse a database or backup a newer TREK migrated, and keep a copy before a restore
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.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { knownMigrationNames, unknownMigrations } from '../../../src/db/known-migrations';
|
||||
import { refuseNewerDatabase } from '../../../src/db/legacy-baseline';
|
||||
|
||||
describe('known migrations', () => {
|
||||
it('KNOWNMIG-001: lists every migration this build ships, by its recorded name', () => {
|
||||
const known = knownMigrationNames();
|
||||
expect(known.has('Migration20200101000000_baseline_schema')).toBe(true);
|
||||
expect(known.has('Migration20200101042000_tours')).toBe(true);
|
||||
expect(known.size).toBe(fs.readdirSync(path.join(__dirname, '../../../src/db/migrations')).length);
|
||||
});
|
||||
|
||||
it('KNOWNMIG-002: reads compiled files too, and ignores maps, declarations and strangers', () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'trek-known-migrations-'));
|
||||
try {
|
||||
for (const file of ['Migration20200101000000_a.js', 'Migration20200101000000_a.js.map', 'Migration20200101000100_b.d.ts', 'README.md']) {
|
||||
fs.writeFileSync(path.join(dir, file), '');
|
||||
}
|
||||
expect([...knownMigrationNames(dir)]).toEqual(['Migration20200101000000_a']);
|
||||
} finally {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('KNOWNMIG-003: names only what this build does not ship, and refuses on it', () => {
|
||||
const known = new Set(['Migration1_a', 'Migration2_b']);
|
||||
expect(unknownMigrations(['Migration1_a', 'Migration3_c'], known)).toEqual(['Migration3_c']);
|
||||
expect(() => refuseNewerDatabase(['Migration1_a', 'Migration2_b'], known)).not.toThrow();
|
||||
expect(() => refuseNewerDatabase(['Migration1_a', 'Migration3_c'], known)).toThrow(/newer TREK/);
|
||||
});
|
||||
});
|
||||
@@ -225,4 +225,17 @@ describe('planLegacyBaseline / migrateToHead', () => {
|
||||
expect(planned.at(-1)).toBe('Migration20200101041100_half_company_holidays_0_5_covers_the');
|
||||
expect(await recorded()).toEqual([]);
|
||||
});
|
||||
|
||||
it('LEGACYBASE-008: refuses a database a newer TREK migrated, before applying anything', async () => {
|
||||
await migrate();
|
||||
await rawExec(orm, "INSERT INTO mikro_orm_migrations (name, executed_at) VALUES ('Migration20990101000000_from_a_newer_release', CURRENT_TIMESTAMP)");
|
||||
const before = await recorded();
|
||||
await expect(migrate()).rejects.toThrow(/Refusing to boot: the database was migrated by a newer TREK \(1 unknown migration\(s\), latest Migration20990101000000_from_a_newer_release\)/);
|
||||
expect(await recorded()).toEqual(before);
|
||||
});
|
||||
|
||||
it('LEGACYBASE-009: a database at head of this build boots again without complaint', async () => {
|
||||
await migrate();
|
||||
await expect(migrate()).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user