feat(migrations): cleanup funnel-duplicate roster leftovers (chat#1859)
What
The cleanup-delete — second of the two backfill follow-ups on chat#1859. Completes the deletion that database#43 deliberately deferred ("Duplicate accounts are NOT deleted — orphaning them is enough; deletion is a follow-up").
Adds 20260708170500_cleanup_funnel_duplicate_roster_leftovers.sql, which removes the two leftovers the repoint migrations cannot:
- Step 1 — redundant roster rows (Category B). When the account already held the canonical (api#768 claim-attach or a prior link), the repoint skipped the duplicate row to avoid a second
(account, canonical)link — leaving a redundant row pointing at the song-less duplicate that no re-run touches. Deletes those. - Step 2 — orphaned duplicate artist accounts. Every repoint leaves the duplicate account behind (song-less, catalog-less, still carrying its scraped Spotify social, polluting artist search). Deletes the fully-orphaned ones.
Scope (measured 2026-07-08 on prod, read-only)
- Step 1 redundant rows removed: 5 (the Category B leftovers).
- Step 2 orphaned dupe accounts deleted: 16 today, plus the 5 that Step 1 orphans and the 2 that the pass-2 repoint orphans → the full accumulated set of orphaned funnel dupes (the original 14 from #43 + the stragglers). The migration self-computes the set; the counts here are the current read-only preview.
Safety (destructive — tightly guarded, idempotent)
- Step 1 removes a roster row only when the same account also holds the canonical twin — so it is never an account's only link (Category A rows, where the duplicate is the sole link, are untouched here; the pass-2 repoint fixes those).
- Step 2 deletes an artist account only when it is a pure funnel artifact: zero
song_artists(never a canonical or any real artist), zeroaccount_catalogs, not inartist_organization_ids, carries a Spotify social, has exactly one song-owning same-name twin, and is fully orphaned (zeroaccount_artist_idsrefs). Deletion cascades only its own scrapedaccount_socials. - Post-condition
RAISE EXCEPTIONif any redundant (dupe + canonical on the same account) row remains. Re-running finds nothing.
Verification
- Read-only preview against prod confirms the exact target sets above (5 rows / 16+ accounts); no row with
song_artists,account_catalogs, or an org link is in scope. - The Supabase Preview check validates it parses + runs against a branch.
- Acceptance: after the pass-2 repoint and this migration run, the #1859 health-check query returns 0 rows — no funnel-duplicate roster artist referenced, and no orphaned funnel-dupe account left.
Merge order
Merge pass-2 repoint (#44) first, then this. This file's timestamp is later so they apply repoint → cleanup; merging in that order keeps the migration history in sequence. Running this before the repoint is safe but leaves Category A unfixed (that's the repoint's job).
Links
- Tracking: recoupable/chat#1859
- Prior: recoupable/database#43 (
20260707190000, the deferred-deletion note) · #44 (pass-2 repoint)
🤖 Generated with Claude Code
Summary by cubic
Adds 20260708170500_cleanup_funnel_duplicate_roster_leftovers.sql to remove redundant roster rows and delete fully orphaned funnel-duplicate artist accounts left by the repoint, completing cleanup for chat#1859.
- Migration
- Deletes redundant
account_artist_idswhere the account already links to the canonical twin. - Deletes duplicate artist accounts only if: no
song_artists, noaccount_catalogs, no org link, has a Spotify social, exactly one same‑name song‑owning twin, and no roster refs; cascades only its ownaccount_socials. - Runs after the pass‑2 repoint; idempotent with a post‑condition check that raises if any redundant rows remain.
- Deletes redundant