Add field tag to ignore migration (#4028)
* Add field tag to ignore migration * Fix null value with space * refactor migration tag
This commit is contained in:
		
							parent
							
								
									883c32e59a
								
							
						
					
					
						commit
						2ba612e805
					
				
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -2,3 +2,4 @@ TODO* | |||||||
| documents | documents | ||||||
| coverage.txt | coverage.txt | ||||||
| _book | _book | ||||||
|  | .idea | ||||||
|  | |||||||
| @ -396,7 +396,7 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if alterColumn { | 	if alterColumn && !field.IgnoreMigration { | ||||||
| 		return m.DB.Migrator().AlterColumn(value, field.Name) | 		return m.DB.Migrator().AlterColumn(value, field.Name) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -70,6 +70,7 @@ type Field struct { | |||||||
| 	ReflectValueOf         func(reflect.Value) reflect.Value | 	ReflectValueOf         func(reflect.Value) reflect.Value | ||||||
| 	ValueOf                func(reflect.Value) (value interface{}, zero bool) | 	ValueOf                func(reflect.Value) (value interface{}, zero bool) | ||||||
| 	Set                    func(reflect.Value, interface{}) error | 	Set                    func(reflect.Value, interface{}) error | ||||||
|  | 	IgnoreMigration        bool | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { | func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { | ||||||
| @ -189,6 +190,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// default value is function or null or blank (primary keys)
 | 	// default value is function or null or blank (primary keys)
 | ||||||
|  | 	field.DefaultValue = strings.TrimSpace(field.DefaultValue) | ||||||
| 	skipParseDefaultValue := strings.Contains(field.DefaultValue, "(") && | 	skipParseDefaultValue := strings.Contains(field.DefaultValue, "(") && | ||||||
| 		strings.Contains(field.DefaultValue, ")") || strings.ToLower(field.DefaultValue) == "null" || field.DefaultValue == "" | 		strings.Contains(field.DefaultValue, ")") || strings.ToLower(field.DefaultValue) == "null" || field.DefaultValue == "" | ||||||
| 	switch reflect.Indirect(fieldValue).Kind() { | 	switch reflect.Indirect(fieldValue).Kind() { | ||||||
| @ -295,11 +297,23 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// setup permission
 | 	// setup permission
 | ||||||
| 	if _, ok := field.TagSettings["-"]; ok { | 	if val, ok := field.TagSettings["-"]; ok { | ||||||
| 		field.Creatable = false | 		val = strings.ToLower(strings.TrimSpace(val)) | ||||||
| 		field.Updatable = false | 		switch val { | ||||||
| 		field.Readable = false | 		case "-": | ||||||
| 		field.DataType = "" | 			field.Creatable = false | ||||||
|  | 			field.Updatable = false | ||||||
|  | 			field.Readable = false | ||||||
|  | 			field.DataType = "" | ||||||
|  | 		case "all": | ||||||
|  | 			field.Creatable = false | ||||||
|  | 			field.Updatable = false | ||||||
|  | 			field.Readable = false | ||||||
|  | 			field.DataType = "" | ||||||
|  | 			field.IgnoreMigration = true | ||||||
|  | 		case "migration": | ||||||
|  | 			field.IgnoreMigration = true | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if v, ok := field.TagSettings["->"]; ok { | 	if v, ok := field.TagSettings["->"]; ok { | ||||||
|  | |||||||
| @ -62,10 +62,11 @@ func TestSmartMigrateColumn(t *testing.T) { | |||||||
| 	DB.AutoMigrate(&UserMigrateColumn{}) | 	DB.AutoMigrate(&UserMigrateColumn{}) | ||||||
| 
 | 
 | ||||||
| 	type UserMigrateColumn2 struct { | 	type UserMigrateColumn2 struct { | ||||||
| 		ID       uint | 		ID                  uint | ||||||
| 		Name     string    `gorm:"size:128"` | 		Name                string    `gorm:"size:128"` | ||||||
| 		Salary   float64   `gorm:"precision:2"` | 		Salary              float64   `gorm:"precision:2"` | ||||||
| 		Birthday time.Time `gorm:"precision:2"` | 		Birthday            time.Time `gorm:"precision:2"` | ||||||
|  | 		NameIgnoreMigration string    `gorm:"size:100"` | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn2{}); err != nil { | 	if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn2{}); err != nil { | ||||||
| @ -95,10 +96,11 @@ func TestSmartMigrateColumn(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	type UserMigrateColumn3 struct { | 	type UserMigrateColumn3 struct { | ||||||
| 		ID       uint | 		ID                  uint | ||||||
| 		Name     string    `gorm:"size:256"` | 		Name                string    `gorm:"size:256"` | ||||||
| 		Salary   float64   `gorm:"precision:3"` | 		Salary              float64   `gorm:"precision:3"` | ||||||
| 		Birthday time.Time `gorm:"precision:3"` | 		Birthday            time.Time `gorm:"precision:3"` | ||||||
|  | 		NameIgnoreMigration string    `gorm:"size:128;-:migration"` | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn3{}); err != nil { | 	if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn3{}); err != nil { | ||||||
| @ -124,6 +126,10 @@ func TestSmartMigrateColumn(t *testing.T) { | |||||||
| 			if precision, _, _ := columnType.DecimalSize(); (fullSupported || 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) | ||||||
| 			} | 			} | ||||||
|  | 		case "name_ignore_migration": | ||||||
|  | 			if length, _ := columnType.Length(); (fullSupported || length != 0) && length != 100 { | ||||||
|  | 				t.Fatalf("name_ignore_migration's length should still be 100 but got %v", length) | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 yrong1997
						yrong1997