From 0aef8acc11c783808d1986e03b5e665f0c60fda4 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 22 Oct 2020 14:00:10 +0800 Subject: [PATCH] Add smart auto migrate tests --- migrator/migrator.go | 6 +++--- tests/go.mod | 6 +++--- tests/migrate_test.go | 16 +++++++++------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/migrator/migrator.go b/migrator/migrator.go index c564cb67..c455a294 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -370,9 +370,9 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy alterColumn = true } else { // has size in data type and not equal - matches := regexp.MustCompile(`[^\d](\d+)[^\d]`).FindAllStringSubmatch(realDataType, -1) - matches2 := regexp.MustCompile(`[^\d]*(\d+)[^\d]`).FindAllStringSubmatch(fullDataType, -1) - if (len(matches) == 1 && matches[0][1] != fmt.Sprint(field.Size)) && (len(matches2) == 1 && matches2[0][1] != fmt.Sprint(length)) { + matches := regexp.MustCompile(`[^\d](\d+)[^\d]?`).FindAllStringSubmatch(realDataType, -1) + matches2 := regexp.MustCompile(`[^\d]*(\d+)[^\d]?`).FindAllStringSubmatch(fullDataType, -1) + if (len(matches) == 1 && matches[0][1] != fmt.Sprint(field.Size) || !field.PrimaryKey) && (len(matches2) == 1 && matches2[0][1] != fmt.Sprint(length)) { alterColumn = true } } diff --git a/tests/go.mod b/tests/go.mod index ddb1773b..3fa011f1 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -6,11 +6,11 @@ require ( github.com/google/uuid v1.1.1 github.com/jinzhu/now v1.1.1 github.com/lib/pq v1.6.0 - gorm.io/driver/mysql v1.0.2 - gorm.io/driver/postgres v1.0.4 + gorm.io/driver/mysql v1.0.3 + gorm.io/driver/postgres v1.0.5 gorm.io/driver/sqlite v1.1.3 gorm.io/driver/sqlserver v1.0.5 - gorm.io/gorm v1.20.2 + gorm.io/gorm v1.20.4 ) replace gorm.io/gorm => ../ diff --git a/tests/migrate_test.go b/tests/migrate_test.go index 4cc8a7c3..275fe634 100644 --- a/tests/migrate_test.go +++ b/tests/migrate_test.go @@ -48,11 +48,13 @@ func TestMigrate(t *testing.T) { } func TestSmartMigrateColumn(t *testing.T) { + fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()] + type UserMigrateColumn struct { ID uint Name string Salary float64 - Birthday time.Time + Birthday time.Time `gorm:"precision:4"` } DB.Migrator().DropTable(&UserMigrateColumn{}) @@ -78,15 +80,15 @@ func TestSmartMigrateColumn(t *testing.T) { for _, columnType := range columnTypes { switch columnType.Name() { case "name": - if length, _ := columnType.Length(); length != 0 && length != 128 { + if length, _ := columnType.Length(); (fullSupported || length != 0) && length != 128 { t.Fatalf("name's length should be 128, but got %v", length) } case "salary": - if precision, o, _ := columnType.DecimalSize(); precision != 0 && precision != 2 { + if precision, o, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 { t.Fatalf("salary's precision should be 2, but got %v %v", precision, o) } case "birthday": - if precision, _, _ := columnType.DecimalSize(); precision != 0 && precision != 2 { + if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 { t.Fatalf("birthday's precision should be 2, but got %v", precision) } } @@ -111,15 +113,15 @@ func TestSmartMigrateColumn(t *testing.T) { for _, columnType := range columnTypes { switch columnType.Name() { case "name": - if length, _ := columnType.Length(); length != 0 && length != 256 { + if length, _ := columnType.Length(); (fullSupported || length != 0) && length != 256 { t.Fatalf("name's length should be 128, but got %v", length) } case "salary": - if precision, _, _ := columnType.DecimalSize(); precision != 0 && precision != 3 { + if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 3 { t.Fatalf("salary's precision should be 2, but got %v", precision) } case "birthday": - if precision, _, _ := columnType.DecimalSize(); precision != 0 && precision != 3 { + if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 3 { t.Fatalf("birthday's precision should be 2, but got %v", precision) } }