From 7894d0fc1f9232f590fbf6381aeca97f86056a95 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:21:31 +1100 Subject: [PATCH] Fix unique folder migration error (#4170) --- pkg/sqlite/migrations/52_postmigrate.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/sqlite/migrations/52_postmigrate.go b/pkg/sqlite/migrations/52_postmigrate.go index 3db07fde8c5..f173d808704 100644 --- a/pkg/sqlite/migrations/52_postmigrate.go +++ b/pkg/sqlite/migrations/52_postmigrate.go @@ -2,6 +2,8 @@ package migrations import ( "context" + "database/sql" + "errors" "fmt" "path/filepath" "strings" @@ -60,6 +62,19 @@ func (m *schema52Migrator) migrate(ctx context.Context) error { logger.Infof("correcting folder path %s to %s", folderPath, correctPath) + // ensure the correct path is unique + var v int + isEmptyErr := m.db.Get(&v, "SELECT 1 FROM folders WHERE path = ?", correctPath) + if isEmptyErr != nil && !errors.Is(isEmptyErr, sql.ErrNoRows) { + return fmt.Errorf("error checking if correct path %s is unique: %w", correctPath, isEmptyErr) + } + + if isEmptyErr == nil { + // correct path is not unique, log and skip + logger.Warnf("correct path %s already exists, skipping...", correctPath) + continue + } + 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) }