Skip to content

Conversation

@rossigee
Copy link

  • 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
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Oct 19, 2025
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Oct 19, 2025
@rossigee rossigee changed the title optimize access refresh clean Optimize 'refreshAccesses' to perform update without removing then adding Oct 19, 2025
@rossigee rossigee marked this pull request as ready for review October 19, 2025 07:24
@lunny lunny added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Oct 20, 2025
@lunny lunny added this to the 1.26.0 milestone Oct 20, 2025
@rossigee rossigee requested a review from lunny October 23, 2025 08:41
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")
Copy link
Member

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.

Comment on lines +118 to +121
var toUpdate []struct {
UserID int64
Mode perm.AccessMode
}
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a test:

  1. Add U1 as public repo's collaborator with "read" access
  2. Trigger "refreshAccesses"
  3. U1's "access" record is skipped
  4. U1 can still access the public repo because the repo is public

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/go Pull requests that update Go code type/refactoring Existing code has been cleaned up. There should be no new functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants