Repository navigation
Fix subscription deploys silently failing to record footer usage - #376
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Summary
Solid, well-scoped bug fix. recordCredentialUsage interpolated raw JS Date objects into sql templates; on the subscription upsert path (windowExpired / the CASE WHEN window update) those Dates reach the postgres-js driver unmapped and throw, and since usage telemetry is best-effort the throw was swallowed — so subscription credentials never got a footer meter. Binding the timestamps as ISO strings cast to ::timestamp keeps them off the rejected Date-binding path.
Correctness
I traced the timestamp round-trip and it's consistent, which is the thing that matters most here:
window_started_at/last_used_ataretimestampwithout time zone (schema.ts:513,525).- Drizzle's
insert().values({ windowStartedAt: now })serializes the Date via the column mapper (toISOString()), storing the UTC wall-clock. - The new
${nowIso}::timestamp/${windowFloorIso}::timestampproduce the same UTC wall-clock, because Postgres ignores the trailingZwhen casting a string totimestampwithout tz. So thesql-built values and the Drizzle-mapped insert values land identically, and thewindowExpiredcomparison against the stored column is apples-to-apples. - On read,
getRecentCredentialUsageandsubscriptionUsagereconstruct these as UTC (Drizzle appends+0000), matching the write side. No skew introduced.
I checked for the same raw-Date-in-sql pattern elsewhere in src/server and found no other occurrences — this was an isolated bug.
Tests
The integration test is the right fix for the coverage gap: the existing unit test (credential-usage.test.ts) mocks the db chain, so the SQL never hits a driver, which is exactly why the bug shipped. The three cases (metered badge, subscription upsert increment to runs === 2, and metered deploy not spending a subscription's window budget) each map cleanly onto the code paths and derived subscriptionUsage output. Good coverage of the record → read seam.
Note: I did not run the integration suite in this review environment (it needs a throwaway Postgres via DATABASE_URL); the assessment above is by code reasoning, and the test itself is designed to reproduce the original failure against a real driver.
Minor / non-blocking
- The new test imports everything via top-level
await import(...), while the siblingsession.integration.test.tsuses ordinary static imports and the same harness. If there's a load-order reason (e.g. deferring aserver-onlyimport), a one-line comment would help; otherwise static imports would be more consistent. - The fix depends on a subtle invariant (Drizzle serializing these columns via
toISOString()+ Postgres discarding theZon a::timestampcast). It's correct and the code comment documents it well — just flagging that it's load-bearing.
LGTM.
| // concurrent deploys can't race a read-then-write: the started-at and count | ||
| // both branch on whether the stored window has elapsed. | ||
| const windowExpired = sql`${credentialUsage.windowStartedAt} < ${windowFloor}`; | ||
| const windowExpired = sql`${credentialUsage.windowStartedAt} < ${windowFloorIso}::timestamp`; |
There was a problem hiding this comment.
Correct: window_started_at is timestamp without time zone, so casting an ISO string to ::timestamp discards the trailing Z and stores the UTC wall-clock — identical to what Drizzle's insert().values({ windowStartedAt: now }) writes via the column mapper. The comparison against the stored column is consistent, and no timezone skew is introduced across the write/read round-trip.
| // away — recordCredentialUsage builds the subscription window update as raw | ||
| // `sql`, whose Date binding only fails against a live driver, so the mocked | ||
| // tests passed while every subscription deploy threw in production. | ||
| const { accountRouter } = await import("~/server/api/routers/account"); |
There was a problem hiding this comment.
Any reason these use top-level await import(...) rather than the plain static imports the sibling session.integration.test.ts uses with the same harness? If it's to defer a server-only/env-time import, a one-line note would help; otherwise static imports would be more consistent. Non-blocking.
48c2aa3 to
7a0c17e
Compare
recordCredentialUsage built the subscription rolling-window update as a
raw `sql` template with JS Date objects interpolated (windowFloor, now).
Drizzle hands an unmapped Date straight to postgres-js, which rejects it
("The string argument must be of type string ... Received an instance of
Date"), so every subscription deploy's upsert threw. create-job treats
usage telemetry as best-effort and swallows the error, so subscription
credentials never got recorded and their footer badges never appeared.
The unit tests mocked the DB, so the broken SQL went unnoticed.
Bind the window timestamps as ISO strings cast to `::timestamp` — the
same UTC wall-clock Drizzle already stores for the Date-valued columns —
keeping the update off the Date-binding path. Metered (api_key) deploys
were unaffected (their branch emits no raw sql) and still work.
Adds a real-Postgres integration test covering the record → read seam the
unit tests mock away: metered, subscription upsert increment, and the
metered-doesn't-spend-a-subscription-window case.
Signed-off-by: based64god <6633471+based64god@users.noreply.github.com>
7a0c17e to
54b0576
Compare
recordCredentialUsagebuilt the subscription window update as a rawsqltemplate with interpolated JSDateobjects, which postgres-js rejects, causing every subscription deploy's usage upsert to throw and get silently swallowed — so footer usage badges never appeared for subscription credentials.Changes
credential-usage.tsas ISO strings cast to::timestampinstead of rawDateobjects, keeping the update off the rejected Date-binding path while storing the same UTC wall-clock