mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +00:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user