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

View File

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

View File

@ -45,9 +45,9 @@ type Schema struct {
func (schema Schema) String() string {
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 {

View File

@ -178,7 +178,8 @@ func ToQueryValues(table string, foreignKeys []string, foreignValues [][]interfa
}
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}
@ -187,8 +188,8 @@ func ToQueryValues(table string, foreignKeys []string, foreignValues [][]interfa
for idx, r := range foreignValues {
queryValues[idx] = r
}
return columns, queryValues
}
}
type embeddedNamer struct {