-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Optimize 'refreshAccesses' to perform update without removing then adding #35702
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
base: main
Are you sure you want to change the base?
Conversation
rossigee
commented
Oct 19, 2025
- Optimize refreshAccesses with cross-comparison to minimize DB operations
- Fix db.Find syntax in refreshAccesses optimization
- Add test for refreshAccesses update path and fix db.Find syntax
Implemented the FIXME to perform cross-comparison between existing and desired accesses, reducing deletions, updates, and insertions to the minimum necessary. This improves performance for repositories with many users by avoiding bulk delete-all and re-insert-all.
Corrected the db.Find call to use builder.Eq for the condition instead of passing a bean, which was causing compilation or runtime errors.
- Fix db.Find syntax error by using db.GetEngine().Where().Find() instead of db.Find() with builder.Eq directly - Add TestRepository_RecalculateAccesses_UpdateMode to test the update optimization path when user access mode changes - Improves test coverage for refreshAccesses from 69.4% to 75.0% - Validates that access mode updates use UPDATE instead of DELETE+INSERT
| has, err = db.GetEngine(t.Context()).Get(updatedAccess) | ||
| assert.NoError(t, err) | ||
| assert.True(t, has, "Access should still exist") | ||
| assert.Equal(t, newMode, updatedAccess.Mode, "Access mode should be updated to new collaboration mode") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to have another use case to remove this user's permission and have a test again.
| var toUpdate []struct { | ||
| UserID int64 | ||
| Mode perm.AccessMode | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can reuse the Access struct, and it could be clearer for the code below
| for userID, ua := range accessMap { | ||
| if ua.Mode < minMode && !ua.User.IsRestricted { | ||
| continue | ||
| // Should not have access |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment seems not right.
If I read correctly, it doesn't mean "Should not have access", but "default access" is enough?
If it really means "should not have access", why ua.User.IsRestricted is kept
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a test:
- Add U1 as public repo's collaborator with "read" access
- Trigger "refreshAccesses"
- U1's "access" record is skipped
- U1 can still access the public repo because the repo is public