From 7a54a83c8d2ed42738b7e54cb416545fcff1c515 Mon Sep 17 00:00:00 2001 From: StarFleetCPTN Date: Sun, 6 Apr 2025 23:11:41 -0700 Subject: [PATCH] fix: Improve migration process by handling potential leftover temporary tables - Added logic to drop any existing temporary tables from previous failed migration runs before renaming the current table. - Enhanced error logging to provide warnings if the drop operation fails, ensuring smoother migration execution. https://github.com/StarFleetCPTN/GoMFT/issues/72 --- internal/db/migrations/012_alter_boolean_defaults.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/db/migrations/012_alter_boolean_defaults.go b/internal/db/migrations/012_alter_boolean_defaults.go index cfcbd42..b71d02c 100644 --- a/internal/db/migrations/012_alter_boolean_defaults.go +++ b/internal/db/migrations/012_alter_boolean_defaults.go @@ -178,6 +178,12 @@ func AlterBooleanDefaults() *gormigrate.Migration { fmt.Printf("Recreating table %s...\n", tableName) oldTableName := fmt.Sprintf("_%s_old", tableName) + // Drop the old temp table if it exists from a previous failed run + if err := tx.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", oldTableName)).Error; err != nil { + // Log the error but proceed, as the rename might still work or fail for the intended reason + fmt.Printf("Warning: failed to drop potential leftover table %s: %v\n", oldTableName, err) + } + // Rename old table if err := tx.Exec(fmt.Sprintf("ALTER TABLE %s RENAME TO %s", tableName, oldTableName)).Error; err == nil { fmt.Printf("Renamed %s to %s.\n", tableName, oldTableName)