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 stay-linked booking on Postgres in the public stay check
Maurice4a3ad562a737
diff --git a/server/scripts/pg-probe/helper-cases.ts b/server/scripts/pg-probe/helper-cases.tsindex 92b5b7936..44e49ef46 100644--- a/server/scripts/pg-probe/helper-cases.ts+++ b/server/scripts/pg-probe/helper-cases.ts@@ -19,6 +19,7 @@ import {castIntegerKysely,concatKysely,currentTimestampKysely,+ integerTextEqualsKysely,nowPlusSecondsKysely,startsWithIsoDateKysely,substringKysely,@@ -355,6 +356,26 @@ export const HELPER_CASES: readonly HelperCase[] = [kysely: (p, eb) => castIntegerKysely(p, eb, 'a'),expect: { equals: 14 },},+ ...['14', '14.0'].map(+ (stored): HelperCase => ({+ name: `integerTextEqualsKysely matches the text ${stored} to 14`,+ row: { a: stored, n: 14 },+ kysely: (p, eb) => eb.case().when(integerTextEqualsKysely(p, eb, 'a', 'n')).then('yes').else('no').end(),+ expect: { equals: 'yes' },+ }),+ ),+ {+ name: 'integerTextEqualsKysely matches the number 14 to 14',+ row: { n: 14 },+ kysely: (p, eb) => eb.case().when(integerTextEqualsKysely(p, eb, 'n', 'n')).then('yes').else('no').end(),+ expect: { equals: 'yes' },+ },+ {+ name: 'integerTextEqualsKysely tells 15.0 from 14',+ row: { a: '15.0', n: 14 },+ kysely: (p, eb) => eb.case().when(integerTextEqualsKysely(p, eb, 'a', 'n')).then('yes').else('no').end(),+ expect: { equals: 'no' },+ },{name: 'unixEpochToIsoKysely',row: { n: EPOCH_MS },diff --git a/server/src/db/dialect/kysely-functions.ts b/server/src/db/dialect/kysely-functions.tsindex 1ad527a63..59636ccd3 100644--- a/server/src/db/dialect/kysely-functions.ts+++ b/server/src/db/dialect/kysely-functions.ts@@ -167,6 +167,23 @@ export function castIntegerKysely<DB, TB extends keyof DB>(return unsupported(platform);}+/**+ * `<text ref> = <integer ref>` for a TEXT column that holds an integer id,+ * written either bare or the way the services store it (`'14'`, `'14.0'`):+ * the text goes through {@link castIntegerKysely}, so both engines match+ * `'14'`, `'14.0'` and a stored 14 against the id 14 alike. A bare cast to+ * integer would do on SQLite, but Postgres refuses `CAST('14.0' AS integer)`+ * (22P02), and a bare text-to-integer comparison (42883).+ */+export function integerTextEqualsKysely<DB, TB extends keyof DB>(+ platform: Platform,+ eb: ExpressionBuilder<DB, TB>,+ text: ReferenceExpression<DB, TB>,+ integer: StringReference<DB, TB>,+): ExpressionWrapper<DB, TB, SqlBool> {+ return eb(castIntegerKysely(platform, eb, text), '=', eb.ref(integer).$castTo<number>());+}+// ---------------------------------------------------------------------------// Plan 3g Task 0 (R1) — no consumer yet: Task 1 (`getJourneyFull`'s gallery// read) and Task 3 (`getPublicJourney`'s gallery read) each fold this intodiff --git a/server/src/db/repositories/Reservations.repository.ts b/server/src/db/repositories/Reservations.repository.tsindex 00ca948ac..e3f6b865b 100644--- a/server/src/db/repositories/Reservations.repository.ts+++ b/server/src/db/repositories/Reservations.repository.ts@@ -350,7 +350,9 @@ type FlightsMissingEndpointsKyselyDB = Pick<DB, 'reservations' | 'reservation_enexport class ReservationsRepository extends TrekRepository<Reservations> {// For its Wallet pass (_shared/reservation-pass-context.ts).- findPassContext(id: number, tripId: number) { return passContextQuery(this.kysely<PassContextDB>(), id, tripId); }+ findPassContext(id: number, tripId: number) {+ return passContextQuery(this.kysely<PassContextDB>(), this.getEntityManager().getPlatform(), id, tripId);+ }private joinedQuery() {return joinedReservationQuery(this.kysely<ReservationJoinDB>(), this.getEntityManager().getPlatform());@@ -928,7 +930,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {const rows = await this.kysely<CalendarStayKyselyDB>().selectFrom('day_accommodations as a').where('a.trip_id', '=', trip_id as number)- .where((eb) => publicStayExists(eb))+ .where((eb) => publicStayExists(this.getEntityManager().getPlatform(), eb)).leftJoin('days as sd', 'sd.id', 'a.start_day_id').leftJoin('days as ed', 'ed.id', 'a.end_day_id').leftJoin('places as p', 'p.id', 'a.place_id')@@ -1164,7 +1166,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {const rows = await this.kysely<SharePublicAccommodationKyselyDB>().selectFrom('day_accommodations as a').where('a.trip_id', '=', trip_id as number)- .where((eb) => publicStayExists(eb))+ .where((eb) => publicStayExists(this.getEntityManager().getPlatform(), eb)).innerJoin('places as p', 'p.id', 'a.place_id').select(['a.id', 'a.trip_id', 'a.place_id', 'a.start_day_id', 'a.end_day_id',@@ -1185,7 +1187,7 @@ export class ReservationsRepository extends TrekRepository<Reservations> {const rows = await this.kysely<SharePublicAccommodationKyselyDB>().selectFrom('day_accommodations as a').where('a.trip_id', '=', trip_id as number)- .where((eb) => publicStayExists(eb))+ .where((eb) => publicStayExists(this.getEntityManager().getPlatform(), eb)).select('a.place_id').distinct().execute();diff --git a/server/src/db/repositories/_shared/reservation-accommodation.ts b/server/src/db/repositories/_shared/reservation-accommodation.tsindex d6d5aee45..863ce5a07 100644--- a/server/src/db/repositories/_shared/reservation-accommodation.ts+++ b/server/src/db/repositories/_shared/reservation-accommodation.ts@@ -1,4 +1,4 @@-import { castIntegerKysely } from '../../dialect/kysely-functions';+import { integerTextEqualsKysely } from '../../dialect/kysely-functions';import type { Platform } from '@mikro-orm/core';import type { ExpressionBuilder, ExpressionWrapper, SqlBool, StringReference } from 'kysely';@@ -7,10 +7,11 @@ import type { ExpressionBuilder, ExpressionWrapper, SqlBool, StringReference } f* The join condition between a reservation and the stay it is booked against.** `reservations.accommodation_id` is TEXT with no foreign key, while- * `day_accommodations.id` is INTEGER, so the comparison is written as- * `CAST(<reservation ref> AS INTEGER) = <stay ref>`. SQLite coerces a bare- * text-to-integer comparison and Postgres refuses it (42883), so the cast is- * needed on both engines. A stored `'14.0'` casts to 14 on both.+ * `day_accommodations.id` is INTEGER, and the services store the id as+ * `'14.0'` (`legacyBoundIntegerText`). The dialect helper+ * `integerTextEqualsKysely` compares the two so that `'14'`, `'14.0'` and 14+ * match the stay 14 on both engines. Every join and correlation on this pair+ * goes through here, so the comparison has one spelling.*/export function accommodationIdMatches<DB, TB extends keyof DB>(platform: Platform,@@ -18,5 +19,5 @@ export function accommodationIdMatches<DB, TB extends keyof DB>(reservationAccommodationId: StringReference<DB, TB>,stayId: StringReference<DB, TB>,): ExpressionWrapper<DB, TB, SqlBool> {- return eb(castIntegerKysely(platform, eb, reservationAccommodationId), '=', eb.ref(stayId).$castTo<number>());+ return integerTextEqualsKysely(platform, eb, reservationAccommodationId, stayId);}diff --git a/server/src/db/repositories/_shared/reservation-pass-context.ts b/server/src/db/repositories/_shared/reservation-pass-context.tsindex 05da299e4..eb7fb53c5 100644--- a/server/src/db/repositories/_shared/reservation-pass-context.ts+++ b/server/src/db/repositories/_shared/reservation-pass-context.ts@@ -1,4 +1,6 @@import type { DB } from '../../kysely/db';+import { accommodationIdMatches } from './reservation-accommodation';+import type { Platform } from '@mikro-orm/core';import type { Kysely } from 'kysely';@@ -21,6 +23,7 @@ export interface ReservationPassContext {*/export async function passContextQuery(kysely: Kysely<PassContextDB>,+ platform: Platform,id: number,trip_id: number,): Promise<ReservationPassContext | undefined> {@@ -28,9 +31,8 @@ export async function passContextQuery(.selectFrom('reservations as r').innerJoin('trips as t', 't.id', 'r.trip_id').leftJoin('places as p', 'p.id', 'r.place_id')- // accommodation_id is TEXT; compared as a number so Postgres takes it as well as SQLite ("14" and "14.0" alike)..leftJoin('day_accommodations as ap', (join) =>- join.on((eb) => eb(eb.cast<number>('r.accommodation_id', 'numeric'), '=', eb.ref('ap.id'))),+ join.on((eb) => accommodationIdMatches(platform, eb, 'r.accommodation_id', 'ap.id')),).leftJoin('places as acc_p', 'acc_p.id', 'ap.place_id').where('r.id', '=', id)diff --git a/server/src/db/repositories/_shared/reservation-visibility.ts b/server/src/db/repositories/_shared/reservation-visibility.tsindex f57dc75c6..0e4c4652e 100644--- a/server/src/db/repositories/_shared/reservation-visibility.ts+++ b/server/src/db/repositories/_shared/reservation-visibility.ts@@ -1,4 +1,5 @@import { coalesceParam } from '../../dialect/sql-functions';+import { accommodationIdMatches } from './reservation-accommodation';import type { DB } from '../../kysely/db';import type { Platform } from '@mikro-orm/core';@@ -137,8 +138,10 @@ export type ReservationVisibilityKyselyDB = Pick<DB, 'day_accommodations' | 'res/*** RV2 as a Kysely predicate: `(NOT EXISTS (…) OR EXISTS (… AND <RV1>))`,- * correlated on `CAST(vr.accommodation_id AS INTEGER) = a.id`. Call inside- * a `.where((eb) => publicStayExists(eb))` on a query already+ * correlated through `accommodationIdMatches` (the stored `'14.0'` matches+ * the stay 14 on both engines; Postgres refuses a bare cast of it to+ * integer). Call inside a `.where((eb) => publicStayExists(platform, eb))`+ * on a query already* `.selectFrom('day_accommodations as a')` against a `Kysely<DB>` whose* `DB` satisfies `ReservationVisibilityKyselyDB`. The parameter type is* fixed to exactly the shape `.selectFrom('day_accommodations as a')`@@ -151,11 +154,14 @@ export type ReservationVisibilityKyselyDB = Pick<DB, 'day_accommodations' | 'res* structurally"); this function accepts exactly one contract instead,* proven end-to-end by this file's own parity-harness test.*/-export function publicStayExists(eb: ExpressionBuilder<ReservationVisibilityKyselyDB & { a: { id: number } }, 'a'>) {+export function publicStayExists(+ platform: Platform,+ eb: ExpressionBuilder<ReservationVisibilityKyselyDB & { a: { id: number } }, 'a'>,+) {const linkedTo = eb.selectFrom('reservations as vr').select('vr.id')- .where((eb2) => eb2(eb2.cast(eb2.ref('vr.accommodation_id'), 'integer'), '=', eb2.ref('a.id')));+ .where((eb2) => accommodationIdMatches(platform, eb2, 'vr.accommodation_id', 'a.id'));return eb.or([eb.not(eb.exists(linkedTo)),diff --git a/server/tests/unit/db/dialect/sql-functions.postgres.test.ts b/server/tests/unit/db/dialect/sql-functions.postgres.test.tsindex f48bfa8c1..da25604f6 100644--- a/server/tests/unit/db/dialect/sql-functions.postgres.test.ts+++ b/server/tests/unit/db/dialect/sql-functions.postgres.test.ts@@ -2,6 +2,7 @@ import {castIntegerKysely,concatKysely,currentTimestampKysely,+ integerTextEqualsKysely,nowPlusSecondsKysely,startsWithIsoDateKysely,substringKysely,@@ -230,6 +231,22 @@ describe('kysely-functions (postgres)', () => {expect(compiled.parameters).toEqual([]);});+ it("SQLFPG-026: integerTextEqualsKysely compares the truncated text with the integer, so '14.0' meets 14", () => {+ const compiled = db+ .selectFrom('users')+ .select('id')+ .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"',+ );+ expect(compiled.parameters).toEqual([]);+ class FakePlatform extends Platform {}+ expect(() =>+ integerTextEqualsKysely(new FakePlatform(), expressionBuilder<ProbeDB, 'users'>(), 'username', 'id'),+ ).toThrow(/no implementation for platform FakePlatform/);+ });+it('SQLFPG-023: unixEpochToIsoKysely renders UTC ISO text from epoch milliseconds', () => {const compiled = db.selectFrom('users')diff --git a/server/tests/unit/db/repositories/_shared/reservation-visibility.test.ts b/server/tests/unit/db/repositories/_shared/reservation-visibility.test.tsindex 0ee44163a..a251a4952 100644--- a/server/tests/unit/db/repositories/_shared/reservation-visibility.test.ts+++ b/server/tests/unit/db/repositories/_shared/reservation-visibility.test.ts@@ -234,7 +234,7 @@ describe('reservation-visibility parity (RV2: publicStayExists vs publicStaySql).selectFrom('day_accommodations as a').select('a.id').where('a.trip_id', '=', trip.id)- .where((eb) => publicStayExists(eb))+ .where((eb) => publicStayExists(t.em.getPlatform(), eb)).orderBy('a.id', 'asc').execute();const legacy = testDb@@ -260,7 +260,7 @@ describe('reservation-visibility parity (RV2: publicStayExists vs publicStaySql).selectFrom('day_accommodations as a').select('a.id').where('a.trip_id', '=', trip.id)- .where((eb) => publicStayExists(eb))+ .where((eb) => publicStayExists(t.em.getPlatform(), eb)).execute();const legacy = testDb// test-sql-allow: the legacy string fragment is this parity harness's oracle and has to run as written.@@ -290,7 +290,7 @@ describe('reservation-visibility parity (RV2: publicStayExists vs publicStaySql).selectFrom('day_accommodations as a').select('a.id').where('a.trip_id', '=', trip.id)- .where((eb) => publicStayExists(eb))+ .where((eb) => publicStayExists(t.em.getPlatform(), eb)).execute();const legacy = testDb// test-sql-allow: the legacy string fragment is this parity harness's oracle and has to run as written.@@ -322,7 +322,7 @@ describe('reservation-visibility parity (RV2: publicStayExists vs publicStaySql).selectFrom('day_accommodations as a').select('a.id').where('a.trip_id', '=', trip.id)- .where((eb) => publicStayExists(eb))+ .where((eb) => publicStayExists(t.em.getPlatform(), eb)).execute();const legacy = testDb// test-sql-allow: the legacy string fragment is this parity harness's oracle and has to run as written.@@ -351,7 +351,7 @@ describe('reservation-visibility parity (RV2: publicStayExists vs publicStaySql).selectFrom('day_accommodations as a').select('a.id').where('a.trip_id', '=', trip.id)- .where((eb) => publicStayExists(eb))+ .where((eb) => publicStayExists(t.em.getPlatform(), eb)).execute();const legacy = testDb// test-sql-allow: the legacy string fragment is this parity harness's oracle and has to run as written.@@ -386,7 +386,7 @@ describe('reservation-visibility parity (RV2: publicStayExists vs publicStaySql).selectFrom('day_accommodations as a').select('a.id').where('a.trip_id', '=', trip.id)- .where((eb) => publicStayExists(eb))+ .where((eb) => publicStayExists(t.em.getPlatform(), eb)).orderBy('a.id', 'asc').execute();const legacy = testDb@@ -427,7 +427,7 @@ describe('reservation-visibility parity (RV2: publicStayExists vs publicStaySql).selectFrom('day_accommodations as a').select('a.id').where('a.trip_id', '=', trip.id)- .where((eb) => publicStayExists(eb))+ .where((eb) => publicStayExists(t.em.getPlatform(), eb)).execute();const legacy = testDb// test-sql-allow: the legacy string fragment is this parity harness's oracle and has to run as written.@@ -458,7 +458,7 @@ describe('reservation-visibility parity (RV2: publicStayExists vs publicStaySql).selectFrom('day_accommodations as a').select('a.id').where('a.trip_id', '=', trip.id)- .where((eb) => publicStayExists(eb))+ .where((eb) => publicStayExists(t.em.getPlatform(), eb)).execute();const legacy = testDb// test-sql-allow: the legacy string fragment is this parity harness's oracle and has to run as written.