Skip to content

🎧 Your Personal Streaming Service

Fix album's image_files

Deluanb68ed2e4f90e
diff --git a/db/migration/20230209181414_change_image_files_list_separator.go b/db/migration/20230209181414_change_image_files_list_separator.go
new file mode 100644
index 000000000..4d9070cb8
--- /dev/null
+++ b/db/migration/20230209181414_change_image_files_list_separator.go
@@ -0,0 +1,59 @@
+package migrations
+
+import (
+ "database/sql"
+ "path/filepath"
+ "strings"
+
+ "github.com/navidrome/navidrome/consts"
+ "github.com/navidrome/navidrome/log"
+ "github.com/pressly/goose"
+ "golang.org/x/exp/slices"
+)
+
+func init() {
+ goose.AddMigration(upChangeImageFilesListSeparator, downChangeImageFilesListSeparator)
+}
+
+func upChangeImageFilesListSeparator(tx *sql.Tx) error {
+ //nolint:gosec
+ rows, err := tx.Query(`select id, image_files from album`)
+ if err != nil {
+ return err
+ }
+
+ stmt, err := tx.Prepare("update album set image_files = ? where id = ?")
+ if err != nil {
+ return err
+ }
+
+ var id, imageFiles string
+ for rows.Next() {
+ err = rows.Scan(&id, &imageFiles)
+ if err != nil {
+ return err
+ }
+
+ files := upChangeImageFilesListSeparatorDirs(imageFiles)
+ if files == imageFiles {
+ continue
+ }
+ _, err = stmt.Exec(files, id)
+ if err != nil {
+ log.Error("Error updating album's image file list", "files", files, "id", id, err)
+ }
+ }
+ return rows.Err()
+}
+
+func upChangeImageFilesListSeparatorDirs(filePaths string) string {
+ allPaths := filepath.SplitList(filePaths)
+ slices.Sort(allPaths)
+ allPaths = slices.Compact(allPaths)
+ return strings.Join(allPaths, consts.Zwsp)
+}
+
+func downChangeImageFilesListSeparator(tx *sql.Tx) error {
+ // This code is executed when the migration is rolled back.
+ return nil
+}