Skip to content

Commit ba492b2

Browse files
committed
chore: implement filters for the organizations query
1 parent f24cb5c commit ba492b2

File tree

13 files changed

+71
-30
lines changed

13 files changed

+71
-30
lines changed

cli/server_createadminuser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (r *RootCmd) newCreateAdminUserCommand() *serpent.Command {
176176
// Create the user.
177177
var newUser database.User
178178
err = db.InTx(func(tx database.Store) error {
179-
orgs, err := tx.GetOrganizations(ctx)
179+
orgs, err := tx.GetOrganizations(ctx, database.GetOrganizationsParams{})
180180
if err != nil {
181181
return xerrors.Errorf("get organizations: %w", err)
182182
}

cli/server_createadminuser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestServerCreateAdminUser(t *testing.T) {
6060
require.EqualValues(t, []string{codersdk.RoleOwner}, user.RBACRoles, "user does not have owner role")
6161

6262
// Check that user is admin in every org.
63-
orgs, err := db.GetOrganizations(ctx)
63+
orgs, err := db.GetOrganizations(ctx, database.GetOrganizationsParams{})
6464
require.NoError(t, err)
6565
orgIDs := make(map[uuid.UUID]struct{}, len(orgs))
6666
for _, org := range orgs {

coderd/database/dbauthz/dbauthz.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,9 +1700,9 @@ func (q *querier) GetOrganizationIDsByMemberIDs(ctx context.Context, ids []uuid.
17001700
return fetchWithPostFilter(q.auth, policy.ActionRead, q.db.GetOrganizationIDsByMemberIDs)(ctx, ids)
17011701
}
17021702

1703-
func (q *querier) GetOrganizations(ctx context.Context) ([]database.Organization, error) {
1703+
func (q *querier) GetOrganizations(ctx context.Context, args database.GetOrganizationsParams) ([]database.Organization, error) {
17041704
fetch := func(ctx context.Context, _ interface{}) ([]database.Organization, error) {
1705-
return q.db.GetOrganizations(ctx)
1705+
return q.db.GetOrganizations(ctx, args)
17061706
}
17071707
return fetchWithPostFilter(q.auth, policy.ActionRead, fetch)(ctx, nil)
17081708
}

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ func (s *MethodTestSuite) TestOrganization() {
635635
def, _ := db.GetDefaultOrganization(context.Background())
636636
a := dbgen.Organization(s.T(), db, database.Organization{})
637637
b := dbgen.Organization(s.T(), db, database.Organization{})
638-
check.Args().Asserts(def, policy.ActionRead, a, policy.ActionRead, b, policy.ActionRead).Returns(slice.New(def, a, b))
638+
check.Args(database.GetOrganizationsParams{}).Asserts(def, policy.ActionRead, a, policy.ActionRead, b, policy.ActionRead).Returns(slice.New(def, a, b))
639639
}))
640640
s.Run("GetOrganizationsByUserID", s.Subtest(func(db database.Store, check *expects) {
641641
u := dbgen.User(s.T(), db, database.User{})

coderd/database/dbmem/dbmem.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,14 +3034,24 @@ func (q *FakeQuerier) GetOrganizationIDsByMemberIDs(_ context.Context, ids []uui
30343034
return getOrganizationIDsByMemberIDRows, nil
30353035
}
30363036

3037-
func (q *FakeQuerier) GetOrganizations(_ context.Context) ([]database.Organization, error) {
3037+
func (q *FakeQuerier) GetOrganizations(_ context.Context, args database.GetOrganizationsParams) ([]database.Organization, error) {
30383038
q.mutex.RLock()
30393039
defer q.mutex.RUnlock()
30403040

3041-
if len(q.organizations) == 0 {
3042-
return nil, sql.ErrNoRows
3041+
tmp := make([]database.Organization, 0)
3042+
for _, org := range q.organizations {
3043+
if len(args.IDs) > 0 {
3044+
if !slices.Contains(args.IDs, org.ID) {
3045+
continue
3046+
}
3047+
}
3048+
if args.Name != "" && !strings.EqualFold(org.Name, args.Name) {
3049+
continue
3050+
}
3051+
tmp = append(tmp, org)
30433052
}
3044-
return q.organizations, nil
3053+
3054+
return tmp, nil
30453055
}
30463056

30473057
func (q *FakeQuerier) GetOrganizationsByUserID(_ context.Context, userID uuid.UUID) ([]database.Organization, error) {
@@ -3060,9 +3070,7 @@ func (q *FakeQuerier) GetOrganizationsByUserID(_ context.Context, userID uuid.UU
30603070
organizations = append(organizations, organization)
30613071
}
30623072
}
3063-
if len(organizations) == 0 {
3064-
return nil, sql.ErrNoRows
3065-
}
3073+
30663074
return organizations, nil
30673075
}
30683076

coderd/database/dbmem/dbmem_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestInTx(t *testing.T) {
4646
go func() {
4747
<-inTx
4848
for i := 0; i < 20; i++ {
49-
orgs, err := uut.GetOrganizations(context.Background())
49+
orgs, err := uut.GetOrganizations(context.Background(), database.GetOrganizationsParams{})
5050
if err != nil {
5151
assert.ErrorIs(t, err, sql.ErrNoRows)
5252
}

coderd/database/dbmetrics/dbmetrics.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ func TestDefaultOrg(t *testing.T) {
516516
ctx := context.Background()
517517

518518
// Should start with the default org
519-
all, err := db.GetOrganizations(ctx)
519+
all, err := db.GetOrganizations(ctx, database.GetOrganizationsParams{})
520520
require.NoError(t, err)
521521
require.Len(t, all, 1)
522522
require.True(t, all[0].IsDefault, "first org should always be default")
@@ -1211,7 +1211,7 @@ func TestExpectOne(t *testing.T) {
12111211
dbgen.Organization(t, db, database.Organization{})
12121212

12131213
// Organizations is an easy table without foreign key dependencies
1214-
_, err = database.ExpectOne(db.GetOrganizations(ctx))
1214+
_, err = database.ExpectOne(db.GetOrganizations(ctx, database.GetOrganizationsParams{}))
12151215
require.ErrorContains(t, err, "too many rows returned")
12161216
})
12171217
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy