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 }