feat(server): scanner + DeleteTrackFile write library_changes

Wires sync.LogChange into the library mutation sites so /api/library/sync
reflects upserts and deletes.

Architectural pivot: LogChange's signature is now (ctx, dbq.DBTX, ...) so
it works with both *pgxpool.Pool and pgx.Tx. The scanner doesn't run
mutations in explicit transactions, so it pool-binds; delete.go matches.
Tx-bound callers (likes/playlists in subsequent commits) keep atomicity.

Also: sync.FormatUUID centralizes the pgtype.UUID → canonical string
conversion that both the scanner and the sync handler need; library_sync.go
now uses it instead of a local copy.

Best-effort logging on scanner failures (Warn, don't fail the scan): a
LogChange error after a successful upsert is rare and self-healing — the
next scan that touches the entity re-emits the change.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-09 22:39:58 -04:00
parent 6bd8a15c7a
commit 0fa7dc7982
5 changed files with 79 additions and 51 deletions
+2 -13
View File
@@ -157,10 +157,10 @@ func TestLibrarySync_LikeRowScopedToUser(t *testing.T) {
ctx := context.Background()
tx, _ := pool.Begin(ctx)
_ = sync.LogChange(ctx, tx, sync.EntityLikeTrack,
sync.EncodeLikeID(uuidToString(u.ID), "11111111-1111-1111-1111-111111111111"),
sync.EncodeLikeID(sync.FormatUUID(u.ID), "11111111-1111-1111-1111-111111111111"),
sync.OpUpsert)
_ = sync.LogChange(ctx, tx, sync.EntityLikeTrack,
sync.EncodeLikeID(uuidToString(other.ID), "22222222-2222-2222-2222-222222222222"),
sync.EncodeLikeID(sync.FormatUUID(other.ID), "22222222-2222-2222-2222-222222222222"),
sync.OpUpsert)
_ = tx.Commit(ctx)
@@ -206,14 +206,3 @@ func TestSplitOnce(t *testing.T) {
}
}
func TestFormatUUIDBytes(t *testing.T) {
var b [16]byte
for i := range b {
b[i] = byte(i)
}
got := formatUUIDBytes(b)
want := "00010203-0405-0607-0809-0a0b0c0d0e0f"
if got != want {
t.Errorf("formatUUIDBytes = %q want %q", got, want)
}
}