Files
minstrel/internal/sync/changes.go
T
bvandeusenandClaude Opus 5.5 58fb2bbe9d
release / go (push) Successful in 1m56s
release / web (push) Successful in 1m18s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m0s
release / android (push) Successful in 4m20s
release / Build signed APK (push) Successful in 4m52s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 25s
release / Verify release artifacts (tag releases only) (push) Skipped
refactor: drop code nothing calls, and the sync log rows no app reads (#8002)
- library.ProbeDurationMs and IsAudioFile, and db.MigrationsFS, had no
  callers.
- The delta sync logged and hydrated like and playlist changes on every
  like, playlist edit and duplicate merge, including a GetPlaylistsByIDs
  query per sync. Android applies only artists, albums and tracks, and
  the web doesn't sync; likes and playlists reach both through their own
  endpoints and live events. The like and playlist-entry deletes also
  went to every caller unscoped, user ids included. They are no longer
  written or served, and migration 0112 clears the rows already logged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 12:52:35 -05:00

84 lines
2.7 KiB
Go

package sync
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// LogChange writes a row to library_changes via the supplied DBTX. dbq.DBTX
// is satisfied by both *pgxpool.Pool and pgx.Tx, so callers can opt into
// transactional safety where they already hold a tx, or pool-bind for
// best-effort logging when no tx is available.
//
// Callers SHOULD pass a tx that owns the underlying mutation — the change
// row and the mutation then commit or roll back together. When pool-bound,
// the scanner-style "log after success" pattern keeps the log consistent
// with reality at the cost of a tiny race window: if the LogChange call
// itself fails after the mutation succeeded, that mutation won't appear in
// the change log until the entity is touched again.
//
// entityID is the row's UUID as a string; use FormatUUID.
func LogChange(ctx context.Context, dbtx dbq.DBTX, entityType EntityType, entityID string, op Op) error {
q := dbq.New(dbtx)
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
}
// LogChanges is LogChange for a set of entities of one type sharing one op.
//
// Exists because the scan's reconcile pass can mark a large part of a library
// missing in a single sweep, and a per-row loop there would be thousands of
// round-trips inside an operation that is already slow. Every
// other caller mutates one entity and should keep using LogChange.
//
// A no-op on an empty set, so callers don't have to guard.
func LogChanges(ctx context.Context, dbtx dbq.DBTX, entityType EntityType, entityIDs []string, op Op) error {
if len(entityIDs) == 0 {
return nil
}
q := dbq.New(dbtx)
if err := q.InsertLibraryChanges(ctx, dbq.InsertLibraryChangesParams{
EntityType: string(entityType),
EntityIds: entityIDs,
Op: string(op),
}); err != nil {
return fmt.Errorf("sync.LogChanges(%s, %d ids, %s): %w", entityType, len(entityIDs), op, err)
}
return nil
}
// FormatUUID renders a pgtype.UUID as the canonical 8-4-4-4-12 hex form.
// Returns "" if the UUID is not valid.
func FormatUUID(u pgtype.UUID) string {
if !u.Valid {
return ""
}
return formatUUIDBytes(u.Bytes)
}
func formatUUIDBytes(b [16]byte) string {
const hex = "0123456789abcdef"
out := make([]byte, 36)
pos := 0
for i := 0; i < 16; i++ {
if i == 4 || i == 6 || i == 8 || i == 10 {
out[pos] = '-'
pos++
}
out[pos] = hex[b[i]>>4]
out[pos+1] = hex[b[i]&0x0f]
pos += 2
}
return string(out)
}