Compare commits

...

8 Commits

Author SHA1 Message Date
David Shrewsberry 912df28cb2
Merge 003bcc3a35 into 49bbaa637f 2024-11-14 17:47:29 +08:00
Dave Shrewsberry 003bcc3a35 more tracing 2024-08-01 17:26:20 -04:00
Dave Shrewsberry 3198b7c219 more tracing 2024-08-01 17:23:39 -04:00
Dave Shrewsberry 38fb45fea8 debug 2024-08-01 17:01:58 -04:00
Dave Shrewsberry e483cd9993 put check back in drop 2024-08-01 16:31:37 -04:00
Dave Shrewsberry 79e73f0d7d move to the caller 2024-08-01 16:17:38 -04:00
Dave Shrewsberry e55b2ba312 if exists doesn't work for mysql, so have to verify constraint exists. 2024-08-01 14:32:27 -04:00
Dave Shrewsberry 92bc52270e use IF EXISTS 2024-08-01 13:45:11 -04:00
2 changed files with 12 additions and 0 deletions

View File

@ -710,6 +710,7 @@ func (m Migrator) GuessConstraintInterfaceAndTable(stmt *gorm.Statement, name st
for _, rel := range stmt.Schema.Relationships.Relations { for _, rel := range stmt.Schema.Relationships.Relations {
if constraint := rel.ParseConstraint(); constraint != nil && constraint.Name == name { if constraint := rel.ParseConstraint(); constraint != nil && constraint.Name == name {
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for relation 0 %v", constraint))
return constraint, getTable(rel) return constraint, getTable(rel)
} }
} }
@ -718,6 +719,7 @@ func (m Migrator) GuessConstraintInterfaceAndTable(stmt *gorm.Statement, name st
for k := range checkConstraints { for k := range checkConstraints {
if checkConstraints[k].Field == field { if checkConstraints[k].Field == field {
v := checkConstraints[k] v := checkConstraints[k]
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for check %v", &v))
return &v, stmt.Table return &v, stmt.Table
} }
} }
@ -725,12 +727,14 @@ func (m Migrator) GuessConstraintInterfaceAndTable(stmt *gorm.Statement, name st
for k := range uniqueConstraints { for k := range uniqueConstraints {
if uniqueConstraints[k].Field == field { if uniqueConstraints[k].Field == field {
v := uniqueConstraints[k] v := uniqueConstraints[k]
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for unique %v", &v))
return &v, stmt.Table return &v, stmt.Table
} }
} }
for _, rel := range stmt.Schema.Relationships.Relations { for _, rel := range stmt.Schema.Relationships.Relations {
if constraint := rel.ParseConstraint(); constraint != nil && rel.Field == field { if constraint := rel.ParseConstraint(); constraint != nil && rel.Field == field {
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for relation %v", constraint))
return constraint, getTable(rel) return constraint, getTable(rel)
} }
} }
@ -757,7 +761,11 @@ func (m Migrator) CreateConstraint(value interface{}, name string) error {
// DropConstraint drop constraint // DropConstraint drop constraint
func (m Migrator) DropConstraint(value interface{}, name string) error { func (m Migrator) DropConstraint(value interface{}, name string) error {
fmt.Println(fmt.Sprintf("SHREWS --In Drop %q", name))
return m.RunWithValue(value, func(stmt *gorm.Statement) error { return m.RunWithValue(value, func(stmt *gorm.Statement) error {
if !m.HasConstraint(value, name) {
return nil
}
constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name) constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name)
if constraint != nil { if constraint != nil {
name = constraint.GetName() name = constraint.GetName()

View File

@ -723,6 +723,10 @@ func TestMigrateConstraint(t *testing.T) {
DB.Migrator().CreateConstraint(&User{}, name) DB.Migrator().CreateConstraint(&User{}, name)
} }
if !DB.Migrator().HasConstraint(&User{}, name) {
t.Fatalf("failed to create constraint %v", name)
}
if err := DB.Migrator().DropConstraint(&User{}, name); err != nil { if err := DB.Migrator().DropConstraint(&User{}, name); err != nil {
t.Fatalf("failed to drop constraint %v, got error %v", name, err) t.Fatalf("failed to drop constraint %v, got error %v", name, err)
} }