Skip to content

Commit 2ba612e

Browse files
authored
Add field tag to ignore migration (#4028)
* Add field tag to ignore migration * Fix null value with space * refactor migration tag
1 parent 883c32e commit 2ba612e

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ TODO*
22
documents
33
coverage.txt
44
_book
5+
.idea

migrator/migrator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
396396
}
397397
}
398398

399-
if alterColumn {
399+
if alterColumn && !field.IgnoreMigration {
400400
return m.DB.Migrator().AlterColumn(value, field.Name)
401401
}
402402

schema/field.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type Field struct {
7070
ReflectValueOf func(reflect.Value) reflect.Value
7171
ValueOf func(reflect.Value) (value interface{}, zero bool)
7272
Set func(reflect.Value, interface{}) error
73+
IgnoreMigration bool
7374
}
7475

7576
func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
@@ -189,6 +190,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
189190
}
190191

191192
// default value is function or null or blank (primary keys)
193+
field.DefaultValue = strings.TrimSpace(field.DefaultValue)
192194
skipParseDefaultValue := strings.Contains(field.DefaultValue, "(") &&
193195
strings.Contains(field.DefaultValue, ")") || strings.ToLower(field.DefaultValue) == "null" || field.DefaultValue == ""
194196
switch reflect.Indirect(fieldValue).Kind() {
@@ -295,11 +297,23 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
295297
}
296298

297299
// setup permission
298-
if _, ok := field.TagSettings["-"]; ok {
299-
field.Creatable = false
300-
field.Updatable = false
301-
field.Readable = false
302-
field.DataType = ""
300+
if val, ok := field.TagSettings["-"]; ok {
301+
val = strings.ToLower(strings.TrimSpace(val))
302+
switch val {
303+
case "-":
304+
field.Creatable = false
305+
field.Updatable = false
306+
field.Readable = false
307+
field.DataType = ""
308+
case "all":
309+
field.Creatable = false
310+
field.Updatable = false
311+
field.Readable = false
312+
field.DataType = ""
313+
field.IgnoreMigration = true
314+
case "migration":
315+
field.IgnoreMigration = true
316+
}
303317
}
304318

305319
if v, ok := field.TagSettings["->"]; ok {

tests/migrate_test.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ func TestSmartMigrateColumn(t *testing.T) {
6262
DB.AutoMigrate(&UserMigrateColumn{})
6363

6464
type UserMigrateColumn2 struct {
65-
ID uint
66-
Name string `gorm:"size:128"`
67-
Salary float64 `gorm:"precision:2"`
68-
Birthday time.Time `gorm:"precision:2"`
65+
ID uint
66+
Name string `gorm:"size:128"`
67+
Salary float64 `gorm:"precision:2"`
68+
Birthday time.Time `gorm:"precision:2"`
69+
NameIgnoreMigration string `gorm:"size:100"`
6970
}
7071

7172
if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn2{}); err != nil {
@@ -95,10 +96,11 @@ func TestSmartMigrateColumn(t *testing.T) {
9596
}
9697

9798
type UserMigrateColumn3 struct {
98-
ID uint
99-
Name string `gorm:"size:256"`
100-
Salary float64 `gorm:"precision:3"`
101-
Birthday time.Time `gorm:"precision:3"`
99+
ID uint
100+
Name string `gorm:"size:256"`
101+
Salary float64 `gorm:"precision:3"`
102+
Birthday time.Time `gorm:"precision:3"`
103+
NameIgnoreMigration string `gorm:"size:128;-:migration"`
102104
}
103105

104106
if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn3{}); err != nil {
@@ -124,6 +126,10 @@ func TestSmartMigrateColumn(t *testing.T) {
124126
if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 3 {
125127
t.Fatalf("birthday's precision should be 2, but got %v", precision)
126128
}
129+
case "name_ignore_migration":
130+
if length, _ := columnType.Length(); (fullSupported || length != 0) && length != 100 {
131+
t.Fatalf("name_ignore_migration's length should still be 100 but got %v", length)
132+
}
127133
}
128134
}
129135

0 commit comments

Comments
 (0)