refactor migration tag

This commit is contained in:
yrong 2021-02-09 15:48:39 +08:00
parent 4a1096bcff
commit 8771f3c27c
2 changed files with 31 additions and 17 deletions

View File

@ -189,10 +189,6 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
field.Comment = val field.Comment = val
} }
if _, ok := field.TagSettings["IGNOREMIGRATION"]; ok {
field.IgnoreMigration = true
}
// 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) field.DefaultValue = strings.TrimSpace(field.DefaultValue)
skipParseDefaultValue := strings.Contains(field.DefaultValue, "(") && skipParseDefaultValue := strings.Contains(field.DefaultValue, "(") &&
@ -301,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 {
val = strings.ToLower(strings.TrimSpace(val))
switch val {
case "-":
field.Creatable = false field.Creatable = false
field.Updatable = false field.Updatable = false
field.Readable = false field.Readable = false
field.DataType = "" 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 {

View File

@ -66,6 +66,7 @@ func TestSmartMigrateColumn(t *testing.T) {
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 {
@ -99,6 +100,7 @@ func TestSmartMigrateColumn(t *testing.T) {
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)
}
} }
} }