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
This commit is contained in:
StarFleetCPTN
2025-04-06 23:11:41 -07:00
parent c52267bbb6
commit 7a54a83c8d
@@ -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)