Fix data corruption when moving folders (#4169)

* Add data correction migration
* Correct folder hierarchy after folder move
This commit is contained in:
WithoutPants
2023-10-03 17:05:08 +11:00
committed by GitHub
parent bc5df7cfaf
commit 0dbe3e6ea8
5 changed files with 117 additions and 1 deletions

View File

@@ -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
}