Skip to content

Commit 65590a8

Browse files
fix(store): date projection formats the naive UTC column without a zone conversion (CL-8452) (#25)
1 parent f4d9c62 commit 65590a8

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/native-store.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "./native-store.js";
99
import { seedScope, withTestDb } from "./test-helpers.js";
1010
import type { MailboxDb } from "./db.js";
11+
import { sql } from "drizzle-orm";
1112

1213
const TENANT_ID = "tenant-native";
1314
const PRINCIPAL_ID = "principal-native";
@@ -182,6 +183,8 @@ describe("native MailboxStore over the principal mailbox tables", () => {
182183
const writeInstant = new Date();
183184
const uid = inbox.append(new Uint8Array([1]), envelope({ date: writeInstant }), []);
184185
await inbox.settled;
186+
// A host's session zone is rarely UTC; the read must not depend on it.
187+
await db.execute(sql`SET TIME ZONE 'America/Los_Angeles'`);
185188

186189
const reopened = await openNativeMailboxStore(db, {
187190
tenantId: TENANT_ID,

src/native-store.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,13 @@ export async function openNativeMailboxStore(
145145
// driver returns every timestamp column as raw text (its type parser is
146146
// disabled for those OIDs), and a bare value would come back ambiguous — so
147147
// format it as an explicit UTC instant here rather than trust the caller's
148-
// (or postgres session's) local timezone to reinterpret it. Never cast
149-
// inside WHERE — that would break the index (see schema.ts).
148+
// (or postgres session's) local timezone to reinterpret it. The column is
149+
// a naive UTC value, so no AT TIME ZONE: that would re-render it in the
150+
// session zone. Never cast inside WHERE — that would break the index.
150151
const rows = await db.execute<Row>(sql`
151152
SELECT "id", "uid", "modseq", "flags", "raw", "subject", "from_address",
152153
"message_id", "in_reply_to", "references", "to_addresses",
153-
to_char("created_at" AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') AS "created_at"
154+
to_char("created_at", 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"') AS "created_at"
154155
FROM "mailbox"."principal_mail"
155156
WHERE "tenant_id" = ${tenantId} AND "principal_id" = ${principalId} AND "folder" = ${folder}
156157
ORDER BY "uid" ASC

0 commit comments

Comments
 (0)