From 8f4589e535d90cfa7754c0daf7077344c0dee44e Mon Sep 17 00:00:00 2001 From: Matt Hook Date: Sat, 30 Oct 2021 22:32:57 +1300 Subject: [PATCH] fix(migration): bubble up recovered panic in new error EE-1971 (#5997) * fix(migration): bubble up recovered panic in new error EE-1971 * improve code and add comments --- api/bolt/migrate_data.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/api/bolt/migrate_data.go b/api/bolt/migrate_data.go index a7cb3863d..fb44a1b59 100644 --- a/api/bolt/migrate_data.go +++ b/api/bolt/migrate_data.go @@ -18,13 +18,16 @@ const beforePortainerVersionUpgradeBackup = "portainer.db.bak" var migrateLog = plog.NewScopedLog("bolt, migrate") // FailSafeMigrate backup and restore DB if migration fail -func (store *Store) FailSafeMigrate(migrator *migrator.Migrator) error { +func (store *Store) FailSafeMigrate(migrator *migrator.Migrator) (err error) { defer func() { - if err := recover(); err != nil { - migrateLog.Info(fmt.Sprintf("Error during migration, recovering [%v]", err)) + if e := recover(); e != nil { store.Rollback(true) + err = fmt.Errorf("%v", e) } }() + + // !Important: we must use a named return value in the function definition and not a local + // !variable referenced from the closure or else the return value will be incorrectly set return migrator.Migrate() }