fix(db): let the move to Postgres change nothing silently

The move stripped NUL from a plugin's own text, merging keys that were
distinct, and turned a plugin's values into another type (text in a BLOB
column into bytea, numbers in an untyped column into text). Its checksum
was built from the converted values, so both passed as verified.

A plugin's data now moves exactly as the Postgres plugin store would
hand it back, or not at all: a NUL, a value of another storage class
than its column, or an integer past 2^53 refuses the move before
anything is written, listing plugin, table, row and column. A column
left open by SQLite's affinity takes the kind its values need, so text
in a BLOB column stays text.

TREK's own text holding a NUL from an older install is refused by
default too. --drop-nul drops it, lists every changed value in the
summary, and names a unique key that dropping a NUL broke.

Every table's checksum is now built from the values read from SQLite,
so a conversion that changes a value fails the verification.
This commit is contained in:
Maurice
2026-11-17 23:28:01 +01:00
parent 6040b60e86
commit bc6728678b
10 changed files with 655 additions and 97 deletions
@@ -14,12 +14,18 @@ import path from 'node:path';
* The command around the move (`src/move-to-postgres.ts` runs it): reads
* TREK_DB_URL as the server does, takes the SQLite file from the data
* directory or `--from=<file>`, the plugin data from the plugins' data
* directory, runs the move and prints its summary. Answers the exit code.
* directory, runs the move and prints its summary. `--drop-nul` lets the move
* drop NUL characters from TREK's own text instead of refusing them. Answers
* the exit code.
*/
export const USAGE =
'Usage: move-to-postgres [--from=<SQLite file>]\n' +
'Copies a TREK SQLite database (data/travel.db by default) into the empty Postgres database TREK_DB_URL names.';
'Usage: move-to-postgres [--from=<SQLite file>] [--drop-nul]\n' +
'Copies a TREK SQLite database (data/travel.db by default) into the empty Postgres database TREK_DB_URL names.\n' +
' --from=<file> the SQLite file to copy\n' +
" --drop-nul drop NUL characters (U+0000) from TREK's own text, which Postgres cannot hold and TREK drops on\n" +
' its next write anyway, instead of refusing the move; the summary lists every value changed.\n' +
" A plugin's own data is never changed: a NUL there refuses the move either way.";
export interface MoveCommandIo {
env: () => AppEnv;
@@ -51,13 +57,14 @@ const defaultIo: MoveCommandIo = {
};
const FROM = '--from=';
const DROP_NUL = '--drop-nul';
async function run(argv: readonly string[], io: MoveCommandIo): Promise<number> {
if (argv.includes('--help') || argv.includes('-h')) {
io.out(USAGE);
return 0;
}
const unknown = argv.find((arg) => !arg.startsWith(FROM));
const unknown = argv.find((arg) => !arg.startsWith(FROM) && arg !== DROP_NUL);
if (unknown !== undefined) throw new MoveRefusal(`Unknown argument ${unknown}.\n${USAGE}`);
const env = io.env();
if (env.db.engine !== 'postgres')
@@ -77,6 +84,7 @@ async function run(argv: readonly string[], io: MoveCommandIo): Promise<number>
known: knownMigrationNames(),
baseline: readPostgresBaseline,
log: (line) => io.out(` ${line}`),
dropNul: argv.includes(DROP_NUL),
});
io.out('');
io.out(renderSummary(summary, target));