|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#32851] A field published `datetime` → `date` keeps its `timestamptz` |
| 5 | + * column on Postgres, and the `date` path read that column in two zones. |
| 6 | + * |
| 7 | + * Measured on live Postgres 16 before the fix, server at `Asia/Shanghai`, |
| 8 | + * process at `America/New_York` (the `Temporal Conformance (live PG + MySQL)` |
| 9 | + * job's pair): |
| 10 | + * |
| 11 | + * - `create({ due: '2026-11-12' })` answered, and read back, `2026-11-11`: |
| 12 | + * the bare day was stored as midnight in the SERVER's zone |
| 13 | + * (`2026-11-11T16:00:00Z`) and read back by its UTC components; |
| 14 | + * - `due = '2026-11-12'` matched that row (the comparand was cast in the |
| 15 | + * session zone too) and missed the row written as the instant |
| 16 | + * `2026-11-12T20:00:00Z` while the field was a `datetime`, which READ |
| 17 | + * `2026-11-12`; |
| 18 | + * - the day bucket put the second row on `2026-11-13` (`::date` in the |
| 19 | + * session zone). |
| 20 | + * |
| 21 | + * The fix reads every door in UTC, the zone the read already used: the write |
| 22 | + * binds the day's UTC midnight, and the filter and the bucket read the column |
| 23 | + * as its UTC calendar day. See `date-on-instant-column.ts`. |
| 24 | + * |
| 25 | + * Every cell writes and reads the same rows: SQLite and MySQL never had the |
| 26 | + * defect (SQLite stores text; MySQL's session is UTC and `DATETIME` is |
| 27 | + * zone-naive) and must still answer the same day. The row written while the |
| 28 | + * field was a `datetime` is asserted through the filter and the bucket on |
| 29 | + * Postgres only: on SQLite and MySQL a bare-day comparand does not match a |
| 30 | + * stored instant with a time of day, the same on the pre-fix tree, and that is |
| 31 | + * not what this card changes. |
| 32 | + */ |
| 33 | + |
| 34 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 35 | +import { SqlDriver } from './sql-driver.js'; |
| 36 | +import { DIALECT_CELLS, declareDialectCell, type DialectCell } from './live-dialect-matrix.testkit.js'; |
| 37 | + |
| 38 | +const MATRIX = 'date on an instant column (#32851)'; |
| 39 | +const TABLE = 'pub32851_due'; |
| 40 | + |
| 41 | +/** The live table as it was created: `due` a `datetime`. */ |
| 42 | +const V1 = { name: TABLE, fields: { label: { type: 'text' }, due: { type: 'datetime' }, seen: { type: 'datetime' } } }; |
| 43 | +/** The published change: `due` becomes a `date`; `seen` stays a `datetime`. */ |
| 44 | +const V2 = { name: TABLE, fields: { label: { type: 'text' }, due: { type: 'date' }, seen: { type: 'datetime' } } }; |
| 45 | + |
| 46 | +/** 20:00Z is 04:00 the NEXT day at `Asia/Shanghai`: a session-zone day discriminates. */ |
| 47 | +const DATETIME_ERA = '2026-11-12T20:00:00.000Z'; |
| 48 | +const DAY = '2026-11-12'; |
| 49 | +const SEEN = '2026-11-12T23:30:00.000Z'; |
| 50 | + |
| 51 | +const rowsOf = (res: unknown): Record<string, unknown>[] => |
| 52 | + (Array.isArray(res) ? res : ((res as { rows?: unknown[] })?.rows ?? [])) as Record<string, unknown>[]; |
| 53 | + |
| 54 | +function measure(cell: DialectCell): void { |
| 55 | + describe(`a \`datetime\` field published as \`date\` — ${cell.label} (#32851)`, () => { |
| 56 | + const pg = cell.id === 'pg'; |
| 57 | + let driver: SqlDriver; |
| 58 | + let columnType = ''; |
| 59 | + let serverOffsetSeconds = 0; |
| 60 | + const ids: Record<'era' | 'created' | 'updated', string> = { era: '', created: '', updated: '' }; |
| 61 | + let createdAnswer: Record<string, unknown> = {}; |
| 62 | + |
| 63 | + const labelsOf = (rows: readonly Record<string, unknown>[]) => rows.map((r) => String(r.label)).sort(); |
| 64 | + |
| 65 | + // Live: a connect, the two syncs and the publish's column half (#14213). |
| 66 | + beforeAll(async () => { |
| 67 | + driver = new SqlDriver(cell.config()); |
| 68 | + await driver.connect(); |
| 69 | + await driver.getKnex().schema.dropTableIfExists(TABLE); |
| 70 | + |
| 71 | + await driver.syncSchema(TABLE, V1); |
| 72 | + ids.era = String((await driver.create(TABLE, { label: 'era', due: DATETIME_ERA })).id); |
| 73 | + ids.updated = String((await driver.create(TABLE, { label: 'updated', due: DATETIME_ERA })).id); |
| 74 | + |
| 75 | + // The publish: its storage sync, then the column half it asks for. |
| 76 | + await driver.syncSchema(TABLE, V2); |
| 77 | + await driver.applyPublishedColumnChanges(V2); |
| 78 | + |
| 79 | + columnType = String(((await driver.getKnex()(TABLE).columnInfo()) as any).due.type).toLowerCase(); |
| 80 | + if (pg) { |
| 81 | + const res = await driver.execute(`select extract(timezone from now())::int as off`); |
| 82 | + serverOffsetSeconds = Number(rowsOf(res)[0]?.off); |
| 83 | + } |
| 84 | + |
| 85 | + createdAnswer = (await driver.create(TABLE, { label: 'created', due: DAY, seen: SEEN })) as Record<string, unknown>; |
| 86 | + ids.created = String(createdAnswer.id); |
| 87 | + await driver.update(TABLE, ids.updated, { due: DAY }); |
| 88 | + }, 60_000); |
| 89 | + |
| 90 | + afterAll(async () => { |
| 91 | + await driver?.getKnex().schema.dropTableIfExists(TABLE).catch(() => {}); |
| 92 | + await driver?.disconnect().catch(() => {}); |
| 93 | + }, 60_000); |
| 94 | + |
| 95 | + it.runIf(pg)('the fixture is real: the column is still `timestamptz` and the server zone is ahead of UTC', () => { |
| 96 | + expect(columnType).toBe('timestamp with time zone'); |
| 97 | + // The defect needs a session zone east of UTC; CI points the server at |
| 98 | + // `Asia/Shanghai`. A server at UTC would pass every assertion below on |
| 99 | + // the pre-fix tree. |
| 100 | + expect(serverOffsetSeconds, 'point the Postgres server at a zone ahead of UTC (e.g. Asia/Shanghai)').toBeGreaterThan(0); |
| 101 | + }); |
| 102 | + |
| 103 | + it('a day written after the publish is answered and read back as that day', async () => { |
| 104 | + expect(createdAnswer.due).toBe(DAY); |
| 105 | + const created: any = await driver.findOne(TABLE, { where: { id: ids.created } }); |
| 106 | + expect(created.due).toBe(DAY); |
| 107 | + const updated: any = await driver.findOne(TABLE, { where: { id: ids.updated } }); |
| 108 | + expect(updated.due).toBe(DAY); |
| 109 | + }); |
| 110 | + |
| 111 | + it('a filter by that day finds every row written as that day', async () => { |
| 112 | + const eq = await driver.find(TABLE, { where: { due: DAY } }); |
| 113 | + expect(labelsOf(eq as any[])).toEqual(expect.arrayContaining(['created', 'updated'])); |
| 114 | + const range = await driver.find(TABLE, { where: { due: { $gte: DAY, $lte: DAY } } }); |
| 115 | + expect(labelsOf(range as any[])).toEqual(expect.arrayContaining(['created', 'updated'])); |
| 116 | + const before = await driver.find(TABLE, { where: { due: { $lt: DAY } } }); |
| 117 | + expect(labelsOf(before as any[])).not.toContain('created'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('a `datetime` field beside it still reads and filters as its instant', async () => { |
| 121 | + const row: any = await driver.findOne(TABLE, { where: { id: ids.created } }); |
| 122 | + expect(row.seen).toBe(SEEN); |
| 123 | + const hit = await driver.find(TABLE, { where: { seen: SEEN } }); |
| 124 | + expect(labelsOf(hit as any[])).toEqual(['created']); |
| 125 | + }); |
| 126 | + |
| 127 | + it.runIf(pg)('the row written as a `datetime` reads, filters and buckets as one day', async () => { |
| 128 | + const era: any = await driver.findOne(TABLE, { where: { id: ids.era } }); |
| 129 | + expect(era.due).toBe(DAY); |
| 130 | + |
| 131 | + const eq = await driver.find(TABLE, { where: { due: DAY } }); |
| 132 | + expect(labelsOf(eq as any[])).toEqual(['created', 'era', 'updated']); |
| 133 | + |
| 134 | + const grouped = await driver.aggregate(TABLE, { |
| 135 | + groupBy: [{ field: 'due', dateGranularity: 'day' }], |
| 136 | + aggregations: [{ function: 'count', alias: 'n' }], |
| 137 | + }); |
| 138 | + expect((grouped as any[]).map((g) => [g.due, Number(g.n)])).toEqual([[DAY, 3]]); |
| 139 | + }); |
| 140 | + |
| 141 | + it.runIf(pg)('the outside-the-builder doors (analytics native SQL) answer the same day', async () => { |
| 142 | + const column = driver.temporalFilterColumnSql(TABLE, 'due', '"due"'); |
| 143 | + const comparand = driver.temporalFilterValue(TABLE, 'due', DAY); |
| 144 | + const hit = rowsOf(await driver.execute(`select label from "${TABLE}" where ${column} = ? order by label`, [comparand])); |
| 145 | + expect(hit.map((r) => r.label)).toEqual(['created', 'era', 'updated']); |
| 146 | + |
| 147 | + const expr = driver.dateBucketSql(TABLE, 'due', 'day'); |
| 148 | + const echoed = rowsOf(await driver.execute(`select ${expr} as b, count(*) as n from "${TABLE}" group by ${expr}`)); |
| 149 | + expect(echoed.map((r) => [r.b, Number(r.n)])).toEqual([[DAY, 3]]); |
| 150 | + }); |
| 151 | + }); |
| 152 | +} |
| 153 | + |
| 154 | +for (const cell of DIALECT_CELLS) { |
| 155 | + declareDialectCell(cell, MATRIX, measure); |
| 156 | +} |
0 commit comments