Skip to content

Commit bc040c4

Browse files
committed
refactor: improved GORM's CreateConstraints to accept more than one constraint name
* Improved GORM's CreateConstraints to accept more than one constraint name * Renamed CreateConstraints as CreateModelsConstraints
1 parent 8c7b870 commit bc040c4

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

gorm/constraint.go

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,75 @@ import (
66
)
77

88
type (
9-
// Constraint struct
10-
Constraint struct {
9+
// ModelConstraints struct
10+
ModelConstraints struct {
1111
model interface{}
12-
field string
12+
names []string
1313
}
1414
)
1515

16-
// NewConstraint creates a new constraint
17-
func NewConstraint(model interface{}, field string) *Constraint {
18-
return &Constraint{
16+
// NewModelConstraints creates a new model constraints
17+
func NewModelConstraints(model interface{}, names ...string) *ModelConstraints {
18+
return &ModelConstraints{
1919
model: model,
20-
field: field,
20+
names: names,
2121
}
2222
}
2323

2424
// HasConstraint checks if a constraint exists
25-
func HasConstraint(database *gorm.DB, constraint *Constraint) bool {
26-
// Check if the database or the constraint is nil
27-
if database == nil || constraint == nil {
25+
func HasConstraint(database *gorm.DB, model interface{}, name string) bool {
26+
// Check if the database or the model is nil
27+
if database == nil || model == nil {
2828
return false
2929
}
3030

3131
// Check if the constraint exists
3232
return database.Migrator().HasConstraint(
33-
constraint.model,
34-
constraint.field,
33+
model,
34+
name,
3535
)
3636
}
3737

38-
// CreateConstraint creates a new constraint
39-
func CreateConstraint(database *gorm.DB, constraint *Constraint) error {
38+
// CreateModelConstraints creates model constraints
39+
func CreateModelConstraints(
40+
database *gorm.DB,
41+
modelConstraints *ModelConstraints,
42+
) (err error) {
4043
// Check if the database or the constraint is nil
4144
if database == nil {
4245
return godatabases.ErrNilDatabase
4346
}
44-
if constraint == nil {
45-
return ErrNilConstraint
47+
if modelConstraints == nil {
48+
return ErrNilModelConstraints
4649
}
4750

48-
// Check if the constraint exists
49-
if HasConstraint(database, constraint) {
50-
return nil
51-
}
51+
for _, name := range modelConstraints.names {
52+
// Check if the constraint exists
53+
if HasConstraint(database, modelConstraints.model, name) {
54+
return nil
55+
}
5256

53-
// Create the constraint
54-
return database.Migrator().CreateConstraint(
55-
constraint.model,
56-
constraint.field,
57-
)
57+
// Create the constraint
58+
if err = database.Migrator().CreateConstraint(
59+
modelConstraints.model,
60+
name,
61+
); err != nil {
62+
return err
63+
}
64+
}
65+
return nil
5866
}
5967

60-
// CreateConstraints creates new constraints
61-
func CreateConstraints(database *gorm.DB, constraints []*Constraint) error {
62-
for _, constraint := range constraints {
63-
if err := CreateConstraint(database, constraint); err != nil {
68+
// CreateModelsConstraints creates models constraints
69+
func CreateModelsConstraints(
70+
database *gorm.DB,
71+
modelsConstraints []*ModelConstraints,
72+
) error {
73+
for _, modelConstraint := range modelsConstraints {
74+
if err := CreateModelConstraints(
75+
database,
76+
modelConstraint,
77+
); err != nil {
6478
return err
6579
}
6680
}

gorm/errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import (
55
)
66

77
var (
8-
ErrNilJoinField = errors.New("join field cannot be nil")
9-
ErrNilConstraint = errors.New("constraint cannot be nil")
8+
ErrNilJoinField = errors.New("join field cannot be nil")
9+
ErrNilModelConstraints = errors.New("model constraints cannot be nil")
1010
)

0 commit comments

Comments
 (0)