fix(db): match a booking to its stay on Postgres without failing on odd ids

The Postgres stay match now casts the leading digits of the stored text as bigint, the way SQLite reads it, so a non-numeric or oversized accommodation_id no longer fails the calendar, share and stay reads for every trip.
This commit is contained in:
Maurice
2026-11-23 14:56:17 +01:00
parent bf8677a28c
commit 3a2645a3e4
3 changed files with 35 additions and 10 deletions
+14
View File
@@ -370,6 +370,20 @@ export const HELPER_CASES: readonly HelperCase[] = [
kysely: (p, eb) => eb.case().when(integerTextEqualsKysely(p, eb, 'n', 'n')).then('yes').else('no').end(),
expect: { equals: 'yes' },
},
...['abc', '', '14abc'].map(
(stored): HelperCase => ({
name: `integerTextEqualsKysely reads the text ${JSON.stringify(stored)} by its leading digits without failing`,
row: { a: stored, n: 14 },
kysely: (p, eb) => eb.case().when(integerTextEqualsKysely(p, eb, 'a', 'n')).then('yes').else('no').end(),
expect: { equals: stored === '14abc' ? 'yes' : 'no' },
}),
),
{
name: 'integerTextEqualsKysely matches an id past 2^31',
row: { a: '3000000000.0', n: 3000000000 },
kysely: (p, eb) => eb.case().when(integerTextEqualsKysely(p, eb, 'a', 'n')).then('yes').else('no').end(),
expect: { equals: 'yes' },
},
{
name: 'integerTextEqualsKysely tells 15.0 from 14',
row: { a: '15.0', n: 14 },
+14 -5
View File
@@ -147,15 +147,21 @@ export function concatKysely<DB, TB extends keyof DB>(
return acc as ExpressionWrapper<DB, TB, string>;
}
/** The integer a text starts with, at most 18 digits so the `bigint` cast cannot overflow. */
const PG_LEADING_INTEGER = '^\\s*([-+]?[0-9]{1,18})';
/**
* The Kysely-expression twin of {@link castInteger}: `CAST(<ref> AS
* INTEGER)`, for `reservations.accommodation_id` (TEXT) compared against an
* INTEGER column inside a Kysely statement (RS20/RV2's join shape — a
* correlated `EXISTS`/scalar-subquery context the MikroORM QueryBuilder
* cannot express, per the inventory's own T6 ruling). Postgres refuses to
* cast the text `'14.0'` to an integer, so its branch goes through `numeric`
* and truncates, as SQLite's cast does: `CAST(trunc(CAST(<ref> AS numeric))
* AS integer)`.
* cannot express, per the inventory's own T6 ruling). SQLite's cast reads
* the integer the text starts with (`'14.0'` and `'14abc'` are 14) and never
* fails. Postgres refuses `CAST('14.0' AS integer)` and any text that is no
* number at all, so its branch casts the leading digits alone, as `bigint`
* like the ids it is compared with: `CAST(substring(CAST(<ref> AS text),
* '<leading integer>') AS bigint)`. Text without leading digits is NULL
* there, where SQLite reads 0; no row has the id 0, so neither matches.
*/
export function castIntegerKysely<DB, TB extends keyof DB>(
platform: Platform,
@@ -163,7 +169,10 @@ export function castIntegerKysely<DB, TB extends keyof DB>(
ref: ReferenceExpression<DB, TB>,
): ExpressionWrapper<DB, TB, number> {
if (isSqlite(platform)) return eb.cast<number>(ref, 'integer');
if (isPostgres(platform)) return eb.cast<number>(eb.fn('trunc', [eb.cast(ref, 'numeric')]), 'integer');
if (isPostgres(platform)) {
const leading = eb.fn<string>('substring', [eb.cast(ref, 'text'), eb.cast(eb.val(PG_LEADING_INTEGER), 'text')]);
return eb.cast<number>(leading, 'bigint');
}
return unsupported(platform);
}
@@ -222,13 +222,15 @@ describe('kysely-functions (postgres)', () => {
expect(sliced.parameters).toEqual([1, 10]);
});
it('SQLFPG-022: castIntegerKysely truncates through numeric', () => {
it('SQLFPG-022: castIntegerKysely casts the leading digits as bigint, so no text and no id past 2^31 can fail it', () => {
const compiled = db
.selectFrom('users')
.select((eb) => castIntegerKysely(pg, eb, 'display_name').as('v'))
.compile();
expect(compiled.sql).toBe('select cast(trunc(cast("display_name" as numeric)) as integer) as "v" from "users"');
expect(compiled.parameters).toEqual([]);
expect(compiled.sql).toBe(
'select cast(substring(cast("display_name" as text), cast($1 as text)) as bigint) as "v" from "users"',
);
expect(compiled.parameters).toEqual(['^\\s*([-+]?[0-9]{1,18})']);
});
it("SQLFPG-026: integerTextEqualsKysely compares the truncated text with the integer, so '14.0' meets 14", () => {
@@ -238,9 +240,9 @@ describe('kysely-functions (postgres)', () => {
.where((eb) => integerTextEqualsKysely(pg, eb, 'display_name', 'id'))
.compile();
expect(compiled.sql).toBe(
'select "id" from "users" where cast(trunc(cast("display_name" as numeric)) as integer) = "id"',
'select "id" from "users" where cast(substring(cast("display_name" as text), cast($1 as text)) as bigint) = "id"',
);
expect(compiled.parameters).toEqual([]);
expect(compiled.parameters).toEqual(['^\\s*([-+]?[0-9]{1,18})']);
class FakePlatform extends Platform {}
expect(() =>
integerTextEqualsKysely(new FakePlatform(), expressionBuilder<ProbeDB, 'users'>(), 'username', 'id'),