The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
fix(db): match a booking to its stay on Postgres without failing on odd ids
Maurice3a2645a3e49b
diff --git a/server/scripts/pg-probe/helper-cases.ts b/server/scripts/pg-probe/helper-cases.tsindex 44e49ef46..fc52f8298 100644--- a/server/scripts/pg-probe/helper-cases.ts+++ b/server/scripts/pg-probe/helper-cases.ts@@ -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 },diff --git a/server/src/db/dialect/kysely-functions.ts b/server/src/db/dialect/kysely-functions.tsindex 59636ccd3..e5c8a9533 100644--- a/server/src/db/dialect/kysely-functions.ts+++ b/server/src/db/dialect/kysely-functions.ts@@ -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);}diff --git a/server/tests/unit/db/dialect/sql-functions.postgres.test.ts b/server/tests/unit/db/dialect/sql-functions.postgres.test.tsindex da25604f6..9b7a9f55a 100644--- a/server/tests/unit/db/dialect/sql-functions.postgres.test.ts+++ b/server/tests/unit/db/dialect/sql-functions.postgres.test.ts@@ -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'),