Skip to content

Commit

Permalink
Fix unexpected alter table in auto migration, close #5942, #5943
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jan 2, 2023
1 parent b0e13d9 commit 3d91802
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
12 changes: 8 additions & 4 deletions migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
if err != nil {
return err
}

var (
parseIndexes = stmt.Schema.ParseIndexes()
parseCheckConstraints = stmt.Schema.ParseCheckConstraints()
)
for _, dbName := range stmt.Schema.DBNames {
field := stmt.Schema.FieldsByDBName[dbName]
var foundColumn gorm.ColumnType
Expand Down Expand Up @@ -157,15 +160,15 @@ 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 err := execTx.Migrator().CreateConstraint(value, chk.Name); err != nil {
return err
}
}
}

for _, idx := range stmt.Schema.ParseIndexes() {
for _, idx := range parseIndexes {
if !queryTx.Migrator().HasIndex(value, idx.Name) {
if err := execTx.Migrator().CreateIndex(value, idx.Name); err != nil {
return err
Expand Down Expand Up @@ -430,7 +433,8 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
realDataType := strings.ToLower(columnType.DatabaseTypeName())

var (
alterColumn, isSameType bool
alterColumn bool
isSameType = fullDataType == realDataType
)

if !field.PrimaryKey {
Expand Down
1 change: 1 addition & 0 deletions schema/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func parseFieldIndexes(field *Field) (indexes []Index, err error) {
}

if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" {
field.Unique = true
settings["CLASS"] = "UNIQUE"
}

Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
gorm.io/driver/postgres v1.4.6
gorm.io/driver/sqlite v1.4.4
gorm.io/driver/sqlserver v1.4.1
gorm.io/gorm v1.24.2
gorm.io/gorm v1.24.3
)

replace gorm.io/gorm => ../
32 changes: 32 additions & 0 deletions tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,38 @@ func TestMigrateDefaultNullString(t *testing.T) {
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) {
type RelationModel1 struct {
ID uint
Expand Down

0 comments on commit 3d91802

Please sign in to comment.