Skip to content

feat: fix SavePolicy bug to preserve unique indexes by using DeleteMany instead of Drop#73

Merged
hsluoyz merged 4 commits into
masterfrom
copilot/fix-savepolicy-unique-index-issue
Nov 7, 2025
Merged

feat: fix SavePolicy bug to preserve unique indexes by using DeleteMany instead of Drop#73
hsluoyz merged 4 commits into
masterfrom
copilot/fix-savepolicy-unique-index-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 7, 2025

fix: #72

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@hsluoyz hsluoyz marked this pull request as ready for review November 7, 2025 04:59
@coveralls
Copy link
Copy Markdown

coveralls commented Nov 7, 2025

Coverage Status

coverage: 82.744%. remained the same
when pulling 34640bb on copilot/fix-savepolicy-unique-index-issue
into 843cf7b on master.

Copilot AI and others added 2 commits November 7, 2025 05:02
… of Drop

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix SavePolicy method to preserve unique index during dropTable Fix SavePolicy to preserve unique indexes by using DeleteMany instead of Drop Nov 7, 2025
Copilot AI requested a review from hsluoyz November 7, 2025 05:07
@hsluoyz
Copy link
Copy Markdown
Member

hsluoyz commented Nov 7, 2025

@copilot fix failed CI check:

Run go test -v -coverprofile=./profile.cov ./...
go: downloading github.com/casbin/casbin/v2 v2.71.1
go: downloading go.mongodb.org/mongo-driver/v2 v2.2.1
go: downloading github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible
go: downloading github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78
go: downloading github.com/golang/snappy v1.0.0
go: downloading github.com/klauspost/compress v1.16.7
go: downloading github.com/xdg-go/scram v1.1.2
go: downloading github.com/xdg-go/stringprep v1.0.4
go: downloading golang.org/x/crypto v0.33.0
go: downloading golang.org/x/text v0.22.0
go: downloading golang.org/x/sync v0.11.0
go: downloading github.com/xdg-go/pbkdf2 v1.0.0
=== RUN   TestAdapter
    adapter_test.go:124: Policy:  []
    adapter_test.go:131: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:158: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:176: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:195: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write] [alice data1 write]]
    adapter_test.go:212: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:226: Policy:  [[alice data1 read] [bob data2 write]]
    adapter_test.go:236: Policy:  [[bob data2 write]]
    adapter_test.go:242: Policy:  []
--- PASS: TestAdapter (0.03s)
=== RUN   TestAddPolicies
    adapter_test.go:124: Policy:  []
    adapter_test.go:131: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:258: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:278: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write] [bob data2 read] [alice data2 write] [alice data2 read] [bob data1 write] [bob data1 read]]
    adapter_test.go:304: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
--- PASS: TestAddPolicies (0.02s)
=== RUN   TestDeleteFilteredAdapter
    adapter_test.go:332: Policy:  [[domain1 alice data3 read accept service1] [domain1 alice data3 write accept service2]]
    adapter_test.go:342: Policy:  [[domain1 alice data3 write accept service2]]
    adapter_test.go:351: Policy:  []
--- PASS: TestDeleteFilteredAdapter (0.01s)
=== RUN   TestFilteredAdapter
    adapter_test.go:377: Policy:  [[bob data2 write]]
    adapter_test.go:385: Policy:  [[alice data1 read] [alice data1 write]]
    adapter_test.go:406: Policy:  [[alice data1 read] [data2_admin data2 read]]
--- PASS: TestFilteredAdapter (0.01s)
=== RUN   TestNewAdapterWithInvalidURL
--- PASS: TestNewAdapterWithInvalidURL (0.00s)
=== RUN   TestNewAdapterWithUnknownURL
--- PASS: TestNewAdapterWithUnknownURL (30.00s)
=== RUN   TestNewAdapterWithDatabase
--- PASS: TestNewAdapterWithDatabase (0.01s)
=== RUN   TestNewAdapterWithClientOption
--- PASS: TestNewAdapterWithClientOption (0.01s)
=== RUN   TestNewAdapterWithCollectionName
--- PASS: TestNewAdapterWithCollectionName (0.01s)
=== RUN   TestNewAdapterByDB
--- PASS: TestNewAdapterByDB (0.00s)
=== RUN   TestUpdatePolicy
    adapter_test.go:124: Policy:  []
    adapter_test.go:131: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:507: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:525: Policy:  [[alice data1 write] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:535: Policy:  [[alice data1 read] [bob data2 read] [data2_admin data2 read] [data2_admin data2 write]]
--- PASS: TestUpdatePolicy (0.01s)
=== RUN   TestUpdateFilteredPolicies
    adapter_test.go:124: Policy:  []
    adapter_test.go:131: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:594: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
Warning: 7 05:06:08 [WARNING]: As your mongodb server doesn't allow a replica set, transaction operation is not supported. So Casbin Adapter will run non-transactional updating!
Warning: 7 05:06:08 [WARNING]: As your mongodb server doesn't allow a replica set, transaction operation is not supported. So Casbin Adapter will run non-transactional updating!
--- PASS: TestUpdateFilteredPolicies (0.01s)
=== RUN   TestUpdateFilteredPoliciesTxn
    adapter_test.go:124: Policy:  []
    adapter_test.go:131: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:624: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
--- PASS: TestUpdateFilteredPoliciesTxn (0.02s)
=== RUN   TestSavePolicyPreservesIndexes
    adapter_test.go:124: Policy:  []
    adapter_test.go:131: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
--- PASS: TestSavePolicyPreservesIndexes (0.01s)
=== RUN   TestSavePolicyPreventsDuplicates
    adapter_test.go:124: Policy:  []
    adapter_test.go:131: Policy:  [[alice data1 read] [bob data2 write] [data2_admin data2 read] [data2_admin data2 write]]
    adapter_test.go:694: Expected AddPolicy of duplicate to fail due to unique index, but it succeeded
--- FAIL: TestSavePolicyPreventsDuplicates (0.01s)
FAIL
	github.com/casbin/mongodb-adapter/v4	coverage: 85.5% of statements
FAIL	github.com/casbin/mongodb-adapter/v4	30.152s
FAIL
Error: Process completed with exit code 1.

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 7, 2025

@copilot fix failed CI check:

...

Fixed in commit 34640bb. The test was calling e.AddPolicy() which performs in-memory duplicate checking before calling the adapter. Changed to call adapter.AddPolicy() directly to test the MongoDB unique index constraint.

@hsluoyz hsluoyz changed the title Fix SavePolicy to preserve unique indexes by using DeleteMany instead of Drop feat: fix SavePolicy bug to preserve unique indexes by using DeleteMany instead of Drop Nov 7, 2025
@hsluoyz hsluoyz merged commit c39abb7 into master Nov 7, 2025
9 of 10 checks passed
hsluoyz added a commit that referenced this pull request Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] SavePolicy method call dropTable, that will delete whole collection, unique index will lose

4 participants