Skip to content

fix(commissions): repair auto-approve SQL — failing for every tenant in prod - #63

Open
keithfawcett wants to merge 2 commits into
mainfrom
fix/commission-auto-approve-sql
Open

fix(commissions): repair auto-approve SQL — failing for every tenant in prod#63
keithfawcett wants to merge 2 commits into
mainfrom
fix/commission-auto-approve-sql

Conversation

@keithfawcett

Copy link
Copy Markdown
Contributor

Active prod incident

The nightly commission-auto-approve job (15 5 * * *) has been failing for every tenant with:

invalid reference to FROM-clause entry for table "c"

Confirmed in prod doctl run logs (05:15 UTC block, all tenant IDs ok: false). The UPDATE "Commission" c ... FROM "Attribution" a ... LEFT JOIN "PartnerCommission" pc ON pc."partnerId" = c."partnerId" references the UPDATE target table c inside a FROM-clause join, which Postgres rejects.

Impact: matured accrued commissions never flip to approved, 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 PartnerCommission join on a."partnerId" instead of c."partnerId". Attribution (a) is already a legal FROM entry; a commission's partnerId equals its attribution's partnerId by construction, and c."attributionId" = a.id already pairs the rows — so this is equivalent, and the only legal form. PartnerCommission.partnerId is the PK (one row per partner), so no program scoping / row multiplication concern.

Tests

New commission-auto-approve.test.ts runs 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 migrate is 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 logs ok: true.

🤖 Generated with Claude Code

keithfawcett and others added 2 commits August 7, 2026 22:11
…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>
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