fix(db): keep booking to stay links of old installs when moving to Postgres

An install from before April 2026 stores reservations.accommodation_id as an INTEGER, so the move now writes such a stay id as the '<id>.0' text TREK compares it as, instead of '14', which no longer matched the stay on Postgres.
This commit is contained in:
Maurice
2026-11-23 14:57:25 +01:00
parent e51902a03b
commit 6a6c403860
3 changed files with 55 additions and 1 deletions
@@ -1,10 +1,12 @@
import type { SqliteSource } from '../../sqlite/sqlite-source';
import { FIX_IN_PLUGIN_DB, FIX_IN_SQLITE_FILE, MoveRefusal } from './refusal';
import {
asLegacyIdText,
beyondSafeInteger,
canonicalFromPostgres,
canonicalFromSource,
convertValue,
holdsLegacyIdText,
NUL_REASON,
PLUGIN_RULES,
TableChecksum,
@@ -326,8 +328,11 @@ export async function copyTable(
return `row ${spec.primaryKey.map((name, index) => `${name}=${key(index)}`).join(', ')}`;
};
const legacyIdText = spec.columns.map((column) => !spec.exact && holdsLegacyIdText(spec.source, column.name));
/** One SQLite row as the values sent to Postgres, or null once a value was refused. */
const convertRow = (raw: unknown[]): PostgresValue[] | null => {
const convertRow = (read: unknown[]): PostgresValue[] | null => {
const raw = read.map((value, index) => (legacyIdText[index] ? asLegacyIdText(value as SqliteValue) : value));
result.rows++;
const values: PostgresValue[] = [];
spec.columns.forEach((column, index) => {
@@ -318,6 +318,27 @@ export function convertValue(value: SqliteValue, kind: ColumnKind, rules: Conver
}
}
/**
* Columns TREK's code compares in the `'<id>.0'` text its services write
* (`legacyBoundIntegerText`). An install that got
* `reservations.accommodation_id` from legacy step 34 has it as INTEGER, so
* SQLite stored that text as the number and still matched it; the Postgres
* column is text and matches only the text.
*/
const LEGACY_ID_TEXT_COLUMNS = new Set(['reservations.accommodation_id']);
/** Whether `table.column` of TREK's own schema holds ids in the `'<id>.0'` text. */
export function holdsLegacyIdText(table: string, column: string): boolean {
return LEGACY_ID_TEXT_COLUMNS.has(`${table}.${column}`);
}
/** A whole number stored in such a column, as the `'<id>.0'` text it was written as; anything else as it is. */
export function asLegacyIdText(value: SqliteValue): SqliteValue {
if (typeof value === 'bigint') return `${value}.0`;
if (typeof value === 'number' && Number.isInteger(value)) return `${value}.0`;
return value;
}
/** Whether an integer value read back would be beyond what TREK's Postgres driver reads (2^53). */
export function beyondSafeInteger(value: PostgresValue): boolean {
if (typeof value !== 'string' || !DECIMAL_INTEGER.test(value)) return false;
@@ -358,6 +358,34 @@ describe('moveSqliteToPostgres', () => {
expect(pg.sequences.get('public.trips_id_seq')).toEqual({ value: '9007199254740993', called: true });
expect(summary.warnings).toEqual(['trips holds 1 integer(s) beyond 2^53, which TREK refuses to read on Postgres.']);
});
it("MOVE-038: a stay id an old install keeps as a number moves as the '<id>.0' text TREK compares it as", async () => {
pg = new FakePostgres([
...SCHEMA,
{
name: 'reservations',
columns: [
{ name: 'id', dataType: 'bigint', nullable: false },
{ name: 'accommodation_id', dataType: 'text' },
],
primaryKey: ['id'],
serial: ['id'],
},
]);
orm = fakeMoveOrm(pg, { known: [M1, M2] });
// The column as legacy step 34 added it: INTEGER, so the '14.0' the services write is stored as 14.
sqlite(
path.join(dir, 'travel.db'),
`create table reservations (id integer primary key, accommodation_id integer);
insert into reservations values (1, '14.0'), (2, null), (3, 'abc');`,
);
await moveSqliteToPostgres(options());
expect(pg.rowsOf('"reservations"')).toEqual([
['1', '14.0'],
['2', null],
['3', 'abc'],
]);
});
});
describe('nothing is changed silently', () => {