Files
minstrel/internal/sync/changes.go
T
bvandeusen 3959c85111 feat(server): sync.LogChange helper + EntityType/Op constants
New package internal/sync. LogChange writes a library_changes row inside
the supplied tx. Encode helpers produce stable composite ids for like_*
and playlist_track entries. Subsequent commits wire LogChange into the
scanner / likes / playlists services.

Also: dbtest.dataTables now includes library_changes so test isolation
holds across runs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 22:29:51 -04:00

43 lines
1.4 KiB
Go

package sync
import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// LogChange writes a row to library_changes inside the supplied transaction.
// Callers MUST pass the same tx that performed the underlying mutation —
// the change row and the mutation must commit or roll back together.
//
// entityID is the string form of the row's primary key. For composite-key
// entities (likes, playlist_tracks), use EncodeLikeID / EncodePlaylistTrackID
// to produce stable strings.
func LogChange(ctx context.Context, tx pgx.Tx, entityType EntityType, entityID string, op Op) error {
q := dbq.New(tx)
if err := q.InsertLibraryChange(ctx, dbq.InsertLibraryChangeParams{
EntityType: string(entityType),
EntityID: entityID,
Op: string(op),
}); err != nil {
return fmt.Errorf("sync.LogChange(%s, %s, %s): %w", entityType, entityID, op, err)
}
return nil
}
// EncodeLikeID joins a user UUID and an entity UUID into the stable
// composite identifier used for like_* rows in library_changes. Both
// sides are UUID strings so no escaping is needed.
func EncodeLikeID(userID, entityID string) string {
return userID + ":" + entityID
}
// EncodePlaylistTrackID joins a playlist UUID and a track UUID into the
// stable composite identifier used for playlist_track rows.
func EncodePlaylistTrackID(playlistID, trackID string) string {
return playlistID + ":" + trackID
}