Skip to content

fix(etl): record album saves and reposts as playlist - #428

Merged
rickyrombo merged 2 commits into
mainfrom
fix/save-repost-album-type
Aug 4, 2026
Merged

fix(etl): record album saves and reposts as playlist#428
rickyrombo merged 2 commits into
mainfrom
fix/save-repost-album-type

Conversation

@rickyrombo

Copy link
Copy Markdown
Contributor

What

save_type / repost_type no longer take the value album. Albums record as playlist through all three resolution paths (metadata type, chain entity_type, DB inference), and isAlbumPlaylist() is removed.

Why

The album value was never a deliberate convention — it arrived as a side effect of the entity-id collision fix (#308). That fix was about not falling through to inferSaveType (which ranks tracks first) when the chain says Playlist; resolving via playlists.is_album happened to emit album when the flag was set. That guard is untouched here and still covered by TestSave_Playlist_WhenTrackIdCollides.

Evidence it was incidental: album appears in prod saves and reposts for the first time on the same day, 2026-05-28, after seven years and 10.2M saves with zero — a deployment fingerprint, not organic behaviour.

Deriving it from is_album is wrong on three counts:

  1. Not replay-safe. is_album is mutable; save_type is written once and is part of the saves / reposts primary key (user_id, save_item_id, save_type, txhash). The same chain history indexed at different times yields different rows. In the genesis migration this silently collapsed saves whose (user, item) existed under both types. Prod already has 139,090 saves typed playlist whose target is now is_album = true.
  2. Duplicate notifications. handle_save / handle_repost build the notification group_id from the type ('save:<id>:type:<save_type>'), so one favourite can notify twice under two group_ids.
  3. Nothing reads it. Every consumer is track / != track, or ORs the two together (get_account_playlists, reconcile_aggregates). The notification triggers already derive album from is_album at read time — the pattern that actually works.

Also drops one query per playlist save/repost.

Scope

  • album remains a valid enum label and is still accepted as metadata input; the indexer just never writes it.
  • Existing prod rows (670 saves, 528 reposts) are backfilled by a separate PR in AudiusProject/api. That migration and this change ship together — the backfill alone would be re-broken by the current indexer.

Testing

All 19 tests in the affected area pass, including the collision guard:

--- PASS: TestSave_Album_RecordsAsPlaylist   (metadata + chain entity_type paths)
--- PASS: TestRepost_Album_RecordsAsPlaylist
--- PASS: TestSave_Playlist_WhenTrackIdCollides
--- PASS: TestSave_/TestRepost_/TestUnsave_/TestUnrepost_/TestShare_ ...
ok  .../entity_manager  45.323s

TestSave_Album_Success is replaced by TestSave_Album_RecordsAsPlaylist, table-driven over both resolution paths, plus a repost mirror.

⚠️ The full processors package was not run to completion locally — it exceeds a 45m timeout in setupTestDB (every test re-runs the whole migration set against one DB), unrelated to this change. Relying on CI for the remainder.

🤖 Generated with Claude Code

save_type/repost_type gained an 'album' value as a side effect of the
entity-id collision fix: resolving a chain "Playlist" entity by reading
playlists.is_album happened to emit 'album' when the flag was set. The
bug that fix was actually about — falling through to track inference and
writing save_type='track' for a playlist whose id collides with a track —
is unaffected by this change and still covered by
TestSave_Playlist_WhenTrackIdCollides.

Deriving the value from is_album is wrong for three reasons:

  * is_album is mutable, but save_type is written once and is part of the
    saves/reposts primary key, so replaying the same chain history at a
    different time produced different rows. In the genesis migration this
    silently collapsed saves whose (user, item) existed under both types.
  * handle_save/handle_repost build the notification group_id from the
    type ('save:<id>:type:<save_type>'), so one favourite could notify
    twice under two different group_ids.
  * Nothing reads the distinction. Every consumer is track/not-track, or
    ORs 'playlist' and 'album' together (get_account_playlists,
    reconcile_aggregates). Callers that need it read playlists.is_album at
    query time, which is what the notification triggers already do.

Albums now record as 'playlist' through all three resolution paths
(metadata "type", chain entity_type, DB inference). This also drops one
query per playlist save/repost.

'album' stays a valid enum label and is still accepted as metadata input;
the indexer just never writes it. Existing rows are backfilled separately
in the api repo.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
With albums no longer resolving to their own value, both switch arms
returned their own input, so the switch was an identity map over
saveTypeFromEntityType's only non-empty results. Fold it into the same
"first non-empty wins" form already used for the metadata lookup, so the
priority order reads as one ladder.

Behaviour is unchanged, including the rule this function exists for: an
entity_type of "Playlist" still returns early and never reaches
inferSaveType, so a same-id track cannot win.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@rickyrombo
rickyrombo merged commit 26ccde7 into main Aug 4, 2026
5 checks passed
@rickyrombo
rickyrombo deleted the fix/save-repost-album-type branch August 4, 2026 20:41
This was referenced Aug 4, 2026
rickyrombo added a commit to AudiusProject/api that referenced this pull request Aug 5, 2026
Moves off the 2026-07-27 pseudo-version onto the real tag. Three changes come
with it, all pkg/etl-only, so the root module pin is untouched:

  * album saves and reposts record save_type/repost_type 'playlist' rather
    than deriving 'album' from the mutable playlists.is_album at index time
    (OpenAudio/go-openaudio#428). The rows already written as 'album' are
    backfilled by ddl 0236 in this PR.
  * one current row per user is enforced via users_current_uniq_idx, and the
    fifteen DISTINCT ON dedupe subqueries in genesis-writer collapse to plain
    joins (#425). The duplicates that index cannot coexist with are removed by
    ddl 0237 in this PR, which runs first.
  * that index migration no longer deletes anything itself (#433), so no ETL
    migration modifies row data.

Also rewrites the comment above the ETL config. It described the migrations
as additive without saying why that matters; it now records the line being
held — a module bump reaches this database automatically, so data repair
belongs in ddl/ where it goes through review and the pre-roll migrate Job —
and the corollary that 0035 fails if ddl 0237 has not run, which the Job's
ordering is what prevents.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
rickyrombo added a commit to AudiusProject/api 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>
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