ADR-0032: Entity Tags Projection Is Repaired By Re-Upsert, Not By Same-Transaction Writes
Context and Problem Statement
[📝 ADR-0025](./📝 ADR-0025-unified-tag-taxonomy) and SPEC-0018 REQ "Denormalized Entity Tags Table" say that when a tag-entity association is created or removed through an Ent edge, the matching entity_tags row SHALL be created or deleted in the same transaction.
Issue #349 confirmed the implementation does not do that: internal/tags/upsert.go writes the Ent edge through the generated client and the entity_tags row through a separate raw database/sql connection, so the two statements commit independently. A crash or a failed statement between them leaves the two stores out of step.
Doing it literally is not available with the tooling the repo has today:
- Ent v0.14.5 generates no raw statement API on
*ent.Tx(it exposes onlyCommit,Rollback,Client()), and the code generator runs without theinterceptforsql/execqueryfeatures (ent/generate.go), so there is no way to run theINSERTon the connection the transaction already holds. - The only way around that is to build a throwaway
ent.Clientaround a hand-opened*sql.Tx(entsql.NewDriverwith a transaction adapter). That client would not carry the hooks registered byinternal/database.RegisterEncryptionHooks, and constructing a client per write is a much larger correctness surface than the drift it prevents. - The default database is SQLite opened with
cache=shared, where two write connections touching the same table are toldSQLITE_LOCKEDinstead of waiting. Holding an Ent transaction open while a second connection writes toentity_tagsis therefore actively harmful on the default configuration.
Decision Drivers
- Tag writes must converge, not necessarily commit atomically:
tags.UpsertTagsForEntityis idempotent, so any later call for the same entity repairs a missing projection row. - Nothing reads
entity_tagsyet. As of this writing the table has exactly one writer (internal/tags/upsert.go) and no reader ininternal/, so drift cannot yet reach a user through tag browsing or filtering. - Every tag write now goes through one helper (
services.MetadataService.PersistTypedTags), so the convergence rule lives in one place instead of per call site.
Considered Options
- Option 1: One Ent transaction covering both writes. Correct, but not expressible with the generated API (above). Blocked, not chosen.
- Option 2: Bind a temporary Ent client to a hand-opened
*sql.Tx. Atomic, but bypasses the registered encryption hooks and rebuilds a client per call. Rejected. - Option 3: Commit the Ent edge first, then insert the projection row idempotently, and repair drift with any later upsert. Chosen: it keeps the source of truth (the Ent junction tables) authoritative, keeps one connection per statement, and makes the failure loud instead of silent.
- Option 4: Stop maintaining
entity_tagsuntil a reader exists. Rejected; the table and its DDL already shipped in #307 and removing its maintenance would be a larger design reversal than #349 asks for.
Decision Outcome
Chosen option 3. The contract SPEC-0018 REQ "Denormalized Entity Tags Table" is amended to state in this PR is:
- The Ent edge write commits first and is authoritative.
- The
entity_tagsrow is written immediately afterwards withINSERT ... ON CONFLICT (tag_id, entity_type, entity_id) DO NOTHING, so the projection converges on every write. - A projection failure is logged with the entity and tag named, never swallowed: at error level on the user-triggered regenerate path, where the request exists precisely to publish those tags, and at warn level inside the background enrichment cycle, which re-runs the upsert on its next pass.
- A missing row is repaired by the next
UpsertTagsForEntityfor that entity: the background enrichment cycle, a "regenerate AI" request, or the tag backfill all re-run it.
The removal half of the original REQ stays unimplemented, on purpose: no code path removes a tag-entity edge today (nothing calls RemoveArtistIDs, RemoveAlbumIDs, RemoveTrackIDs or their Clear equivalents on Tag), so there is no removal to mirror. Whoever adds tag removal, or deletes artists, albums or tracks outright, must delete the matching entity_tags rows in the same change and move this ADR to accepted with that rule recorded.
Consequences
- Positive: no second write connection, no hook bypass, no new lock contention on the default SQLite configuration; every write path converges the projection through one helper.
- Positive: a projection failure is now visible in the log instead of being a
Warninside an enrichment loop. - Negative: a process crash between the two statements can leave an entity missing from
entity_tagsuntil it is next enriched, regenerated or backfilled. Reads must tolerate that until this ADR is superseded. - Negative: the guarantee is dialect-dependent. The
ON CONFLICTform is PostgreSQL and SQLite syntax; MySQL compatibility is tracked separately in #346 and is not changed here. - Open: the table has no reader yet. Whoever adds the first reader decides whether the drift window needs a reconciliation job, and should revisit this record at that point.