fix(subsonic): always emit required created field on AlbumID3 (#5340)

* fix(subsonic): always emit required `created` field on AlbumID3

Strict OpenSubsonic clients (e.g. Navic via dev.zt64.subsonic) reject
search3/getAlbum/getAlbumList2 responses that omit the `created` field,
which the spec marks as required. Navidrome was dropping it whenever
the album's CreatedAt was zero.

Root cause was threefold:

1. buildAlbumID3/childFromAlbum conditionally emitted `created`, so a
   zero CreatedAt became a missing JSON key.
2. ToAlbum's `older()` helper treated a zero BirthTime as the minimum,
   so a single track with missing filesystem birth time could poison
   the album aggregation.
3. phase_1_folders' CopyAttributes copied `created_at` from the previous
   album row unconditionally, propagating an already-zero value forward
   on every metadata-driven album ID change. Since sql_base_repository
   drops `created_at` on UPDATE, a poisoned row could never self-heal.

Fixes:
- Always emit `created`, falling back to UpdatedAt/ImportedAt when
  CreatedAt is zero. Adds albumCreatedAt() helper used by both
  buildAlbumID3 and childFromAlbum.
- Guard `older()` against a zero second argument.
- Skip the CopyAttributes call in phase_1_folders when the previous
  album's created_at is zero, so the freshly-computed value survives.
- New migration backfills existing broken rows from media_file.birth_time
  (falling back to updated_at).

Tested against a real DB: repaired 605/6922 affected rows, no side
effects on healthy rows.

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(subsonic): return albumCreatedAt by value to avoid heap escape

Returning *time.Time from albumCreatedAt caused Go escape analysis to
move the entire model.Album parameter to the heap, since the returned
pointer aliased a field of the value receiver. For hot endpoints like
getAlbumList2 and search3, this meant one full-struct heap allocation
per album result.

Return time.Time by value and let callers wrap it with gg.P() to take
the address locally. Only the small time.Time value escapes; the
model.Album struct stays on the stack. Also corrects the doc comment
to reflect the actual guarantee ("best-effort" rather than "non-zero"),
matching the test case that exercises the all-zero fallback.

---------

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan Quintão 2026-04-10 19:29:20 -04:00 • committed by GitHub
commit 9b0bfc606b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 126 additions and 5 deletions

View file

@ -361,6 +361,9 @@ func older(t1, t2 time.Time) time.Time {
if t1.IsZero() {
return t2
}
if t2.IsZero() {
return t1
}
if t1.After(t2) {
return t2
}

View file

@ -119,6 +119,20 @@ var _ = Describe("MediaFiles", func() {
Expect(a.MinYear).To(Equal(1999))
})
})
Context("CreatedAt aggregation", func() {
It("ignores zero BirthTime values when computing the oldest", func() {
mfs = MediaFiles{
{BirthTime: t("2022-12-19 08:30")},
{BirthTime: time.Time{}},
{BirthTime: t("2022-12-18 10:00")},
}
Expect(mfs.ToAlbum().CreatedAt).To(Equal(t("2022-12-18 10:00")))
})
It("returns zero when all BirthTime values are zero", func() {
mfs = MediaFiles{{BirthTime: time.Time{}}, {BirthTime: time.Time{}}}
Expect(mfs.ToAlbum().CreatedAt).To(BeZero())
})
})
})
When("we have multiple songs with same dates", func() {
BeforeEach(func() {