From 8b509f94f69bd1d5e1ed21f09d0877b6292561ac Mon Sep 17 00:00:00 2001 From: Deluan Date: Tue, 21 Jul 2026 09:48:27 -0400 Subject: [PATCH] refactor(db): inline canonicalID into its only consumer, the uniform-ids migration --- .../20260720015443_uniform_canonical_ids.go | 34 ++++++++++++++++ db/migrations/id_canonical.go | 39 ------------------- 2 files changed, 34 insertions(+), 39 deletions(-) delete mode 100644 db/migrations/id_canonical.go diff --git a/db/migrations/20260720015443_uniform_canonical_ids.go b/db/migrations/20260720015443_uniform_canonical_ids.go index 20d0b68cc..cde0b584c 100644 --- a/db/migrations/20260720015443_uniform_canonical_ids.go +++ b/db/migrations/20260720015443_uniform_canonical_ids.go @@ -2,16 +2,50 @@ package migrations import ( "context" + "crypto/md5" "database/sql" + "encoding/hex" "encoding/json" "fmt" + "math/big" "strings" "github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/consts" + "github.com/navidrome/navidrome/model/id" "github.com/pressly/goose/v3" ) +// canonicalID maps any historical Navidrome id shape to the canonical 22-char base62 encoding +// of a 128-bit value; unrecognized shapes (including empty and share ids) pass through unchanged. +func canonicalID(s string) string { + switch len(s) { + case 22: + v, ok := new(big.Int).SetString(s, 62) + if !ok || v.Sign() < 0 || v.BitLen() <= 128 { + return s + } + sum := md5.Sum([]byte(s)) + return id.Encode128(sum) + case 32: + b, err := hex.DecodeString(s) + if err != nil { + return s + } + return id.Encode128([16]byte(b)) + case 36: + if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { + return s + } + b, err := hex.DecodeString(s[:8] + s[9:13] + s[14:18] + s[19:23] + s[24:]) + if err != nil { + return s + } + return id.Encode128([16]byte(b)) + } + return s +} + func init() { goose.AddMigrationContext(upUniformCanonicalIds, downUniformCanonicalIds) } diff --git a/db/migrations/id_canonical.go b/db/migrations/id_canonical.go deleted file mode 100644 index f080a1723..000000000 --- a/db/migrations/id_canonical.go +++ /dev/null @@ -1,39 +0,0 @@ -package migrations - -import ( - "crypto/md5" - "encoding/hex" - "math/big" - - "github.com/navidrome/navidrome/model/id" -) - -// canonicalID maps any historical Navidrome id shape to the canonical 22-char base62 encoding -// of a 128-bit value; unrecognized shapes (including empty and share ids) pass through unchanged. -func canonicalID(s string) string { - switch len(s) { - case 22: - v, ok := new(big.Int).SetString(s, 62) - if !ok || v.Sign() < 0 || v.BitLen() <= 128 { - return s - } - sum := md5.Sum([]byte(s)) - return id.Encode128(sum) - case 32: - b, err := hex.DecodeString(s) - if err != nil { - return s - } - return id.Encode128([16]byte(b)) - case 36: - if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { - return s - } - b, err := hex.DecodeString(s[:8] + s[9:13] + s[14:18] + s[19:23] + s[24:]) - if err != nil { - return s - } - return id.Encode128([16]byte(b)) - } - return s -}