Repository navigation
feat(migrations): cleanup funnel-duplicate roster leftovers (chat#1859) #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| -- Clean up funnel-duplicate roster leftovers: redundant rows + orphaned dupe | ||
| -- artists (recoupable/chat#1859; follow-up to 20260707190000 + 20260708170000). | ||
| -- | ||
| -- 20260707190000 repointed duplicates to the canonical ONLY when the account did | ||
| -- not already hold the canonical, and — by design — never deleted anything | ||
| -- ("orphaning them is enough; deletion is a follow-up"). Two leftovers remain, | ||
| -- neither fixable by re-running the repoint | ||
| -- (https://github.com/recoupable/chat/issues/1859#issuecomment-4916313589): | ||
| -- | ||
| -- A. REDUNDANT ROSTER ROWS (Category B). When the account already held the | ||
| -- canonical (via api#768 claim-attach or a prior link), the repoint SKIPPED | ||
| -- the duplicate row to avoid minting a second (account, canonical) link — so | ||
| -- the account keeps a redundant row pointing at the song-less duplicate that | ||
| -- the repoint will never touch. Measured 2026-07-08 on prod: 5 rows. | ||
| -- | ||
| -- B. ORPHANED DUPLICATE ARTIST ACCOUNTS. Every repoint (the original 14 + this | ||
| -- pass) leaves the duplicate account behind: song-less, catalog-less, still | ||
| -- carrying its scraped Spotify social, polluting artist search. Measured | ||
| -- 2026-07-08 on prod: 16 already orphaned, plus the 5 orphaned by step 1 | ||
| -- below and the 2 orphaned by the pass-2 repoint (20260708170000). | ||
| -- | ||
| -- This migration completes the deferred cleanup. It runs AFTER the pass-2 repoint | ||
| -- (20260708170000 < this file's timestamp), so Category A is already handled and | ||
| -- only Category B redundant rows remain to remove. | ||
| -- | ||
| -- Safety: destructive, so tightly guarded and 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 to that artist | ||
| -- (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 references). Deleting it cascades only its own scraped | ||
| -- account_socials. | ||
| -- Re-running finds nothing (rows/accounts already gone). | ||
|
|
||
| -- Duplicate -> canonical twin map (same 5-condition signature as 20260707190000). | ||
| CREATE TEMP TABLE cleanup_dupe_map ON COMMIT DROP AS | ||
| WITH dupes AS ( | ||
| SELECT a.id, a.name | ||
| FROM public.accounts a | ||
| WHERE EXISTS (SELECT 1 FROM public.account_artist_ids aai WHERE aai.artist_id = a.id) | ||
| AND NOT EXISTS (SELECT 1 FROM public.song_artists sa WHERE sa.artist = a.id) | ||
| AND EXISTS ( | ||
| SELECT 1 | ||
| FROM public.account_socials asoc | ||
| JOIN public.socials s ON s.id = asoc.social_id | ||
| WHERE asoc.account_id = a.id | ||
| AND s.profile_url ILIKE '%open.spotify.com/artist/%') | ||
| AND NOT EXISTS ( | ||
| SELECT 1 | ||
| FROM public.account_artist_ids aai | ||
| WHERE aai.artist_id = a.id | ||
| AND NOT EXISTS ( | ||
| SELECT 1 FROM public.account_catalogs ac WHERE ac.account = aai.account_id)) | ||
| ) | ||
| SELECT d.id AS dupe_id, d.name, (array_agg(c.id ORDER BY c.id))[1] AS canonical_id | ||
| FROM dupes d | ||
| JOIN public.accounts c | ||
| ON c.name = d.name | ||
| AND c.id <> d.id | ||
| AND EXISTS (SELECT 1 FROM public.song_artists sa WHERE sa.artist = c.id) | ||
| GROUP BY d.id, d.name | ||
| HAVING count(*) = 1; | ||
|
|
||
| -- STEP 1: delete redundant duplicate roster rows (account already holds the | ||
| -- canonical twin). Capture first, then delete. | ||
| CREATE TEMP TABLE redundant_rows ON COMMIT DROP AS | ||
| SELECT aai.id AS row_id, aai.account_id, m.dupe_id, m.canonical_id | ||
| FROM public.account_artist_ids aai | ||
| JOIN cleanup_dupe_map m ON m.dupe_id = aai.artist_id | ||
| WHERE EXISTS ( | ||
| SELECT 1 FROM public.account_artist_ids k | ||
| WHERE k.account_id = aai.account_id AND k.artist_id = m.canonical_id); | ||
|
|
||
| DELETE FROM public.account_artist_ids aai | ||
| USING redundant_rows r | ||
| WHERE aai.id = r.row_id; | ||
|
|
||
| -- STEP 2: delete fully-orphaned funnel-duplicate artist accounts. Independent of | ||
| -- the map above so it also sweeps the duplicates orphaned by the original repoint | ||
| -- and by the pass-2 repoint, not just the ones step 1 just orphaned. | ||
| CREATE TEMP TABLE orphan_dupe_accounts ON COMMIT DROP AS | ||
| SELECT a.id | ||
| FROM public.accounts a | ||
| WHERE NOT EXISTS (SELECT 1 FROM public.account_artist_ids aai WHERE aai.artist_id = a.id) | ||
| AND NOT EXISTS (SELECT 1 FROM public.song_artists sa WHERE sa.artist = a.id) | ||
| AND NOT EXISTS (SELECT 1 FROM public.account_catalogs ac WHERE ac.account = a.id) | ||
| AND NOT EXISTS (SELECT 1 FROM public.artist_organization_ids aoi WHERE aoi.artist_id = a.id) | ||
| AND EXISTS ( | ||
| SELECT 1 FROM public.account_socials asoc | ||
| JOIN public.socials s ON s.id = asoc.social_id | ||
| WHERE asoc.account_id = a.id | ||
| AND s.profile_url ILIKE '%open.spotify.com/artist/%') | ||
| AND (SELECT count(*) FROM public.accounts c | ||
| WHERE c.name = a.name AND c.id <> a.id | ||
| AND EXISTS (SELECT 1 FROM public.song_artists sa WHERE sa.artist = c.id)) = 1; | ||
|
|
||
| DELETE FROM public.accounts a | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Deleting these orphan duplicate accounts can cascade more than the intended scraped account_socials when a qualifying account still has artist-scoped records such as funnel_analytics, campaigns, segment_reports, memories, or scheduled_actions. Consider either guarding orphan_dupe_accounts against every cascading dependent table or deleting only after explicitly verifying those dependents are absent. Prompt for AI agents |
||
| USING orphan_dupe_accounts o | ||
| WHERE a.id = o.id; | ||
|
|
||
| -- Post-condition: no account now holds BOTH a qualifying duplicate row and the | ||
| -- canonical (every redundant row was removed). | ||
| DO $$ | ||
| DECLARE remaining integer; | ||
| BEGIN | ||
| SELECT count(*) INTO remaining | ||
| FROM public.account_artist_ids aai | ||
| JOIN cleanup_dupe_map m ON m.dupe_id = aai.artist_id | ||
| WHERE EXISTS ( | ||
| SELECT 1 FROM public.account_artist_ids k | ||
| WHERE k.account_id = aai.account_id AND k.artist_id = m.canonical_id); | ||
| IF remaining > 0 THEN | ||
| RAISE EXCEPTION 'cleanup_funnel_duplicate_roster_leftovers: % redundant rows remain', remaining; | ||
| END IF; | ||
| END $$; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Step 2 only checks whether the duplicate account is referenced as
artist_id, so it can still delete an account that ownsaccount_artist_idsrows and cascade those rows away. The guard needs to exclude both sides of the join before deleting the account.Prompt for AI agents