mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
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:
@@ -135,22 +135,26 @@ interface TrekCopyPlan {
|
||||
/** The tables in the order they are filled. */
|
||||
specs: TableSpec[];
|
||||
sequences: CopiedSequence[];
|
||||
/** Why the two schemas do not match; the move refuses when there is any. */
|
||||
/** The tables and columns only one of the two has; the move refuses when there is any. */
|
||||
differences: string[];
|
||||
/** What this TREK's own schema holds that the move cannot copy (a bug in TREK); the move refuses on any. */
|
||||
problems: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Pairs every table of the target with the SQLite table of the same name and
|
||||
* puts them in foreign key order. Anything the two do not share, or a column
|
||||
* type the move has no rule for, is a problem: copying around it would lose
|
||||
* data or guess.
|
||||
* puts them in foreign key order. Anything the two do not share is a
|
||||
* difference, and a column type the move has no rule for, or a deferred
|
||||
* reference that cannot be NULL meanwhile, is a problem of the schema itself:
|
||||
* copying around either would lose data or guess.
|
||||
*/
|
||||
export function planTrekTables(target: TargetSchema, source: SourceTables): TrekCopyPlan {
|
||||
const differences: string[] = [];
|
||||
const problems: string[] = [];
|
||||
const tables = target.tables.filter((table) => table.name !== HISTORY_TABLE);
|
||||
const names = new Set(tables.map((table) => table.name));
|
||||
for (const name of source.keys()) {
|
||||
if (name !== HISTORY_TABLE && !names.has(name)) problems.push(`the SQLite table ${name} has no Postgres table`);
|
||||
if (name !== HISTORY_TABLE && !names.has(name)) differences.push(`the SQLite table ${name} has no Postgres table`);
|
||||
}
|
||||
const plan = planCopyOrder(
|
||||
tables.map((table) => table.name),
|
||||
@@ -163,18 +167,18 @@ export function planTrekTables(target: TargetSchema, source: SourceTables): Trek
|
||||
const sourceColumns = source.get(name);
|
||||
if (!table) continue;
|
||||
if (!sourceColumns) {
|
||||
problems.push(`the Postgres table ${name} has no SQLite table`);
|
||||
differences.push(`the Postgres table ${name} has no SQLite table`);
|
||||
continue;
|
||||
}
|
||||
for (const column of sourceColumns) {
|
||||
if (!table.columns.some((candidate) => candidate.name === column)) {
|
||||
problems.push(`the SQLite column ${name}.${column} has no Postgres column`);
|
||||
differences.push(`the SQLite column ${name}.${column} has no Postgres column`);
|
||||
}
|
||||
}
|
||||
const columns = table.columns.flatMap((column) => {
|
||||
const kind = columnKind(column.dataType);
|
||||
if (!sourceColumns.includes(column.name))
|
||||
problems.push(`the Postgres column ${name}.${column.name} has no SQLite column`);
|
||||
differences.push(`the Postgres column ${name}.${column.name} has no SQLite column`);
|
||||
else if (kind === null)
|
||||
problems.push(
|
||||
`the Postgres column ${name}.${column.name} has the type ${column.dataType}, which the move does not know`,
|
||||
@@ -202,5 +206,5 @@ export function planTrekTables(target: TargetSchema, source: SourceTables): Trek
|
||||
const sequences = target.sequences
|
||||
.filter((entry) => names.has(entry.table))
|
||||
.map((entry) => ({ ...entry, target: quoteIdentifier(entry.table) }));
|
||||
return { specs, sequences, problems };
|
||||
return { specs, sequences, differences, problems };
|
||||
}
|
||||
|
||||
@@ -109,6 +109,49 @@ const PLUGIN_DATA_REMEDY =
|
||||
"that type. Change those values in the plugin's plugin.db (through the plugin, or with the sqlite3 tool while " +
|
||||
'TREK is stopped), or uninstall the plugin and remove its data directory, then run the move again.';
|
||||
|
||||
/**
|
||||
* What a difference between the two schemas means and what the operator can
|
||||
* do, for a file already at this build's migration level (the level is
|
||||
* checked first, so the start that brought it there ran every migration,
|
||||
* including the one that adds the columns an old upgrade skipped).
|
||||
*/
|
||||
export const SCHEMA_DIFFERENCE_REMEDY =
|
||||
"The SQLite file is at this TREK version's migration level, so its tables should match this schema exactly. " +
|
||||
'A table or column only Postgres has is one the SQLite install never got: an install upgraded before 5.0 can ' +
|
||||
'have skipped some, and the start that brought the file to this level added every column it could; TREK names ' +
|
||||
'the rest in its start log ("[DB] The database lacks ..."). A table or column only the SQLite file has was ' +
|
||||
"added outside TREK's migrations, by hand or by a development build. TREK cannot correct either on its own: " +
|
||||
'keep running TREK on SQLite, which still works, and please report this message, with that line of the start ' +
|
||||
'log if there is one.';
|
||||
|
||||
/** What a column of this TREK's own Postgres schema that the move cannot copy means: the file is not at fault. */
|
||||
export const SCHEMA_BUG_REMEDY =
|
||||
"These come from this TREK version's own Postgres schema, not from the SQLite file, so this is a bug in TREK. " +
|
||||
'Keep running TREK on SQLite, which still works, and please report this message.';
|
||||
|
||||
/**
|
||||
* The refusal for a SQLite file whose tables do not pair with the schema, or
|
||||
* null when they do: the tables and columns only one side has, with what
|
||||
* that means, and apart from them what the schema itself holds that the move
|
||||
* cannot copy, which is TREK's own fault.
|
||||
*/
|
||||
export function schemaRefusal(differences: readonly string[], problems: readonly string[]): string | null {
|
||||
const parts: string[] = [];
|
||||
if (differences.length > 0) {
|
||||
parts.push(
|
||||
`The SQLite file and this TREK's Postgres schema differ; nothing was written:\n` +
|
||||
`${listLines(differences, differences.length)}\n${SCHEMA_DIFFERENCE_REMEDY}`,
|
||||
);
|
||||
}
|
||||
if (problems.length > 0) {
|
||||
parts.push(
|
||||
`This TREK's Postgres schema holds what the move cannot copy; nothing was written:\n` +
|
||||
`${listLines(problems, problems.length)}\n${SCHEMA_BUG_REMEDY}`,
|
||||
);
|
||||
}
|
||||
return parts.length > 0 ? parts.join('\n') : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Why the SQLite file is not at this build's migration level, or null when it
|
||||
* is: it must have recorded exactly the migrations this build ships.
|
||||
@@ -211,11 +254,8 @@ async function copyAll(
|
||||
source.tables().map((table) => [table, source.columns(table).map((column) => column.name)]),
|
||||
);
|
||||
const plan = planTrekTables(await readTargetSchema(trx), sourceTables);
|
||||
if (plan.problems.length > 0) {
|
||||
throw new MoveRefusal(
|
||||
`The SQLite file and this TREK's Postgres schema differ:\n${listLines(plan.problems, plan.problems.length)}`,
|
||||
);
|
||||
}
|
||||
const refusal = schemaRefusal(plan.differences, plan.problems);
|
||||
if (refusal) throw new MoveRefusal(refusal);
|
||||
// The bootstrap's own rows (default settings, addons, providers) give way to the file's.
|
||||
await run(trx, `truncate table ${plan.specs.map((spec) => spec.target).join(', ')}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user