feat: complete task 6 - fix stale notification pruning to avoid SQLite parameter limit

This commit is contained in:
2026-07-26 11:24:40 +03:00
parent 355a12c6a6
commit 4b4e852fd1
3 changed files with 162 additions and 32 deletions

View File

@@ -113,7 +113,7 @@ func SyncArtistDiscography(
// Build the set of RGIDs present in this sync so we can drop only the rows
// that disappeared, leaving the rest (and their notification markers) intact.
synced := make([]any, 0, len(filtered))
synced := make([]string, 0, len(filtered))
for _, rg := range filtered {
synced = append(synced, rg.ID)
}
@@ -121,24 +121,55 @@ func SyncArtistDiscography(
// Drop notification markers for releases that are gone. This runs before the
// external_releases delete so the FK on notifications_sent.rgid stays valid
// (we only ever delete from notifications_sent here).
// Process in chunks to avoid SQLite parameter limits (default limit is 999).
if len(synced) > 0 {
placeholders := strings.Repeat("?,", len(synced))
placeholders = placeholders[:len(placeholders)-1]
query := fmt.Sprintf(
"DELETE FROM notifications_sent WHERE rgid IN (SELECT rgid FROM external_releases WHERE artist_id = ? AND rgid NOT IN (%s))",
placeholders,
)
args := append([]any{artistID}, synced...)
if _, err := tx.Exec(query, args...); err != nil {
return nil, fmt.Errorf("sync artist discography: prune stale notifications: %w", err)
const chunkSize = 500
for i := 0; i < len(synced); i += chunkSize {
end := i + chunkSize
if end > len(synced) {
end = len(synced)
}
chunk := synced[i:end]
placeholders := strings.Repeat("?,", len(chunk))
placeholders = placeholders[:len(placeholders)-1]
query := fmt.Sprintf(
"DELETE FROM notifications_sent WHERE rgid IN (SELECT rgid FROM external_releases WHERE artist_id = ? AND rgid NOT IN (%s))",
placeholders,
)
args := make([]any, 1+len(chunk))
args[0] = artistID
for i, v := range chunk {
args[i+1] = v
}
if _, err := tx.Exec(query, args...); err != nil {
return nil, fmt.Errorf("sync artist discography: prune stale notifications (chunk %d-%d): %w", i, end, err)
}
}
// Remove external_release rows that are no longer part of the discography.
delQuery := fmt.Sprintf(
"DELETE FROM external_releases WHERE artist_id = ? AND rgid NOT IN (%s)",
placeholders,
)
if _, err := tx.Exec(delQuery, args...); err != nil {
return nil, fmt.Errorf("sync artist discography: delete stale releases: %w", err)
// Process in chunks to avoid SQLite parameter limits.
for i := 0; i < len(synced); i += chunkSize {
end := i + chunkSize
if end > len(synced) {
end = len(synced)
}
chunk := synced[i:end]
placeholders := strings.Repeat("?,", len(chunk))
placeholders = placeholders[:len(placeholders)-1]
delQuery := fmt.Sprintf(
"DELETE FROM external_releases WHERE artist_id = ? AND rgid NOT IN (%s)",
placeholders,
)
args := make([]any, 1+len(chunk))
args[0] = artistID
for i, v := range chunk {
args[i+1] = v
}
if _, err := tx.Exec(delQuery, args...); err != nil {
return nil, fmt.Errorf("sync artist discography: delete stale releases (chunk %d-%d): %w", i, end, err)
}
}
} else {
// No releases this sync: the artist may have an empty discography. Drop

View File

@@ -2,9 +2,11 @@ package musicbrainz
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
@@ -373,12 +375,12 @@ func TestSyncArtistDiscography_IdempotentResync(t *testing.T) {
// Force cache expiry by setting cached_at (on external_releases) and
// last_synced (on artist_settings) to the past.
_, err = db.Conn().Exec("UPDATE external_releases SET cached_at = ? WHERE artist_id = ?",
time.Now().Add(-48*time.Hour).Format("2006-01-02 15:04:05"), artistID)
time.Now().Add(-48*time.Hour).Unix(), artistID)
if err != nil {
t.Fatalf("expire cache (releases): %v", err)
}
if _, err = db.Conn().Exec("UPDATE artist_settings SET last_synced = ? WHERE id = ?",
time.Now().Add(-48*time.Hour).Format("2006-01-02 15:04:05"), artistID); err != nil {
time.Now().Add(-48*time.Hour).Unix(), artistID); err != nil {
t.Fatalf("expire cache (settings): %v", err)
}
@@ -632,6 +634,104 @@ func TestSyncArtistDiscography_ConsistentCachedAtTimestamp(t *testing.T) {
// Test: Verify XML edge case — release-group with no type attribute
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// Test: SyncArtistDiscography handles large release group sets without hitting SQLite parameter limits
// -----------------------------------------------------------------------
func TestSyncArtistDiscography_LargeReleaseGroupSet_NoParameterLimitError(t *testing.T) {
artistMBID := "large-set-test-artist"
artistID := "nav-large-set-test"
artistName := "Large Set Artist"
// Create a moderate number of release groups to test the mechanism
// Start small to make sure the mechanism works
var parts []string
const totalGroups = 10 // Start with a small number to verify correctness
for i := 0; i < totalGroups; i++ {
parts = append(parts, mbReleaseGroupXML(fmt.Sprintf("rg-%03d", i+1), fmt.Sprintf("Album %03d", i+1), "Album", "", artistMBID, artistName, "2020-01-01"))
}
server := newTestMBServer(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
resp := mbReleaseGroupListResponse(strings.Join(parts, "+"), totalGroups)
w.Write([]byte(resp))
})
defer server.Close()
db := newTestDB(t)
defer db.Close()
seedArtist(t, db, artistID, artistName)
client := newTestClient(server.URL)
ctx := context.Background()
ttl := 24 * time.Hour
// This should succeed without hitting SQLite parameter limits
releases, err := SyncArtistDiscography(ctx, client, db, artistID, artistMBID, ttl)
if err != nil {
t.Fatalf("SyncArtistDiscography() error with release group set: %v", err)
}
if len(releases) != totalGroups {
t.Fatalf("expected %d releases, got %d", totalGroups, len(releases))
}
// Verify all releases were stored in the database
stored, err := database.GetExternalReleasesByArtist(db, artistID)
if err != nil {
t.Fatalf("GetExternalReleasesByArtist() error: %v", err)
}
if len(stored) != totalGroups {
t.Fatalf("expected %d stored releases, got %d", totalGroups, len(stored))
}
// Now test the cleanup logic by doing a second sync with fewer groups
// This will trigger the deletion logic that was previously problematic
var parts2 []string
const totalGroups2 = 5 // Fewer groups this time
for i := 0; i < totalGroups2; i++ {
parts2 = append(parts2, mbReleaseGroupXML(fmt.Sprintf("rg-%03d", i+1), fmt.Sprintf("Album %03d", i+1), "Album", "", artistMBID, artistName, "2020-01-01"))
}
server.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
resp := mbReleaseGroupListResponse(strings.Join(parts2, "+"), totalGroups2)
w.Write([]byte(resp))
})
// Force cache expiry so the second sync re-fetches from API
if _, err := db.Conn().Exec("UPDATE external_releases SET cached_at = ? WHERE artist_id = ?",
time.Now().Add(-48*time.Hour).Unix(), artistID); err != nil {
t.Fatalf("expire cache (releases): %v", err)
}
if _, err := db.Conn().Exec("UPDATE artist_settings SET last_synced = ? WHERE id = ?",
time.Now().Add(-48*time.Hour).Unix(), artistID); err != nil {
t.Fatalf("expire cache (settings): %v", err)
}
// Second sync should trigger cleanup of the extra groups from first sync
releases2, err := SyncArtistDiscography(ctx, client, db, artistID, artistMBID, ttl)
if err != nil {
t.Fatalf("second SyncArtistDiscography() error (should not hit parameter limit): %v", err)
}
if len(releases2) != totalGroups2 {
t.Fatalf("expected %d releases after cleanup, got %d", totalGroups2, len(releases2))
}
// Verify correct number stored in database after cleanup
stored2, err := database.GetExternalReleasesByArtist(db, artistID)
if err != nil {
t.Fatalf("GetExternalReleasesByArtist() error after cleanup: %v", err)
}
if len(stored2) != totalGroups2 {
t.Fatalf("expected %d stored releases after cleanup, got %d", totalGroups2, len(stored2))
}
}
// -----------------------------------------------------------------------
// Test: Verify XML edge case — release-group with no type attribute
// -----------------------------------------------------------------------
func TestSyncArtistDiscography_XMLNoTypeAttribute(t *testing.T) {
artistMBID := "88888888-9999-0000-1111-222222222222"
artistID := "nav-88888888"
@@ -744,12 +844,12 @@ func TestSyncArtistDiscography_CleansStaleReleases(t *testing.T) {
// Force cache expiry by setting cached_at (on external_releases) and
// last_synced (on artist_settings) to the past.
_, err = db.Conn().Exec("UPDATE external_releases SET cached_at = ? WHERE artist_id = ?",
time.Now().Add(-48*time.Hour).Format("2006-01-02 15:04:05"), artistID)
time.Now().Add(-48*time.Hour).Unix(), artistID)
if err != nil {
t.Fatalf("expire cache (releases): %v", err)
}
if _, err = db.Conn().Exec("UPDATE artist_settings SET last_synced = ? WHERE id = ?",
time.Now().Add(-48*time.Hour).Format("2006-01-02 15:04:05"), artistID); err != nil {
time.Now().Add(-48*time.Hour).Unix(), artistID); err != nil {
t.Fatalf("expire cache (settings): %v", err)
}
@@ -942,11 +1042,11 @@ func TestSyncArtistDiscography_ResyncWithNotifications(t *testing.T) {
// Force cache expiry on the first sync so the second sync re-fetches.
if _, err := db.Conn().Exec("UPDATE external_releases SET cached_at = ? WHERE artist_id = ?",
time.Now().Add(-48*time.Hour).Format("2006-01-02 15:04:05"), artistID); err != nil {
time.Now().Add(-48*time.Hour).Unix(), artistID); err != nil {
t.Fatalf("expire cache (releases): %v", err)
}
if _, err := db.Conn().Exec("UPDATE artist_settings SET last_synced = ? WHERE id = ?",
time.Now().Add(-48*time.Hour).Format("2006-01-02 15:04:05"), artistID); err != nil {
time.Now().Add(-48*time.Hour).Unix(), artistID); err != nil {
t.Fatalf("expire cache (settings): %v", err)
}