diff --git a/gorm.go b/gorm.go index 81b6e2afc..653039b34 100644 --- a/gorm.go +++ b/gorm.go @@ -39,6 +39,8 @@ type Config struct { DisableForeignKeyConstraintWhenMigrating bool // DisableNestedTransaction disable nested transaction DisableNestedTransaction bool + // SafetyAutoMigration disable columns alter when it exists + SafetyAutoMigration bool // AllowGlobalUpdate allow global update AllowGlobalUpdate bool // QueryFields executes the SQL query with all fields of the table diff --git a/migrator/migrator.go b/migrator/migrator.go index 29c0c00c0..91414481e 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -120,6 +120,8 @@ func (m Migrator) AutoMigrate(values ...interface{}) error { if err := tx.Migrator().AddColumn(value, dbName); err != nil { return err } + } else if m.DB.SafetyAutoMigration { + // skip MigrationColumn } else if err := m.DB.Migrator().MigrateColumn(value, field, foundColumn); err != nil { // found, smart migrate return err diff --git a/tests/go.mod b/tests/go.mod index ebebabc0b..b0587e3fe 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -10,7 +10,7 @@ require ( github.com/lib/pq v1.10.7 github.com/mattn/go-sqlite3 v1.14.15 // indirect golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0 // indirect - gorm.io/driver/mysql v1.3.6 + gorm.io/driver/mysql v1.4.7 gorm.io/driver/postgres v1.3.10 gorm.io/driver/sqlite v1.3.6 gorm.io/driver/sqlserver v1.3.2