Skip to content

feat(db): Repair money written in old units during the micro-USD rollout - #4041

Merged
BYK merged 2 commits into
mainfrom
byk/money-micro-usd-repair-2
Nov 22, 2026
Merged

BYK merged 2 commits into
mainfrom
byk/money-micro-usd-repair-2

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

#3621 moved money to integer micro-USD. While its migrations (0119, 0120) rolled out, the previous runtime still wrote money in the old units, and nothing converted those rows afterwards:

  • cost_usd metric rows, which every cost read now skips. The usage cost of those Conversations, and the cost of their Automation runs, came from one runtime and missed the other runtime's rows.
  • Brief versions with only cost_usd set.
  • Stats counters added to the legacy count column, including cache warming dollar counters under their old names and units.

Migration 0160 converts the old cost rows to cost_micro_usd. When both runtimes wrote a cost row for the same Run, the later row wins. It then sums the usage cost of each affected Conversation again and copies it to their Automation runs. It also fills cost_micro_usd on those Brief versions and moves count amounts into value, using the micro-USD names for cache warming counters. Each statement touches only old-unit rows, so running it a second time changes nothing.

The metrics table is large and has no index on cost_usd. So the cost repair is one statement with data-modifying CTEs: it scans the table once, and finds the other rows of each affected Conversation by primary key.

#3615 is a different repair: it fixed Automation run cost and last run after the 0117 rollout.

via @BYK.

--

View Session [Sentry]

While 0119 and 0120 rolled out, the runtime before micro-USD still wrote
cost metric rows, Brief costs, and stats counters in the old units. This
migration converts those rows and sums the usage cost of the affected
Conversations and their Automation runs again. Each statement is
idempotent.

The metrics table has no index on the old metric name, so the migration
scans it once into a temporary table before it locks any row.

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot requested a review from BYK November 22, 2026 20:50
@vercel

vercel Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 22, 2026 9:03pm UTC

Request Review

@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 22, 2026
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

4 screenshot changes — 4 changed · 0 added · 0 removed

Review screenshots in Frameshift

Inbox Palette Pages · Desktop
Inbox Palette Pages · Desktop
Changed
Lead Home · Desktop
Lead Home · Desktop
Changed
Lead Home · Mobile
Lead Home · Mobile
Changed
System Cache Health Stale · Desktop
System Cache Health Stale · Desktop
Changed

…able

The migration test fixture runs each statement outside a transaction, so an
ON COMMIT DROP table is gone before the next statement reads it. One
statement with data-modifying CTEs scans the metrics table once and needs
no session state.
@BYK
BYK merged commit bb2c3fa into main Nov 22, 2026
57 checks passed
@BYK
BYK deleted the byk/money-micro-usd-repair-2 branch November 22, 2026 21:38

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 81d920cf Deployed Nov 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant