-
-
Notifications
You must be signed in to change notification settings - Fork 809
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix data corruption when moving folders (#4169)
* Add data correction migration * Correct folder hierarchy after folder move
- Loading branch information
1 parent
bc5df7c
commit 0dbe3e6
Showing
5 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/stashapp/stash/pkg/logger" | ||
"github.com/stashapp/stash/pkg/sqlite" | ||
) | ||
|
||
type schema52Migrator struct { | ||
migrator | ||
} | ||
|
||
func post52(ctx context.Context, db *sqlx.DB) error { | ||
logger.Info("Running post-migration for schema version 52") | ||
|
||
m := schema52Migrator{ | ||
migrator: migrator{ | ||
db: db, | ||
}, | ||
} | ||
|
||
return m.migrate(ctx) | ||
} | ||
|
||
func (m *schema52Migrator) migrate(ctx context.Context) error { | ||
if err := m.withTxn(ctx, func(tx *sqlx.Tx) error { | ||
query := "SELECT `folders`.`id`, `folders`.`path`, `parent_folder`.`path` FROM `folders` " + | ||
"INNER JOIN `folders` AS `parent_folder` ON `parent_folder`.`id` = `folders`.`parent_folder_id`" | ||
|
||
rows, err := m.db.Query(query) | ||
if err != nil { | ||
return err | ||
} | ||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var ( | ||
id int | ||
folderPath string | ||
parentFolderPath string | ||
) | ||
|
||
err := rows.Scan(&id, &folderPath, &parentFolderPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// ensure folder path is correct | ||
if !strings.HasPrefix(folderPath, parentFolderPath) { | ||
logger.Debugf("folder path %s does not have prefix %s. Correcting...", folderPath, parentFolderPath) | ||
|
||
// get the basename of the zip folder path and append it to the correct path | ||
folderBasename := filepath.Base(folderPath) | ||
correctPath := filepath.Join(parentFolderPath, folderBasename) | ||
|
||
logger.Infof("correcting folder path %s to %s", folderPath, correctPath) | ||
|
||
if _, err := m.db.Exec("UPDATE folders SET path = ? WHERE id = ?", correctPath, id); err != nil { | ||
return fmt.Errorf("error updating folder path %s to %s: %w", folderPath, correctPath, err) | ||
} | ||
} | ||
} | ||
|
||
return rows.Err() | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
sqlite.RegisterPostMigration(52, post52) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- no schema changes |