diff --git a/cmd/naviwatcher/main.go b/cmd/naviwatcher/main.go index 55a8214..41aee35 100644 --- a/cmd/naviwatcher/main.go +++ b/cmd/naviwatcher/main.go @@ -49,7 +49,7 @@ func main() { cancel() }() - app, err := NewApp(ctx, cfg) + app, err := NewApp(ctx, cfg, "naviwatcher.db") if err != nil { log.Fatalf("Failed to initialize application: %v", err) } @@ -63,9 +63,10 @@ func main() { } // NewApp initializes all application components: config, database, and MusicBrainz client. -func NewApp(ctx context.Context, cfg *config.Config) (*App, error) { - // Initialize database (uses default path or could be made configurable). - db, err := database.New("naviwatcher.db") +// dbPath is the SQLite database path (use ":memory:" for tests). +func NewApp(ctx context.Context, cfg *config.Config, dbPath string) (*App, error) { + // Initialize database. + db, err := database.New(dbPath) if err != nil { return nil, fmt.Errorf("failed to initialize database: %w", err) } diff --git a/cmd/naviwatcher/main_test.go b/cmd/naviwatcher/main_test.go index 0faf8ce..474e479 100644 --- a/cmd/naviwatcher/main_test.go +++ b/cmd/naviwatcher/main_test.go @@ -132,7 +132,7 @@ func TestNewApp_CreatesMusicBrainzClient(t *testing.T) { } ctx := context.Background() - app, err := NewApp(ctx, cfg) + app, err := NewApp(ctx, cfg, ":memory:") if err != nil { t.Fatalf("NewApp returned error: %v", err) } @@ -167,7 +167,7 @@ func TestNewApp_GracefulShutdown(t *testing.T) { } ctx := context.Background() - app, err := NewApp(ctx, cfg) + app, err := NewApp(ctx, cfg, ":memory:") if err != nil { t.Fatalf("NewApp returned error: %v", err) } @@ -195,7 +195,7 @@ func TestAppRun_GracefulShutdown(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) - app, err := NewApp(ctx, cfg) + app, err := NewApp(ctx, cfg, ":memory:") if err != nil { t.Fatalf("NewApp returned error: %v", err) } diff --git a/internal/database/external_releases.go b/internal/database/external_releases.go index c2388b3..af1f6da 100644 --- a/internal/database/external_releases.go +++ b/internal/database/external_releases.go @@ -24,9 +24,9 @@ func FormatCachedAt(t time.Time) interface{} { return t.UTC().Format(utcLayout) } -// joinSecondaryTypes renders a slice of secondary types as a comma-separated +// JoinSecondaryTypes renders a slice of secondary types as a comma-separated // string for storage in the secondary_types TEXT column (empty when none). -func joinSecondaryTypes(types []string) string { +func JoinSecondaryTypes(types []string) string { return strings.Join(types, ",") } @@ -72,7 +72,7 @@ func SaveExternalRelease(db *DB, release *ExternalRelease) error { var cachedAt interface{} = FormatCachedAt(release.CachedAt) _, err := db.Conn().Exec( "INSERT OR REPLACE INTO external_releases (rgid, artist_id, title, type, release_date, is_ignored, cached_at, secondary_types) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", - release.RGID, release.ArtistID, release.Title, release.Type, release.ReleaseDate, release.IsIgnored, cachedAt, joinSecondaryTypes(release.SecondaryTypes), + release.RGID, release.ArtistID, release.Title, release.Type, release.ReleaseDate, release.IsIgnored, cachedAt, JoinSecondaryTypes(release.SecondaryTypes), ) if err != nil { return fmt.Errorf("save external release: %w", err) diff --git a/internal/musicbrainz/sync.go b/internal/musicbrainz/sync.go index 6ab7cce..33d5b51 100644 --- a/internal/musicbrainz/sync.go +++ b/internal/musicbrainz/sync.go @@ -136,8 +136,8 @@ func SyncArtistDiscography( } if _, err := tx.Exec( - "INSERT INTO external_releases (rgid, artist_id, title, type, release_date, is_ignored, cached_at) VALUES (?, ?, ?, ?, ?, ?, ?)", - ext.RGID, ext.ArtistID, ext.Title, ext.Type, ext.ReleaseDate, ext.IsIgnored, database.FormatCachedAt(ext.CachedAt), + "INSERT INTO external_releases (rgid, artist_id, title, type, release_date, is_ignored, cached_at, secondary_types) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + ext.RGID, ext.ArtistID, ext.Title, ext.Type, ext.ReleaseDate, ext.IsIgnored, database.FormatCachedAt(ext.CachedAt), database.JoinSecondaryTypes(ext.SecondaryTypes), ); err != nil { return nil, fmt.Errorf("sync artist discography: insert release %s: %w", rg.ID, err) }