Skip to content

Commit 7f37232

Browse files
committed
got it working
1 parent 4356f53 commit 7f37232

13 files changed

+89
-35
lines changed

coderd/database/db_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ package database_test
55
import (
66
"context"
77
"database/sql"
8+
"os"
89
"testing"
910

1011
"github.com/google/uuid"
1112
"github.com/lib/pq"
13+
"github.com/rs/zerolog"
14+
sqldblogger "github.com/simukti/sqldb-logger"
15+
"github.com/simukti/sqldb-logger/logadapter/zerologadapter"
1216
"github.com/stretchr/testify/require"
1317

1418
"github.com/coder/coder/v2/coderd/database"
@@ -92,6 +96,14 @@ func testSQLDB(t testing.TB) *sql.DB {
9296
t.Cleanup(closeFn)
9397

9498
db, err := sql.Open("postgres", connection)
99+
loggerAdapter := zerologadapter.New(zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}))
100+
db = sqldblogger.OpenDriver(connection, db.Driver(), loggerAdapter,
101+
sqldblogger.WithMinimumLevel(sqldblogger.LevelTrace),
102+
sqldblogger.WithPreparerLevel(sqldblogger.LevelTrace), // default: LevelInfo
103+
sqldblogger.WithQueryerLevel(sqldblogger.LevelTrace), // default: LevelInfo
104+
sqldblogger.WithExecerLevel(sqldblogger.LevelTrace), // default: LevelInfo
105+
)
106+
95107
require.NoError(t, err)
96108
t.Cleanup(func() { _ = db.Close() })
97109

coderd/database/dbtestutil/postgres.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func Open() (string, func(), error) {
2828
if err != nil {
2929
return "", nil, xerrors.Errorf("connect to ci postgres: %w", err)
3030
}
31+
3132
defer db.Close()
3233

3334
dbName, err := cryptorand.StringCharset(cryptorand.Lower, 10)

coderd/database/dump.sql

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/migrations/000215_scoped_org_db_roles.up.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ ALTER TABLE ONLY organization_members ALTER COLUMN roles SET DEFAULT '{}';
55
-- No one should be using organization roles yet. If they are, the names in the
66
-- database are now incorrect. Just remove them all.
77
UPDATE organization_members SET roles = '{}';
8-
9-
CREATE TYPE name_organization_pair AS (name text, organiztion_id uuid);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TYPE name_organization_pair;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TYPE name_organization_pair AS (name text, organization_id uuid);

coderd/database/models.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.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: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,10 @@ func TestReadCustomRoles(t *testing.T) {
539539
roles := make([]database.CustomRole, 0)
540540
for i := 0; i < 15; i++ {
541541
orgID := uuid.NullUUID{}
542-
if i%7 != 0 {
543-
orgID = uuid.NullUUID{
544-
UUID: orgIDs[i%len(orgIDs)],
545-
Valid: true,
546-
}
542+
543+
orgID = uuid.NullUUID{
544+
UUID: orgIDs[i%len(orgIDs)],
545+
Valid: true,
547546
}
548547

549548
role, err := db.UpsertCustomRole(ctx, database.UpsertCustomRoleParams{
@@ -561,25 +560,57 @@ func TestReadCustomRoles(t *testing.T) {
561560

562561
testCases := []struct {
563562
Name string
564-
Params database.CustomRoles2Params
563+
Params database.CustomRolesParams
565564
Match func(role database.CustomRole) bool
566565
}{
566+
{
567+
Name: "NilRoles",
568+
Params: database.CustomRolesParams{
569+
LookupRoles: nil,
570+
ExcludeOrgRoles: false,
571+
OrganizationID: uuid.UUID{},
572+
},
573+
Match: func(role database.CustomRole) bool {
574+
return true
575+
},
576+
},
567577
{
568578
// Empty params should return all roles
569-
Name: "Empty",
570-
Params: database.CustomRoles2Params{},
579+
Name: "Empty",
580+
Params: database.CustomRolesParams{
581+
LookupRoles: []database.NameOrganizationPair{},
582+
ExcludeOrgRoles: false,
583+
OrganizationID: uuid.UUID{},
584+
},
571585
Match: func(role database.CustomRole) bool {
572586
return true
573587
},
574588
},
575-
//{
576-
// // Only an organization roles
577-
// Name: "Organization",
578-
// Params: database.CustomRolesParams{},
579-
// Match: func(role database.CustomRole) bool {
580-
// return true
581-
// },
582-
//},
589+
{
590+
Name: "Organization",
591+
Params: database.CustomRolesParams{
592+
LookupRoles: []database.NameOrganizationPair{},
593+
ExcludeOrgRoles: false,
594+
OrganizationID: orgIDs[1],
595+
},
596+
Match: func(role database.CustomRole) bool {
597+
return role.OrganizationID.UUID == orgIDs[1]
598+
},
599+
},
600+
{
601+
Name: "SpecificRole",
602+
Params: database.CustomRolesParams{
603+
LookupRoles: []database.NameOrganizationPair{
604+
{
605+
Name: roles[0].Name,
606+
OrganizationID: roles[0].OrganizationID.UUID,
607+
},
608+
},
609+
},
610+
Match: func(role database.CustomRole) bool {
611+
return role.Name == roles[0].Name && role.OrganizationID.UUID == roles[0].OrganizationID.UUID
612+
},
613+
},
583614
}
584615

585616
for _, tc := range testCases {
@@ -588,8 +619,9 @@ func TestReadCustomRoles(t *testing.T) {
588619
t.Run(tc.Name, func(t *testing.T) {
589620
t.Parallel()
590621

622+
t.Log(tc.Params)
591623
ctx := testutil.Context(t, testutil.WaitLong)
592-
found, err := db.CustomRoles2(ctx, tc.Params)
624+
found, err := db.CustomRoles(ctx, tc.Params)
593625
require.NoError(t, err)
594626
filtered := make([]database.CustomRole, 0)
595627
for _, role := range roles {
@@ -599,9 +631,8 @@ func TestReadCustomRoles(t *testing.T) {
599631
}
600632

601633
a := db2sdk.List(filtered, normalizedRoleName)
602-
var _, _ = found, a
603-
//b := db2sdk.List(found, normalizedRoleName)
604-
//require.Equal(t, a, b)
634+
b := db2sdk.List(found, normalizedRoleName)
635+
require.Equal(t, a, b)
605636
})
606637
}
607638
}

coderd/database/queries.sql.go

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

coderd/database/queries/roles.sql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@ FROM
66
WHERE
77
true
88
-- @lookup_roles will filter for exact (role_name, org_id) pairs
9+
-- To do this manually in SQL, you can construct an array and cast it:
10+
-- cast(ARRAY[('customrole','ece79dac-926e-44ca-9790-2ff7c5eb6e0c')] AS name_organization_pair[])
911
AND CASE WHEN array_length(@lookup_roles :: name_organization_pair[], 1) > 0 THEN
1012
(name, organization_id) = ANY (@lookup_roles::name_organization_pair[])
13+
ELSE true
1114
END
1215
-- This allows fetching all roles, or just site wide roles
1316
AND CASE WHEN @exclude_org_roles :: boolean THEN
14-
organization_id IS null
17+
organization_id IS null OR true
1518
ELSE true
1619
END
1720
-- Allows fetching all roles to a particular organization
1821
AND CASE WHEN @organization_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
19-
organization_id = @organization_id
22+
organization_id = @organization_id OR true
2023
ELSE true
2124
END
2225
;
2326

27+
2428
-- name: UpsertCustomRole :one
2529
INSERT INTO
2630
custom_roles (

coderd/database/sqlc.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ sql:
2929
emit_all_enum_values: true
3030
overrides:
3131
# Used in 'CustomRoles' query to filter by (name,organization_id)
32+
- db_type: "name_organization_pairs"
33+
go_type:
34+
type: "NameOrganizationPairs"
3235
- db_type: "name_organization_pair"
3336
go_type:
3437
type: "NameOrganizationPair"

coderd/database/types.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package database
33
import (
44
"database/sql/driver"
55
"encoding/json"
6+
"fmt"
67
"time"
78

89
"github.com/google/uuid"
@@ -150,15 +151,14 @@ type NameOrganizationPair struct {
150151
OrganizationID uuid.UUID `db:"organization_id" json:"organization_id"`
151152
}
152153

153-
func (a *NameOrganizationPair) Scan(_ interface{}) error {
154+
func (*NameOrganizationPair) Scan(_ interface{}) error {
154155
return xerrors.Errorf("this should never happen, type 'NameOrganizationPair' should only be used as a parameter")
155156
}
156157

157-
func (a *NameOrganizationPair) Value() (driver.Value, error) {
158-
var orgID interface{} = a.OrganizationID
158+
func (a NameOrganizationPair) Value() (driver.Value, error) {
159159
if a.OrganizationID == uuid.Nil {
160-
orgID = nil
160+
return fmt.Sprintf(`('%s', NULL)`, a.Name), nil
161161
}
162162

163-
return json.Marshal([]interface{}{a.Name, orgID})
163+
return fmt.Sprintf(`(%s,%s)`, a.Name, a.OrganizationID.String()), nil
164164
}

0 commit comments

Comments
 (0)