From 3fff973ab6d4b6cb1b384dabadd9b5bc451183cb Mon Sep 17 00:00:00 2001 From: Deluan Date: Mon, 20 Jul 2026 08:41:04 -0400 Subject: [PATCH] refactor(db): apply review feedback to id migration Filter empty strings in collectColumn's SQL, reuse a prepared statement for rewriteColumn updates, and clarify the legacy ID functions' comment now that they emit the canonical encoding. --- .../20260720015443_uniform_canonical_ids.go | 13 ++++++++++--- model/metadata/legacy_ids.go | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/db/migrations/20260720015443_uniform_canonical_ids.go b/db/migrations/20260720015443_uniform_canonical_ids.go index a0fdd9e4d..4f987e108 100644 --- a/db/migrations/20260720015443_uniform_canonical_ids.go +++ b/db/migrations/20260720015443_uniform_canonical_ids.go @@ -98,7 +98,7 @@ func buildIDMap(ctx context.Context, tx *sql.Tx) error { func collectColumn(ctx context.Context, tx *sql.Tx, ins *sql.Stmt, table, col string) error { rows, err := tx.QueryContext(ctx, fmt.Sprintf( - "SELECT DISTINCT %[2]s FROM %[1]s WHERE %[2]s IS NOT NULL", table, col)) + "SELECT DISTINCT %[2]s FROM %[1]s WHERE %[2]s IS NOT NULL AND %[2]s <> ''", table, col)) if err != nil { return err } @@ -168,9 +168,16 @@ func rewriteColumn(ctx context.Context, tx *sql.Tx, table, col string, transform return err } _ = rows.Close() + if len(changes) == 0 { + return nil + } + upd, err := tx.PrepareContext(ctx, fmt.Sprintf("UPDATE %s SET %s = ? WHERE rowid = ?", table, col)) + if err != nil { + return err + } + defer upd.Close() for _, c := range changes { - if _, err := tx.ExecContext(ctx, fmt.Sprintf( - "UPDATE %s SET %s = ? WHERE rowid = ?", table, col), c.val, c.rowid); err != nil { + if _, err := upd.ExecContext(ctx, c.val, c.rowid); err != nil { return err } } diff --git a/model/metadata/legacy_ids.go b/model/metadata/legacy_ids.go index 57c367b44..0c1d5825a 100644 --- a/model/metadata/legacy_ids.go +++ b/model/metadata/legacy_ids.go @@ -12,8 +12,8 @@ import ( "github.com/navidrome/navidrome/model/id" ) -// These are the legacy ID functions that were used in the original Navidrome ID generation. -// They are kept here for backwards compatibility with existing databases. +// These legacy ID functions hash the same inputs as the original Navidrome ID generation, +// now emitted in the canonical base62 encoding (matching what the uniform-ids migration stores). func legacyTrackID(mf model.MediaFile, prependLibId bool) string { key := mf.Path