Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(MigrateColumn):declared different type without length #5465

Merged
merged 1 commit into from Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions migrator/migrator.go
Expand Up @@ -15,7 +15,6 @@ import (
)

var (
regRealDataType = regexp.MustCompile(`[^\d](\d+)[^\d]?`)
regFullDataType = regexp.MustCompile(`[^\d]*(\d+)[^\d]?`)
)

Expand Down Expand Up @@ -404,21 +403,25 @@ func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error
// MigrateColumn migrate column
func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnType gorm.ColumnType) error {
// found, smart migrate
fullDataType := strings.ToLower(m.DB.Migrator().FullDataTypeOf(field).SQL)
fullDataType := strings.TrimSpace(strings.ToLower(m.DB.Migrator().FullDataTypeOf(field).SQL))
realDataType := strings.ToLower(columnType.DatabaseTypeName())

alterColumn := false

// check type
if !field.PrimaryKey && !strings.HasPrefix(fullDataType, realDataType) {
alterColumn = true
}

// check size
if length, ok := columnType.Length(); length != int64(field.Size) {
if length > 0 && field.Size > 0 {
alterColumn = true
} else {
// has size in data type and not equal
// Since the following code is frequently called in the for loop, reg optimization is needed here
matches := regRealDataType.FindAllStringSubmatch(realDataType, -1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove regRealDataType because it is not match anything after #5088

matches2 := regFullDataType.FindAllStringSubmatch(fullDataType, -1)
if (len(matches) == 1 && matches[0][1] != fmt.Sprint(field.Size) || !field.PrimaryKey) &&
if !field.PrimaryKey &&
(len(matches2) == 1 && matches2[0][1] != fmt.Sprint(length) && ok) {
alterColumn = true
}
Expand Down
39 changes: 39 additions & 0 deletions tests/migrate_test.go
Expand Up @@ -884,3 +884,42 @@ func TestInvalidCachedPlan(t *testing.T) {
t.Errorf("AutoMigrate err:%v", err)
}
}

func TestDifferentTypeWithoutDeclaredLength(t *testing.T) {
type DiffType struct {
ID uint
Name string `gorm:"type:varchar(20)"`
}

type DiffType1 struct {
ID uint
Name string `gorm:"type:text"`
}

var err error
DB.Migrator().DropTable(&DiffType{})

err = DB.AutoMigrate(&DiffType{})
if err != nil {
t.Errorf("AutoMigrate err:%v", err)
}

ct, err := findColumnType(&DiffType{}, "name")
if err != nil {
t.Errorf("findColumnType err:%v", err)
}

AssertEqual(t, "varchar", strings.ToLower(ct.DatabaseTypeName()))

err = DB.Table("diff_types").AutoMigrate(&DiffType1{})
if err != nil {
t.Errorf("AutoMigrate err:%v", err)
}

ct, err = findColumnType(&DiffType{}, "name")
if err != nil {
t.Errorf("findColumnType err:%v", err)
}

AssertEqual(t, "text", strings.ToLower(ct.DatabaseTypeName()))
}