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
@@ -23,6 +23,8 @@ interface TargetTable {
name: string;
columns: TargetColumn[];
primaryKey: string[];
/** Every unique index (the primary key's among them) over plain columns, by name. */
uniqueKeys: Array<{ name: string; columns: string[] }>;
}
export interface TargetSchema {
@@ -57,9 +59,24 @@ export async function readTargetSchema(trx: Trx): Promise<TargetSchema> {
"and pg_get_serial_sequence(format('%I', table_name), column_name) is not null order by table_name, column_name",
);
// A unique constraint is backed by an index of its name, so the index names what a refusal reports.
const uniqueIndexes = await run<{ table_name: string; index_name: string; columns: string[] }>(
trx,
'select t.relname as table_name, i.relname as index_name, array_agg(a.attname::text order by k.ord) as columns ' +
'from pg_index x join pg_class i on i.oid = x.indexrelid join pg_class t on t.oid = x.indrelid ' +
'cross join lateral unnest(x.indkey) with ordinality as k(attnum, ord) ' +
'join pg_attribute a on a.attrelid = x.indrelid and a.attnum = k.attnum ' +
'where x.indisunique and t.relnamespace = current_schema()::regnamespace group by t.relname, i.relname',
);
const tables = new Map<string, TargetTable>();
for (const column of columns) {
const table = tables.get(column.table_name) ?? { name: column.table_name, columns: [], primaryKey: [] };
const table = tables.get(column.table_name) ?? {
name: column.table_name,
columns: [],
primaryKey: [],
uniqueKeys: [],
};
table.columns.push({
name: column.column_name,
dataType: column.data_type,
@@ -67,6 +84,9 @@ export async function readTargetSchema(trx: Trx): Promise<TargetSchema> {
});
tables.set(column.table_name, table);
}
for (const index of uniqueIndexes) {
tables.get(index.table_name)?.uniqueKeys.push({ name: index.index_name, columns: index.columns });
}
const foreignKeys: ForeignKey[] = [];
for (const constraint of constraints) {
if (constraint.kind === 'p') {
@@ -154,6 +174,8 @@ export function planTrekTables(target: TargetSchema, source: SourceTables): Trek
columns,
deferred,
primaryKey: table.primaryKey,
exact: false,
uniqueKeys: table.uniqueKeys,
});
}
const sequences = target.sequences