From 365df5220be27e343896e94028ba53f736f3b2db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deluan=20Quint=C3=A3o?= Date: Sun, 9 Mar 2025 19:14:29 -0400 Subject: [PATCH] fix(server): db migration not working when MusicFolder is a relative path (#3766) * fix(server): db migration not working when MusicFolder is a relative path Signed-off-by: Deluan * remove todo Signed-off-by: Deluan * fix migration of paths in Windows --------- Signed-off-by: Deluan --- .../20241026183640_support_new_scanner.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/db/migrations/20241026183640_support_new_scanner.go b/db/migrations/20241026183640_support_new_scanner.go index 1d7a21fac..a9c48cc7d 100644 --- a/db/migrations/20241026183640_support_new_scanner.go +++ b/db/migrations/20241026183640_support_new_scanner.go @@ -7,6 +7,7 @@ import ( "io/fs" "os" "path/filepath" + "strings" "testing/fstest" "unicode/utf8" @@ -143,7 +144,6 @@ join library on media_file.library_id = library.id`, string(os.PathSeparator))) // Then create an in-memory filesystem with all paths var path string var lib model.Library - var f *model.Folder fsys := fstest.MapFS{} for rows.Next() { @@ -152,9 +152,9 @@ join library on media_file.library_id = library.id`, string(os.PathSeparator))) return err } - // BFR Windows!! + path = strings.TrimPrefix(path, filepath.Clean(lib.Path)) + path = strings.TrimPrefix(path, string(os.PathSeparator)) path = filepath.Clean(path) - path, _ = filepath.Rel("/", path) fsys[path] = &fstest.MapFile{Mode: fs.ModeDir} } if err = rows.Err(); err != nil { @@ -164,19 +164,18 @@ join library on media_file.library_id = library.id`, string(os.PathSeparator))) return nil } - // Finally, walk the in-mem filesystem and insert all folders into the DB. stmt, err := tx.PrepareContext(ctx, "insert into folder (id, library_id, path, name, parent_id) values (?, ?, ?, ?, ?)") if err != nil { return err } - root, _ := filepath.Rel("/", lib.Path) - err = fs.WalkDir(fsys, root, func(path string, d fs.DirEntry, err error) error { + + // Finally, walk the in-mem filesystem and insert all folders into the DB. + err = fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } if d.IsDir() { - path, _ = filepath.Rel(root, path) - f = model.NewFolder(lib, path) + f := model.NewFolder(lib, path) _, err = stmt.ExecContext(ctx, f.ID, lib.ID, f.Path, f.Name, f.ParentID) if err != nil { log.Error("Error writing folder to DB", "path", path, err) @@ -190,7 +189,7 @@ join library on media_file.library_id = library.id`, string(os.PathSeparator))) libPathLen := utf8.RuneCountInString(lib.Path) _, err = tx.ExecContext(ctx, fmt.Sprintf(` -update media_file set path = substr(path,%d);`, libPathLen+2)) +update media_file set path = replace(substr(path, %d), '\', '/');`, libPathLen+2)) if err != nil { return fmt.Errorf("error updating media_file path: %w", err) }