Add smart auto migrate tests
This commit is contained in:
		
							parent
							
								
									6d90d09cb8
								
							
						
					
					
						commit
						0aef8acc11
					
				| @ -370,9 +370,9 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy | |||||||
| 			alterColumn = true | 			alterColumn = true | ||||||
| 		} else { | 		} else { | ||||||
| 			// has size in data type and not equal
 | 			// has size in data type and not equal
 | ||||||
| 			matches := regexp.MustCompile(`[^\d](\d+)[^\d]`).FindAllStringSubmatch(realDataType, -1) | 			matches := regexp.MustCompile(`[^\d](\d+)[^\d]?`).FindAllStringSubmatch(realDataType, -1) | ||||||
| 			matches2 := regexp.MustCompile(`[^\d]*(\d+)[^\d]`).FindAllStringSubmatch(fullDataType, -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)) { | 			if (len(matches) == 1 && matches[0][1] != fmt.Sprint(field.Size) || !field.PrimaryKey) && (len(matches2) == 1 && matches2[0][1] != fmt.Sprint(length)) { | ||||||
| 				alterColumn = true | 				alterColumn = true | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  | |||||||
| @ -6,11 +6,11 @@ require ( | |||||||
| 	github.com/google/uuid v1.1.1 | 	github.com/google/uuid v1.1.1 | ||||||
| 	github.com/jinzhu/now v1.1.1 | 	github.com/jinzhu/now v1.1.1 | ||||||
| 	github.com/lib/pq v1.6.0 | 	github.com/lib/pq v1.6.0 | ||||||
| 	gorm.io/driver/mysql v1.0.2 | 	gorm.io/driver/mysql v1.0.3 | ||||||
| 	gorm.io/driver/postgres v1.0.4 | 	gorm.io/driver/postgres v1.0.5 | ||||||
| 	gorm.io/driver/sqlite v1.1.3 | 	gorm.io/driver/sqlite v1.1.3 | ||||||
| 	gorm.io/driver/sqlserver v1.0.5 | 	gorm.io/driver/sqlserver v1.0.5 | ||||||
| 	gorm.io/gorm v1.20.2 | 	gorm.io/gorm v1.20.4 | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| replace gorm.io/gorm => ../ | replace gorm.io/gorm => ../ | ||||||
|  | |||||||
| @ -48,11 +48,13 @@ func TestMigrate(t *testing.T) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestSmartMigrateColumn(t *testing.T) { | func TestSmartMigrateColumn(t *testing.T) { | ||||||
|  | 	fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()] | ||||||
|  | 
 | ||||||
| 	type UserMigrateColumn struct { | 	type UserMigrateColumn struct { | ||||||
| 		ID       uint | 		ID       uint | ||||||
| 		Name     string | 		Name     string | ||||||
| 		Salary   float64 | 		Salary   float64 | ||||||
| 		Birthday time.Time | 		Birthday time.Time `gorm:"precision:4"` | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	DB.Migrator().DropTable(&UserMigrateColumn{}) | 	DB.Migrator().DropTable(&UserMigrateColumn{}) | ||||||
| @ -78,15 +80,15 @@ func TestSmartMigrateColumn(t *testing.T) { | |||||||
| 	for _, columnType := range columnTypes { | 	for _, columnType := range columnTypes { | ||||||
| 		switch columnType.Name() { | 		switch columnType.Name() { | ||||||
| 		case "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) | 				t.Fatalf("name's length should be 128, but got %v", length) | ||||||
| 			} | 			} | ||||||
| 		case "salary": | 		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) | 				t.Fatalf("salary's precision should be 2, but got %v %v", precision, o) | ||||||
| 			} | 			} | ||||||
| 		case "birthday": | 		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) | 				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 { | 	for _, columnType := range columnTypes { | ||||||
| 		switch columnType.Name() { | 		switch columnType.Name() { | ||||||
| 		case "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) | 				t.Fatalf("name's length should be 128, but got %v", length) | ||||||
| 			} | 			} | ||||||
| 		case "salary": | 		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) | 				t.Fatalf("salary's precision should be 2, but got %v", precision) | ||||||
| 			} | 			} | ||||||
| 		case "birthday": | 		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) | 				t.Fatalf("birthday's precision should be 2, but got %v", precision) | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu