fix(commissions): repair auto-approve SQL — failing for every tenant in prod - #63
Open
keithfawcett wants to merge 2 commits into
Open
fix(commissions): repair auto-approve SQL — failing for every tenant in prod#63keithfawcett wants to merge 2 commits into
keithfawcett wants to merge 2 commits into
Conversation
…ant in prod The nightly commission-auto-approve job has been throwing in prod for all tenants: `invalid reference to FROM-clause entry for table "c"`. The UPDATE...FROM referenced the update target `c` inside a FROM-clause LEFT JOIN (`pc."partnerId" = c."partnerId"`), which Postgres forbids. Net effect: matured accrued commissions never auto-approved, so they never progressed to payout — platform-wide, every night. Fix: key the PartnerCommission join on `a."partnerId"` (Attribution is already a legal FROM entry; a commission's partnerId equals its attribution's partnerId by construction, and c."attributionId" = a.id pairs them). No behavior change beyond the query now executing. Adds commission-auto-approve.test.ts (5 cases incl. partner-snapshot-holdback preference) that runs the real query against Postgres — there was no test exercising this SQL, which is how it reached prod. Discovered while checking prod logs (doctl) for an unrelated RLS finding. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Per review: also require c."partnerId" = a."partnerId" in the auto-approve UPDATE. It's equal by construction (a commission's partner comes from its attribution), but the DB doesn't constrain it, so this guards against any future data-integrity drift approving a mismatched row. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
keithfawcett
added a commit
that referenced
this pull request
Aug 9, 2026
Turns the handoff brief into a status record: what each item was, what actually shipped, and — the part that is still open — the staging exercises that have to pass before either money path is trusted. Item A (#10, PR #73): planner/executor split with a durable payout intent and a frozen commission set. Item B (#12, PR #75): the three funding races plus a live-Stripe backstop for missed refund/reversal webhooks. Item C (#8, PR #74): the three missing tables, per-table primary keys, a portable SQL dump, and two array round-trip bugs the test found. No code left on any of the three; the remaining work is the staging checklists in docs/direct-connect-payouts.md and section H of the funding staging runbook, plus the two post-merge prod actions for #62 and #63. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Active prod incident
The nightly
commission-auto-approvejob (15 5 * * *) has been failing for every tenant with:Confirmed in prod
doctlrun logs (05:15 UTC block, all tenant IDsok: false). TheUPDATE "Commission" c ... FROM "Attribution" a ... LEFT JOIN "PartnerCommission" pc ON pc."partnerId" = c."partnerId"references the UPDATE target tablecinside a FROM-clause join, which Postgres rejects.Impact: matured
accruedcommissions never flip toapproved, so they never reach payout — platform-wide, likely since the partner-snapshot-holdback join was introduced (~June 2026). There is probably a backlog of accrued commissions in prod that should already be approved.Fix
Key the
PartnerCommissionjoin ona."partnerId"instead ofc."partnerId".Attribution(a) is already a legal FROM entry; a commission'spartnerIdequals its attribution'spartnerIdby construction, andc."attributionId" = a.idalready pairs the rows — so this is equivalent, and the only legal form.PartnerCommission.partnerIdis the PK (one row per partner), so no program scoping / row multiplication concern.Tests
New
commission-auto-approve.test.tsruns the real query against Postgres (there was no test exercising this SQL — that's how it shipped): executes cleanly, approves matured commissions, leaves immature ones, prefers the partner-snapshotted holdback (exercises the join), and skips null/0-holdback programs. Typecheck clean.Deploy
After merge: run
pnpm migrateis not needed (no schema change), but run the job once (or wait for 05:15 UTC) to clear the accrued backlog, and confirm the next run logsok: true.🤖 Generated with Claude Code