Skip to content

Fix subscription deploys silently failing to record footer usage - #376

Merged
based64god merged 1 commit into
mainfrom
bandolier/footer-usage-indicators-4t957f
Jul 18, 2026
Merged

based64god merged 1 commit into
mainfrom
bandolier/footer-usage-indicators-4t957f

Conversation

@based64god

Copy link
Copy Markdown
Owner

recordCredentialUsage built the subscription window update as a raw sql template with interpolated JS Date objects, 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

  • Bind the window timestamps in credential-usage.ts as ISO strings cast to ::timestamp instead of raw Date objects, keeping the update off the rejected Date-binding path while storing the same UTC wall-clock
  • Add an integration test against a real Postgres instance covering the record → read seam (metered deploy, subscription upsert increment, and metered deploys not spending a subscription's window budget) that the mocked unit tests were missing

@vercel

vercel Bot commented Jul 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
bandolier Building Building Preview, Comment Jul 18, 2026 4:35pm

Request Review

@bandolier-dev bandolier-dev Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_at are timestamp without 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}::timestamp produce the same UTC wall-clock, because Postgres ignores the trailing Z when casting a string to timestamp without tz. So the sql-built values and the Drizzle-mapped insert values land identically, and the windowExpired comparison against the stored column is apples-to-apples.
  • On read, getRecentCredentialUsage and subscriptionUsage reconstruct 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 sibling session.integration.test.ts uses ordinary static imports and the same harness. If there's a load-order reason (e.g. deferring a server-only import), 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 the Z on a ::timestamp cast). 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`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions
github-actions Bot force-pushed the bandolier/footer-usage-indicators-4t957f branch from 48c2aa3 to 7a0c17e Compare July 18, 2026 16:28
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>
@github-actions
github-actions Bot force-pushed the bandolier/footer-usage-indicators-4t957f branch from 7a0c17e to 54b0576 Compare July 18, 2026 16:35
@based64god
based64god enabled auto-merge July 18, 2026 16:36
@based64god
based64god merged commit 0b6a0a5 into main Jul 18, 2026
12 of 13 checks passed
@based64god
based64god deleted the bandolier/footer-usage-indicators-4t957f branch July 18, 2026 16:38

This branch had an error being deployed

1 failed deployment
production — 54b05760 Deployed Jul 18, 2026 by based64god via delete-branch #279
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant