Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto migrate tries to delete non-existent unique constraint #7135

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@

for _, rel := range stmt.Schema.Relationships.Relations {
if constraint := rel.ParseConstraint(); constraint != nil && constraint.Name == name {
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for relation 0 %v", constraint))

Check failure on line 713 in migrator/migrator.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 S1038: should use fmt.Printf instead of fmt.Println(fmt.Sprintf(...)) (but don't forget the newline) (gosimple) Raw Output: migrator/migrator.go:713:4: S1038: should use fmt.Printf instead of fmt.Println(fmt.Sprintf(...)) (but don't forget the newline) (gosimple) fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for relation 0 %v", constraint)) ^
return constraint, getTable(rel)
}
}
Expand All @@ -718,19 +719,22 @@
for k := range checkConstraints {
if checkConstraints[k].Field == field {
v := checkConstraints[k]
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for check %v", &v))

Check failure on line 722 in migrator/migrator.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 S1038: should use fmt.Printf instead of fmt.Println(fmt.Sprintf(...)) (but don't forget the newline) (gosimple) Raw Output: migrator/migrator.go:722:5: S1038: should use fmt.Printf instead of fmt.Println(fmt.Sprintf(...)) (but don't forget the newline) (gosimple) fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for check %v", &v)) ^
return &v, stmt.Table
}
}

for k := range uniqueConstraints {
if uniqueConstraints[k].Field == field {
v := uniqueConstraints[k]
fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for unique %v", &v))

Check failure on line 730 in migrator/migrator.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 S1038: should use fmt.Printf instead of fmt.Println(fmt.Sprintf(...)) (but don't forget the newline) (gosimple) Raw Output: migrator/migrator.go:730:5: S1038: should use fmt.Printf instead of fmt.Println(fmt.Sprintf(...)) (but don't forget the newline) (gosimple) fmt.Println(fmt.Sprintf("SHREWS --Out of Guess for unique %v", &v)) ^
return &v, stmt.Table
}
}

for _, rel := range stmt.Schema.Relationships.Relations {
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)
}
}
Expand All @@ -757,7 +761,11 @@

// DropConstraint drop constraint
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 {
if !m.HasConstraint(value, name) {
return nil
}
constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name)
if constraint != nil {
name = constraint.GetName()
Expand Down
4 changes: 4 additions & 0 deletions tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,10 @@ func TestMigrateConstraint(t *testing.T) {
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 {
t.Fatalf("failed to drop constraint %v, got error %v", name, err)
}
Expand Down
Loading