diff --git a/server/src/db/engine/postgres/sqlite-move/copy.ts b/server/src/db/engine/postgres/sqlite-move/copy.ts index b34d0edbb..eedf5f0f4 100644 --- a/server/src/db/engine/postgres/sqlite-move/copy.ts +++ b/server/src/db/engine/postgres/sqlite-move/copy.ts @@ -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) => { diff --git a/server/src/db/engine/postgres/sqlite-move/values.ts b/server/src/db/engine/postgres/sqlite-move/values.ts index 61498ba21..783ff1d28 100644 --- a/server/src/db/engine/postgres/sqlite-move/values.ts +++ b/server/src/db/engine/postgres/sqlite-move/values.ts @@ -318,6 +318,27 @@ export function convertValue(value: SqliteValue, kind: ColumnKind, rules: Conver } } +/** + * Columns TREK's code compares in the `'.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 `'.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 `'.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; diff --git a/server/tests/unit/db/engine/postgres/sqlite-move/move.test.ts b/server/tests/unit/db/engine/postgres/sqlite-move/move.test.ts index 40ff0d042..3af48c8fe 100644 --- a/server/tests/unit/db/engine/postgres/sqlite-move/move.test.ts +++ b/server/tests/unit/db/engine/postgres/sqlite-move/move.test.ts @@ -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 '.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', () => {