Improve performance of allowed org repo creation query (#6100)

See https://codeberg.org/Codeberg-Infrastructure/build-deploy-forgejo/issues/144#issuecomment-2484031.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6100
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
Co-authored-by: Otto Richter <git@otto.splvs.net>
Co-committed-by: Otto Richter <git@otto.splvs.net>
This commit is contained in:
Otto Richter 2024-12-01 15:16:25 +00:00 committed by 0ko
parent d68c99542c
commit d2bf2e7631
2 changed files with 15 additions and 2 deletions

View file

@ -57,11 +57,11 @@ func (opts FindOrgOptions) ToOrders() string {
func GetOrgsCanCreateRepoByUserID(ctx context.Context, userID int64) ([]*Organization, error) {
orgs := make([]*Organization, 0, 10)
return orgs, db.GetEngine(ctx).Where(builder.In("id", builder.Select("`user`.id").From("`user`").
return orgs, db.GetEngine(ctx).Select("DISTINCT `user`.id, `user`.*").Table("`user`").
Join("INNER", "`team_user`", "`team_user`.org_id = `user`.id").
Join("INNER", "`team`", "`team`.id = `team_user`.team_id").
Where(builder.Eq{"`team_user`.uid": userID}).
And(builder.Eq{"`team`.authorize": perm.AccessModeOwner}.Or(builder.Eq{"`team`.can_create_org_repo": true})))).
And(builder.Eq{"`team`.authorize": perm.AccessModeOwner}.Or(builder.Eq{"`team`.can_create_org_repo": true})).
Asc("`user`.name").
Find(&orgs)
}

View file

@ -51,6 +51,19 @@ func TestFindOrgs(t *testing.T) {
assert.EqualValues(t, 1, total)
}
func TestGetOrgsCanCreateRepoByUserID(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
orgs, err := organization.GetOrgsCanCreateRepoByUserID(db.DefaultContext, 2)
require.NoError(t, err)
assert.Len(t, orgs, 1)
assert.EqualValues(t, 3, orgs[0].ID)
orgs, err = organization.GetOrgsCanCreateRepoByUserID(db.DefaultContext, 1)
require.NoError(t, err)
assert.Len(t, orgs, 2)
assert.EqualValues(t, 36, orgs[0].ID)
assert.EqualValues(t, 35, orgs[1].ID)
}
func TestGetUserOrgsList(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
orgs, err := organization.GetUserOrgsList(db.DefaultContext, &user_model.User{ID: 4})