feat: avoid using the same name field and if..else optimization adjustment

This commit is contained in:
daheige 2021-05-27 22:26:19 +08:00
parent 10338441ba
commit c27b80ab28
4 changed files with 36 additions and 34 deletions

View File

@ -119,13 +119,10 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
for _, rel := range stmt.Schema.Relationships.Relations { for _, rel := range stmt.Schema.Relationships.Relations {
if !m.DB.Config.DisableForeignKeyConstraintWhenMigrating { if !m.DB.Config.DisableForeignKeyConstraintWhenMigrating {
if constraint := rel.ParseConstraint(); constraint != nil { if constraint := rel.ParseConstraint(); constraint != nil &&
if constraint.Schema == stmt.Schema { constraint.Schema == stmt.Schema && !tx.Migrator().HasConstraint(value, constraint.Name) {
if !tx.Migrator().HasConstraint(value, constraint.Name) { if err := tx.Migrator().CreateConstraint(value, constraint.Name); err != nil {
if err := tx.Migrator().CreateConstraint(value, constraint.Name); err != nil { return err
return err
}
}
} }
} }
} }
@ -294,16 +291,20 @@ func (m Migrator) RenameTable(oldName, newName interface{}) error {
func (m Migrator) AddColumn(value interface{}, field string) error { func (m Migrator) AddColumn(value interface{}, field string) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error { return m.RunWithValue(value, func(stmt *gorm.Statement) error {
if field := stmt.Schema.LookUpField(field); field != nil { // avoid using the same name field
if !field.IgnoreMigration { f := stmt.Schema.LookUpField(field)
return m.DB.Exec( if f == nil {
"ALTER TABLE ? ADD ? ?", return fmt.Errorf("failed to look up field with name: %s", field)
m.CurrentTable(stmt), clause.Column{Name: field.DBName}, m.DB.Migrator().FullDataTypeOf(field),
).Error
}
return nil
} }
return fmt.Errorf("failed to look up field with name: %s", field)
if !f.IgnoreMigration {
return m.DB.Exec(
"ALTER TABLE ? ADD ? ?",
m.CurrentTable(stmt), clause.Column{Name: f.DBName}, m.DB.Migrator().FullDataTypeOf(f),
).Error
}
return nil
}) })
} }

View File

@ -423,12 +423,12 @@ func (field *Field) setupValuerAndSetter() {
} else { } else {
v = v.Field(-idx - 1) v = v.Field(-idx - 1)
if v.Type().Elem().Kind() == reflect.Struct { if v.Type().Elem().Kind() != reflect.Struct {
if !v.IsNil() { return nil, true
v = v.Elem() }
} else {
return nil, true if !v.IsNil() {
} v = v.Elem()
} else { } else {
return nil, true return nil, true
} }

View File

@ -45,9 +45,9 @@ type Schema struct {
func (schema Schema) String() string { func (schema Schema) String() string {
if schema.ModelType.Name() == "" { if schema.ModelType.Name() == "" {
return fmt.Sprintf("%v(%v)", schema.Name, schema.Table) return fmt.Sprintf("%s(%s)", schema.Name, schema.Table)
} }
return fmt.Sprintf("%v.%v", schema.ModelType.PkgPath(), schema.ModelType.Name()) return fmt.Sprintf("%s.%s", schema.ModelType.PkgPath(), schema.ModelType.Name())
} }
func (schema Schema) MakeSlice() reflect.Value { func (schema Schema) MakeSlice() reflect.Value {

View File

@ -178,17 +178,18 @@ func ToQueryValues(table string, foreignKeys []string, foreignValues [][]interfa
} }
return clause.Column{Table: table, Name: foreignKeys[0]}, queryValues return clause.Column{Table: table, Name: foreignKeys[0]}, queryValues
} else {
columns := make([]clause.Column, len(foreignKeys))
for idx, key := range foreignKeys {
columns[idx] = clause.Column{Table: table, Name: key}
}
for idx, r := range foreignValues {
queryValues[idx] = r
}
return columns, queryValues
} }
columns := make([]clause.Column, len(foreignKeys))
for idx, key := range foreignKeys {
columns[idx] = clause.Column{Table: table, Name: key}
}
for idx, r := range foreignValues {
queryValues[idx] = r
}
return columns, queryValues
} }
type embeddedNamer struct { type embeddedNamer struct {