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
@@ -22,6 +22,11 @@ import path from 'node:path';
* here (foreign keys between its tables, views, triggers, partial or
* expression indexes, other defaults) is not created and is listed in the
* summary. The file itself stays where it is, untouched.
*
* The rows are the plugin's, so none is changed on the way: every value must
* come back from the Postgres store with the type and bytes it has in the
* file, or the move refuses before it writes anything and lists the value
* (`PLUGIN_RULES` in `values.ts`, checked by `checkPluginData` in `move.ts`).
*/
/** The plugin store's own record of the migrations it ran, created exactly as the store creates it. */
@@ -40,21 +45,27 @@ const PG_TYPE: Record<ColumnKind, string> = {
/**
* The kind of a plugin's column, by SQLite's affinity rules (the declared
* type's text decides, in this order: INT, then CHAR/CLOB/TEXT, then BLOB or
* none, then REAL/FLOA/DOUB, else NUMERIC). Where the affinity leaves the
* storage open (none, or NUMERIC), the values the column holds decide, and
* text is the answer that holds every one of them.
* none, then REAL/FLOA/DOUB, else NUMERIC). An INTEGER, TEXT or REAL affinity
* names the type the plugin asked for. Where the affinity leaves the storage
* open (BLOB or none, or NUMERIC), the values the column holds decide: the
* kind that hands every one of them back to the plugin as it reads it from
* SQLite (an INTEGER or a REAL as a number, TEXT as a string, a BLOB as a
* Buffer). A column whose values no single kind hands back like that still
* gets one, and the move refuses each value it would change
* (`PLUGIN_RULES` in `values.ts`); nothing is converted to fit.
*/
export function pluginColumnKind(declaredType: string, storage: ReadonlySet<string>): ColumnKind {
const declared = declaredType.toUpperCase();
const only = (...classes: string[]) => storage.size > 0 && [...storage].every((value) => classes.includes(value));
if (declared.includes('INT')) return 'integer';
if (/CHAR|CLOB|TEXT/.test(declared)) return 'text';
if (declared.includes('BLOB')) return only('blob', 'text') ? 'bytes' : 'text';
if (/REAL|FLOA|DOUB/.test(declared)) return only('integer', 'real') || storage.size === 0 ? 'float' : 'text';
const blob = declared.includes('BLOB') || declared.trim() === '';
if (!blob && /REAL|FLOA|DOUB/.test(declared)) return 'float';
if (only('integer')) return 'integer';
if (only('integer', 'real')) return 'float';
if (only('blob')) return 'bytes';
return 'text';
// Empty, or text, or a mix: a column declared BLOB with no values keeps bytes for what the plugin writes later.
return storage.size === 0 && declared.includes('BLOB') ? 'bytes' : 'text';
}
const NUMBER_LITERAL = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i;
@@ -143,6 +154,8 @@ export function planPluginData(pluginId: string, source: SqliteSource): PluginPl
],
deferred: [],
primaryKey: ['id'],
exact: true,
uniqueKeys: [],
});
continue;
}
@@ -168,6 +181,9 @@ export function planPluginData(pluginId: string, source: SqliteSource): PluginPl
columns: columns.map((column, index) => ({ name: column.name, kind: kinds[index] })),
deferred: [],
primaryKey: keys.map((key) => key.name),
// A plugin's values are never changed (`PLUGIN_RULES`), so no key of its tables can break on the way.
exact: true,
uniqueKeys: [],
});
// The identity's sequence is named by Postgres; resolveIdentitySequences reads it once the table exists.
if (rowid) plan.sequences.push({ table, target, column: rowid, sequence: '' });