Skip to content

Commit

Permalink
feat: add OnConflict=DoNothing on create operations (#243)
Browse files Browse the repository at this point in the history
* add TestAddPolicy

* add OnConflict=DoNothing on create

* more db

* fix test

* on conflict clause seems not work in sqlserver
  • Loading branch information
longshine authored Aug 14, 2024
1 parent 6a0d216 commit 0560ffa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
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

0 comments on commit 0560ffa

Please sign in to comment.