feat(migrations): cleanup funnel-duplicate roster leftovers (chat#1859)

Merged
SW
5mo ago
+119-0

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), zero account_catalogs, not in artist_organization_ids, carries a Spotify social, has exactly one song-owning same-name twin, and is fully orphaned (zero account_artist_ids refs). Deletion cascades only its own scraped account_socials.
  • Post-condition RAISE EXCEPTION if 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

🤖 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_ids where the account already links to the canonical twin.
    • Deletes duplicate artist accounts only if: no song_artists, no account_catalogs, no org link, has a Spotify social, exactly one same‑name song‑owning twin, and no roster refs; cascades only its own account_socials.
    • Runs after the pass‑2 repoint; idempotent with a post‑condition check that raises if any redundant rows remain.