fix(ddl): backfill album saves and reposts to playlist - #1008
Closed
rickyrombo wants to merge 1 commit into
Closed
Conversation
An album is a playlist with is_album = true. The indexer briefly derived a separate 'album' save_type/repost_type by reading playlists.is_album at index time; it no longer does (OpenAudio/go-openaudio#428). This backfills the rows written while that was live: 670 saves and 528 reposts, all first appearing on 2026-05-28. Two hazards this has to avoid: * on_save/on_repost fire on AFTER INSERT OR UPDATE, and the notification group_id embeds the type ('save:<id>:type:<save_type>'). A plain UPDATE would mint a second favourite/repost notification per row under a new group_id, so those two triggers are disabled for the backfill. trg_saves/trg_reposts stay enabled so the search indexer still sees the rows change. * save_type/repost_type are part of the primary key. Verified against prod data that no (user_id, item_id, txhash) has both a 'playlist' and an 'album' row, so this is a straight UPDATE with no conflict handling. Aggregate counts are unaffected either way: handle_save's delta is transition-aware and evaluates to 0 when is_delete does not change. Each table gets its own transaction to keep the ACCESS EXCLUSIVE lock taken by ALTER TABLE ... DISABLE TRIGGER as short as possible. Re-running is a no-op once no 'album' rows remain. The 'album' label is left in the savetype/reposttype enums since Postgres cannot drop an enum value without rebuilding the type. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This was referenced Aug 4, 2026
Contributor
Author
|
Superseded by #1011, which combines this with the other two api-side changes. They're only correct together in one deploy — merging the module bump without the users backfill would fail ETL migration 0035's index creation and stop the indexer starting. |
rickyrombo
added a commit
that referenced
this pull request
Aug 5, 2026
Supersedes #1008, #1009 and #1010, which were the same work split three ways. ## Why one PR These three changes are only correct **together, in one deploy**. Split, each one alone breaks something: | merged alone | result | |---|---| | the bump | ETL `0035` creates `users_current_uniq_idx`, **fails on the existing duplicates**, `RunMigrations` errors and the indexer won't start | | `0236` (album) | the old indexer keeps deriving `album` from `is_album` and undoes the backfill | | `0237` (users) | harmless, but pointless without the index that stops it recurring | As one PR the deploy is atomic, and the ordering inside it is guaranteed by existing machinery: `bridge migrate` runs as a pre-roll Job that every serving Deployment `DependsOn` (serving pods get `runMigrations=false`), so both ddl migrations complete before the indexer starts and runs the ETL's. ## Contents **`0237_users_one_current_row_backfill`** — deletes 5 duplicate `is_current` rows from `users`. Small count, large blast radius: joins from an entity to its owner's wallet fan out, measured at **+18 tracks and +787 follows** on a production clone. Must precede the ETL index. **`0236_saves_reposts_album_to_playlist`** — 670 saves and 528 reposts written as `album` collapse to `playlist`. `on_save`/`on_repost` are disabled for the update (their notification `group_id` embeds the type, so a plain UPDATE would mint duplicate favourite notifications); `trg_saves`/`trg_reposts` stay enabled so the search indexer sees the change. **`deps: pin pkg/etl v1.6.4`** — brings OpenAudio/go-openaudio#428 (album type), #425 (the `users` invariant + genesis-writer join simplification) and #433 (`0035` no longer deletes anything). ## The delete moved out of the ETL migration `v1.6.3`'s `0035` deleted the duplicates itself. Since ETL migrations run automatically at indexer start, that made a `go get` able to remove rows from this database. #433 split it: the index stays in the ETL migration, the repair moved to `0237` here. **`v1.6.4` ships zero `DELETE` statements** — verified against the resolved module, not just the tag. The comment above the ETL config now records that line, and its corollary: an ETL migration can depend on a ddl one having run, and `0035` fails loudly if `0237` hasn't. ## Verified - Resolved module `pkg/etl@v1.6.4` contains `0035` with `CREATE UNIQUE INDEX` and **0** `DELETE` statements. - Both migration orders against fixtures: backfill→index applies cleanly (`violations 0`, `indisvalid = t`); index→backfill fails with `could not create unique index … Key (user_id)=(98311147) is duplicated`, which is the intended signal that `0237` hasn't run. - Both migrations idempotent; re-running is a no-op. - `0236` fires **zero** `on_save`/`on_repost` triggers against a fixture with the real wiring, and exactly one `pg_notify` per updated row. - `go build ./...` and `go vet ./indexer/` clean. - No FK references `users`; its triggers are INSERT / INSERT OR UPDATE, so the delete fires neither. - Cutting `pkg/etl/v1.6.4` did not move `openaudio/go-openaudio:stable` — still the 2026-07-30 `v1.8.2` digest, so no node-operator rollout. ## Not established The cause of the duplicate `users` rows. Both indexer create paths reject an existing user, so a single writer can't produce them; a second writer can, since check-then-act isn't atomic across transactions. Three of five pairs put a bare-hex `txhash` next to a `0x`-prefixed one, which fits but doesn't prove it. The index will surface it if it recurs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 <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.
What
Collapses
save_type/repost_type'album'back into'playlist'— 670 saves and 528 reposts.Pairs with OpenAudio/go-openaudio#428, which stops the indexer writing
'album'. These ship together: this backfill alone would be re-broken by the current indexer.Why
An album is a playlist with
is_album = true. The indexer briefly derived a separate'album'type by readingplaylists.is_albumat index time — a side effect of an entity-id collision fix, not a deliberate convention. It first appears in prodsavesandrepostson the same day, 2026-05-28, after seven years with zero.is_albumis mutable, butsave_typeis written once and is part of the primary key(user_id, save_item_id, save_type, txhash)— so the same chain history indexed at different times produced different rows. Nothing reads the distinction: every consumer istrack/!= track, or ORs the two together (get_account_playlists,reconcile_aggregates). The notification triggers already derive album fromis_albumat read time.Two hazards, and how they're handled
Duplicate notifications.
on_save/on_repostfire onAFTER INSERT OR UPDATE, and the notificationgroup_idembeds the type ('save:<id>:type:<save_type>'). A plain UPDATE would mint a second favourite notification per row under a new group_id. Those two triggers are disabled for the backfill;trg_saves/trg_reposts(thepg_notify→ search indexer) stay enabled so ES still sees the change.Primary key. The type is part of the PK. Verified against prod data that no
(user_id, item_id, txhash)has both a'playlist'and an'album'row, so this is a straight UPDATE with no conflict handling.Aggregate counts are unaffected either way —
handle_save'sdeltais transition-aware and evaluates to0whenis_deletedoes not change.Verification
Applied against a fixture mirroring the real wiring (enums, 4-column PK, all four triggers):
album→playlist;trackuntouchedon_save/on_repostfiringspg_notifyfired exactly once per updated rowplaylistand analbumversion kept both (differenttxhash)tgenabled = 'O') afterwardsNotes
lock_timeoutto keep theACCESS EXCLUSIVElock fromALTER TABLE ... DISABLE TRIGGERas short as possible. ~1.2k rows, so it should be milliseconds.'album'rows remain.'album'is deliberately left in thesavetype/reposttypeenums — Postgres can't drop an enum value without rebuilding the type, and the indexer no longer writes it.🤖 Generated with Claude Code