fix(db): say what a schema difference means when the move to Postgres refuses it

The refusal listed the differences and nothing else. A table or column
only one side has now comes with what that means: the file is at this
version's level, the start that brought it there added every column it
could, the start log names the rest, and the operator keeps running on
SQLite and reports it. A column of TREK's own schema the move cannot copy
is listed apart and named a bug in TREK, since the file is not at fault.
The server guide describes the gap, the repair, the check and the refusal.
This commit is contained in:
Maurice
2026-11-18 15:22:37 +01:00
parent 771dab7c35
commit 85b773420c
5 changed files with 108 additions and 19 deletions
@@ -1,5 +1,5 @@
/**
* MOVE-001..032: the move of a SQLite install into Postgres
* MOVE-001..037: the move of a SQLite install into Postgres
* (src/db/engine/postgres/sqlite-move/), over real SQLite files and a
* stand-in for Postgres that keeps what it is sent (tests/helpers/fake-postgres-move.ts):
* the refusals before anything is written, the copy in foreign key order with
@@ -26,6 +26,9 @@ import {
migrationLevelProblem,
moveSqliteToPostgres,
renderSummary,
SCHEMA_BUG_REMEDY,
SCHEMA_DIFFERENCE_REMEDY,
schemaRefusal,
type MoveOptions,
type MoveSummary,
} from '../../../../../../src/db/engine/postgres/sqlite-move/move';
@@ -248,6 +251,44 @@ describe('moveSqliteToPostgres', () => {
);
});
it('MOVE-036: a SQLite file whose tables differ from the schema is refused with what that means and what to do', async () => {
sqlite(
path.join(dir, 'travel.db'),
'alter table trips drop column score; alter table users add column nickname text;',
);
const failure = moveSqliteToPostgres(options());
await expect(failure).rejects.toBeInstanceOf(MoveRefusal);
await expect(failure).rejects.toThrow(
"The SQLite file and this TREK's Postgres schema differ; nothing was written:\n" +
' - the SQLite column users.nickname has no Postgres column\n' +
' - the Postgres column trips.score has no SQLite column\n' +
SCHEMA_DIFFERENCE_REMEDY,
);
expect(SCHEMA_DIFFERENCE_REMEDY).toContain('[DB] The database lacks');
expect(SCHEMA_DIFFERENCE_REMEDY).toContain('keep running TREK on SQLite');
expect(pg.statements.some((sql) => sql.startsWith('insert into'))).toBe(false);
expect(pg.statements.at(-1)).toBe(CLEAR_SCHEMA_SQL);
});
it('MOVE-037: what the schema itself cannot copy is named apart, as a bug in TREK, not as the file lacking it', () => {
const typeProblem = 'the Postgres column trips.when has the type interval, which the move does not know';
expect(schemaRefusal([], [])).toBeNull();
expect(schemaRefusal([], [typeProblem])).toBe(
"This TREK's Postgres schema holds what the move cannot copy; nothing was written:\n" +
` - ${typeProblem}\n` +
SCHEMA_BUG_REMEDY,
);
expect(schemaRefusal(['the Postgres column trips.score has no SQLite column'], [typeProblem])).toBe(
"The SQLite file and this TREK's Postgres schema differ; nothing was written:\n" +
' - the Postgres column trips.score has no SQLite column\n' +
`${SCHEMA_DIFFERENCE_REMEDY}\n` +
"This TREK's Postgres schema holds what the move cannot copy; nothing was written:\n" +
` - ${typeProblem}\n` +
SCHEMA_BUG_REMEDY,
);
expect(SCHEMA_BUG_REMEDY).not.toContain('[DB] The database lacks');
});
it('MOVE-011: a plugin database goes into its schema with its migration record, ids and indexes', async () => {
const pluginDir = path.join(dir, 'plugins-data', 'move-notes');
fs.mkdirSync(pluginDir, { recursive: true });
@@ -101,6 +101,7 @@ describe('planTrekTables', () => {
['users', ['id', 'avatar']],
]),
);
expect(plan.differences).toEqual([]);
expect(plan.problems).toEqual([]);
expect(plan.specs.map((spec) => spec.target)).toEqual(['"messages"', '"users"']);
expect(plan.specs[0]).toMatchObject({
@@ -145,13 +146,16 @@ describe('planTrekTables', () => {
['leftover', ['id']],
]),
);
expect(plan.problems).toEqual([
expect(plan.differences).toEqual([
'the SQLite table leftover has no Postgres table',
'the Postgres table extra has no SQLite table',
'the SQLite column messages.legacy has no Postgres column',
'the Postgres column users.avatar has no SQLite column',
]);
// What the schema itself holds that the move cannot copy is kept apart from what the file lacks.
expect(plan.problems).toEqual([
'the Postgres column messages.sent_at has the type timestamp with time zone, which the move does not know',
'messages.reply_to refers back to a table filled later and cannot be NULL meanwhile',
'the Postgres column users.avatar has no SQLite column',
]);
});
});