mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Fix data corruption when moving folders (#4169)
* Add data correction migration * Correct folder hierarchy after folder move
This commit is contained in:
@@ -212,3 +212,34 @@ func (m *Mover) rollback() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// correctSubFolderHierarchy sets the path of all contained folders to be relative to the given folder.
|
||||
// It does not move the folder hierarchy in the filesystem.
|
||||
func correctSubFolderHierarchy(ctx context.Context, rw models.FolderReaderWriter, folder *models.Folder) error {
|
||||
folders, err := rw.FindByParentFolderID(ctx, folder.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("finding contained folders in folder %s: %w", folder.Path, err)
|
||||
}
|
||||
|
||||
folderPath := folder.Path
|
||||
|
||||
for _, f := range folders {
|
||||
oldPath := f.Path
|
||||
folderBasename := filepath.Base(f.Path)
|
||||
correctPath := filepath.Join(folderPath, folderBasename)
|
||||
|
||||
logger.Debugf("updating folder %s to %s", oldPath, correctPath)
|
||||
|
||||
f.Path = correctPath
|
||||
if err := rw.Update(ctx, f); err != nil {
|
||||
return fmt.Errorf("updating folder path %s -> %s: %w", oldPath, f.Path, err)
|
||||
}
|
||||
|
||||
// recurse
|
||||
if err := correctSubFolderHierarchy(ctx, rw, f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user