feat: avoid using the same name field and if..else optimization adjustment
This commit is contained in:
parent
10338441ba
commit
c27b80ab28
@ -119,16 +119,13 @@ 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, chk := range stmt.Schema.ParseCheckConstraints() {
|
for _, chk := range stmt.Schema.ParseCheckConstraints() {
|
||||||
if !tx.Migrator().HasConstraint(value, chk.Name) {
|
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 {
|
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)
|
||||||
|
if f == nil {
|
||||||
|
return fmt.Errorf("failed to look up field with name: %s", field)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !f.IgnoreMigration {
|
||||||
return m.DB.Exec(
|
return m.DB.Exec(
|
||||||
"ALTER TABLE ? ADD ? ?",
|
"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
|
).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
return fmt.Errorf("failed to look up field with name: %s", field)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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() {
|
|
||||||
v = v.Elem()
|
|
||||||
} else {
|
|
||||||
return nil, true
|
return nil, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !v.IsNil() {
|
||||||
|
v = v.Elem()
|
||||||
} else {
|
} else {
|
||||||
return nil, true
|
return nil, true
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
@ -178,7 +178,8 @@ 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))
|
columns := make([]clause.Column, len(foreignKeys))
|
||||||
for idx, key := range foreignKeys {
|
for idx, key := range foreignKeys {
|
||||||
columns[idx] = clause.Column{Table: table, Name: key}
|
columns[idx] = clause.Column{Table: table, Name: key}
|
||||||
@ -187,9 +188,9 @@ func ToQueryValues(table string, foreignKeys []string, foreignValues [][]interfa
|
|||||||
for idx, r := range foreignValues {
|
for idx, r := range foreignValues {
|
||||||
queryValues[idx] = r
|
queryValues[idx] = r
|
||||||
}
|
}
|
||||||
|
|
||||||
return columns, queryValues
|
return columns, queryValues
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
type embeddedNamer struct {
|
type embeddedNamer struct {
|
||||||
Table string
|
Table string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user