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

feat: add OnConflict=DoNothing on create operations #243

Merged
merged 5 commits into from
Aug 14, 2024
Merged
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
13 changes: 7 additions & 6 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"gorm.io/driver/postgres"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"gorm.io/plugin/dbresolver"
)
Expand Down Expand Up @@ -626,7 +627,7 @@ func (a *Adapter) SavePolicyCtx(ctx context.Context, model model.Model) error {
for _, rule := range ast.Policy {
lines = append(lines, a.savePolicyLine(ptype, rule))
if len(lines) > flushEvery {
if err := tx.Create(&lines).Error; err != nil {
if err := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&lines).Error; err != nil {
tx.Rollback()
return err
}
Expand All @@ -639,7 +640,7 @@ func (a *Adapter) SavePolicyCtx(ctx context.Context, model model.Model) error {
for _, rule := range ast.Policy {
lines = append(lines, a.savePolicyLine(ptype, rule))
if len(lines) > flushEvery {
if err := tx.Create(&lines).Error; err != nil {
if err := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&lines).Error; err != nil {
tx.Rollback()
return err
}
Expand All @@ -648,7 +649,7 @@ func (a *Adapter) SavePolicyCtx(ctx context.Context, model model.Model) error {
}
}
if len(lines) > 0 {
if err := tx.Create(&lines).Error; err != nil {
if err := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&lines).Error; err != nil {
tx.Rollback()
return err
}
Expand All @@ -666,7 +667,7 @@ func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error {
// AddPolicyCtx adds a policy rule to the storage.
func (a *Adapter) AddPolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error {
line := a.savePolicyLine(ptype, rule)
err := a.db.WithContext(ctx).Create(&line).Error
err := a.db.WithContext(ctx).Clauses(clause.OnConflict{DoNothing: true}).Create(&line).Error
return err
}

Expand All @@ -689,7 +690,7 @@ func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error
line := a.savePolicyLine(ptype, rule)
lines = append(lines, line)
}
return a.db.Create(&lines).Error
return a.db.Clauses(clause.OnConflict{DoNothing: true}).Create(&lines).Error
}

// Transaction perform a set of operations within a transaction
Expand Down Expand Up @@ -930,7 +931,7 @@ func (a *Adapter) UpdateFilteredPolicies(sec string, ptype string, newPolicies [
return nil, err
}
for i := range newP {
if err := tx.Create(&newP[i]).Error; err != nil {
if err := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&newP[i]).Error; err != nil {
tx.Rollback()
return nil, err
}
Expand Down
40 changes: 40 additions & 0 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,46 @@ func TestAddPolicies(t *testing.T) {
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"jack", "data1", "read"}, {"jack2", "data1", "read"}})
}

func TestAddPolicy(t *testing.T) {
tests := []struct {
driverName string
dataSourceName string
params []any
}{
{"mysql", "root:@tcp(127.0.0.1:3306)/", []any{"casbin", "casbin_rule"}},
{"postgres", "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable", nil},
// {"sqlserver", "sqlserver://sa:SqlServer123@localhost:1433", []any{"master", "casbin_rule"}},
}

for _, test := range tests {
test := test
t.Run(test.driverName, func(t *testing.T) {
t.Parallel()
a := initAdapter(t, test.driverName, test.dataSourceName, test.params...)
e1, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
e2, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)

policy := []string{"alice", "data1", "TestAddPolicy"}

ok, err := e1.AddPolicy(policy)
if err != nil {
t.Errorf("e1.AddPolicy() got err %v", err)
}
if !ok {
t.Errorf("e1.AddPolicy() got false, want true")
}

ok, err = e2.AddPolicy(policy)
if err != nil {
t.Errorf("e2.AddPolicy() got err %v", err)
}
if !ok {
t.Errorf("e2.AddPolicy() got false, want true")
}
})
}
}

func TestTransaction(t *testing.T) {
a := initAdapter(t, "mysql", "root:@tcp(127.0.0.1:3306)/", "casbin", "casbin_rule")
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
Expand Down
Loading