Fix unexpected alter table in auto migration, close #5942, #5943

This commit is contained in:
Jinzhu 2023-01-02 20:52:44 +08:00
parent b0e13d95b4
commit 3d91802b1d
4 changed files with 42 additions and 5 deletions

View File

@ -120,7 +120,10 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
if err != nil { if err != nil {
return err return err
} }
var (
parseIndexes = stmt.Schema.ParseIndexes()
parseCheckConstraints = stmt.Schema.ParseCheckConstraints()
)
for _, dbName := range stmt.Schema.DBNames { for _, dbName := range stmt.Schema.DBNames {
field := stmt.Schema.FieldsByDBName[dbName] field := stmt.Schema.FieldsByDBName[dbName]
var foundColumn gorm.ColumnType var foundColumn gorm.ColumnType
@ -157,7 +160,7 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
} }
} }
for _, chk := range stmt.Schema.ParseCheckConstraints() { for _, chk := range parseCheckConstraints {
if !queryTx.Migrator().HasConstraint(value, chk.Name) { if !queryTx.Migrator().HasConstraint(value, chk.Name) {
if err := execTx.Migrator().CreateConstraint(value, chk.Name); err != nil { if err := execTx.Migrator().CreateConstraint(value, chk.Name); err != nil {
return err return err
@ -165,7 +168,7 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
} }
} }
for _, idx := range stmt.Schema.ParseIndexes() { for _, idx := range parseIndexes {
if !queryTx.Migrator().HasIndex(value, idx.Name) { if !queryTx.Migrator().HasIndex(value, idx.Name) {
if err := execTx.Migrator().CreateIndex(value, idx.Name); err != nil { if err := execTx.Migrator().CreateIndex(value, idx.Name); err != nil {
return err return err
@ -430,7 +433,8 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
realDataType := strings.ToLower(columnType.DatabaseTypeName()) realDataType := strings.ToLower(columnType.DatabaseTypeName())
var ( var (
alterColumn, isSameType bool alterColumn bool
isSameType = fullDataType == realDataType
) )
if !field.PrimaryKey { if !field.PrimaryKey {

View File

@ -129,6 +129,7 @@ func parseFieldIndexes(field *Field) (indexes []Index, err error) {
} }
if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" { if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" {
field.Unique = true
settings["CLASS"] = "UNIQUE" settings["CLASS"] = "UNIQUE"
} }

View File

@ -13,7 +13,7 @@ require (
gorm.io/driver/postgres v1.4.6 gorm.io/driver/postgres v1.4.6
gorm.io/driver/sqlite v1.4.4 gorm.io/driver/sqlite v1.4.4
gorm.io/driver/sqlserver v1.4.1 gorm.io/driver/sqlserver v1.4.1
gorm.io/gorm v1.24.2 gorm.io/gorm v1.24.3
) )
replace gorm.io/gorm => ../ replace gorm.io/gorm => ../

View File

@ -1270,6 +1270,38 @@ func TestMigrateDefaultNullString(t *testing.T) {
AssertEqual(t, ok, false) AssertEqual(t, ok, false)
} }
func TestMigrateMySQLWithCustomizedTypes(t *testing.T) {
if DB.Dialector.Name() != "mysql" {
t.Skip()
}
type MyTable struct {
Def string `gorm:"size:512;index:idx_def,unique"`
Abc string `gorm:"size:65000000"`
}
DB.Migrator().DropTable("my_tables")
sql := "CREATE TABLE `my_tables` (`def` varchar(512),`abc` longtext,UNIQUE INDEX `idx_def` (`def`))"
if err := DB.Exec(sql).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
session := DB.Session(&gorm.Session{Logger: Tracer{
Logger: DB.Config.Logger,
Test: func(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
sql, _ := fc()
if strings.HasPrefix(sql, "ALTER TABLE") {
t.Errorf("shouldn't execute: sql=%s", sql)
}
},
}})
if err := session.AutoMigrate(&MyTable{}); err != nil {
t.Errorf("Failed, got error: %v", err)
}
}
func TestMigrateIgnoreRelations(t *testing.T) { func TestMigrateIgnoreRelations(t *testing.T) {
type RelationModel1 struct { type RelationModel1 struct {
ID uint ID uint