From 0560ffac976ee7eec0cadc13b79619a886d83126 Mon Sep 17 00:00:00 2001 From: longshine Date: Wed, 14 Aug 2024 23:57:33 +0800 Subject: [PATCH] feat: add OnConflict=DoNothing on create operations (#243) * add TestAddPolicy * add OnConflict=DoNothing on create * more db * fix test * on conflict clause seems not work in sqlserver --- adapter.go | 13 +++++++------ adapter_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/adapter.go b/adapter.go index 49dc9a5..a32a0df 100755 --- a/adapter.go +++ b/adapter.go @@ -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" ) @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 @@ -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 } diff --git a/adapter_test.go b/adapter_test.go index e244f59..c7a673f 100755 --- a/adapter_test.go +++ b/adapter_test.go @@ -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)