fix(db): find no nearest day for a date the calendar does not have

The nearest-day fallback of a booking's time cast the bound date to a
timestamp on Postgres, which refuses 2024-02-30 with a 500, while SQLite's
JULIANDAY rolled it over into March and placed the booking there.
dayDistance now refuses anything but a calendar date on both engines
(isCalendarDate), so a caller that forgets the check fails on SQLite too,
and findNearestDayId answers no day for such a date. The size baseline of
Reservations.repository.ts drops by one line.
This commit is contained in:
Maurice
2026-11-17 21:23:31 +01:00
parent 1b216d8f48
commit 143e2ae447
5 changed files with 63 additions and 6 deletions
@@ -27,6 +27,7 @@ import {
dateOf,
dayDistance,
foundAgainState,
isCalendarDate,
lower,
lowerParam,
lowerTrim,
@@ -48,6 +49,7 @@ import { createJourney, createJourneyEntry } from '../../../helpers/factories';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { Platform } from '@mikro-orm/core';
import { BasePostgreSqlPlatform, SqlitePlatform } from '@mikro-orm/sql';
import { expressionBuilder, type ExpressionBuilder } from 'kysely';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
@@ -982,6 +984,26 @@ describe('sql-functions (sqlite)', () => {
expect(() => dayDistance(foreign, 'u.date', '2026-01-01')).toThrow(/no implementation for platform FakePlatform/);
});
it('SQLF-100: dayDistance refuses a date the calendar does not have, on every engine, which SQLite would roll over', () => {
expect(isCalendarDate('2024-02-29')).toBe(true);
expect(isCalendarDate('2026-12-31')).toBe(true);
for (const text of [
'2024-02-30',
'2023-02-29',
'2024-13-01',
'2024-00-10',
'2024-04-31',
'2024-1-01',
'2024-01-01T10:00',
'',
]) {
expect(isCalendarDate(text), text).toBe(false);
}
for (const platform of [new SqlitePlatform(), new BasePostgreSqlPlatform()]) {
expect(() => dayDistance(platform, 'u.created_at', '2024-02-30')).toThrow(/needs a calendar date/);
}
});
// Plan 3d Task 0 review (H1/L1): the five helpers above return a MikroORM
// RawQueryFragment, which stringifies to a literal `?` token and throws at
// execution when handed into a Kysely statement — verified directly below