2023-02-10 00:29:08 +01:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
2023-11-27 20:46:44 +01:00
|
|
|
"context"
|
2023-02-10 00:29:08 +01:00
|
|
|
"database/sql"
|
|
|
|
"path/filepath"
|
2024-02-17 04:00:44 +01:00
|
|
|
"slices"
|
2023-02-10 00:29:08 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/navidrome/navidrome/consts"
|
|
|
|
"github.com/navidrome/navidrome/log"
|
2023-04-04 15:57:00 +02:00
|
|
|
"github.com/pressly/goose/v3"
|
2023-02-10 00:29:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-11-27 20:46:44 +01:00
|
|
|
goose.AddMigrationContext(upChangeImageFilesListSeparator, downChangeImageFilesListSeparator)
|
2023-02-10 00:29:08 +01:00
|
|
|
}
|
|
|
|
|
2023-11-27 20:46:44 +01:00
|
|
|
func upChangeImageFilesListSeparator(_ context.Context, tx *sql.Tx) error {
|
2023-02-10 00:29:08 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-12 20:58:33 +01:00
|
|
|
var id string
|
|
|
|
var imageFiles sql.NullString
|
2023-02-10 00:29:08 +01:00
|
|
|
for rows.Next() {
|
|
|
|
err = rows.Scan(&id, &imageFiles)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-12 20:58:33 +01:00
|
|
|
files := upChangeImageFilesListSeparatorDirs(imageFiles.String)
|
|
|
|
if files == imageFiles.String {
|
2023-02-10 00:29:08 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-11-27 20:46:44 +01:00
|
|
|
func downChangeImageFilesListSeparator(_ context.Context, tx *sql.Tx) error {
|
2023-02-10 00:29:08 +01:00
|
|
|
// This code is executed when the migration is rolled back.
|
|
|
|
return nil
|
|
|
|
}
|