Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
use v4 compliant uuid, fix tests
  • Loading branch information
AbhineetJain committed Jun 16, 2022
commit e5dfd1c49d9c5f4df5902f274122c100e1c5dc76
1 change: 1 addition & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func New() database.Store {
HashedPassword: make([]byte, 0),
CreatedAt: database.Now(),
UpdatedAt: database.Now(),
Status: database.UserStatusActive,
RBACRoles: make([]string, 0),
}
return &fakeQuerier{
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"golang.org/x/xerrors"
)

var SystemUserID uuid.UUID = uuid.MustParse("11111111-1111-1111-1111-111111111111")
var SystemUserID uuid.UUID = uuid.MustParse("c0de2b07-0000-4000-A000-000000000000")
Copy link
Member

Choose a reason for hiding this comment

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

Let's add a comment here. Why was this UUID chosen? Why not uuid.Nil?

Copy link
Member

Choose a reason for hiding this comment

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

uuid.Nil already has meaning in the db. We use it as a null in search filters since sqlc didn't support nullable args at the time. (It does as of like a week ago, I made a bug report on it as I ran into some issues using sqlc.narg)


// Store contains all queryable database functions.
// It extends the generated interface to add transaction support.
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/migrations/000024_add_system_user.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
DELETE FROM
users
WHERE
id = '11111111-1111-1111-1111-111111111111';
id = 'c0de2b07-0000-4000-A000-000000000000';
2 changes: 1 addition & 1 deletion coderd/database/migrations/000024_add_system_user.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ INSERT INTO
rbac_roles
)
VALUES
('11111111-1111-1111-1111-111111111111', 'system@coder.com', 'system', '{}', NOW(), NOW(), '{}');
('c0de2b07-0000-4000-A000-000000000000', 'system@coder.com', 'system', '', NOW(), NOW(), '{}');
4 changes: 2 additions & 2 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions coderd/database/queries/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ SELECT
FROM
users
WHERE
id != '11111111-1111-1111-1111-111111111111';
id != 'c0de2b07-0000-4000-A000-000000000000';

-- name: InsertUser :one
INSERT INTO
Expand Down Expand Up @@ -111,7 +111,7 @@ WHERE
AND CASE
WHEN @include_system_user :: boolean THEN true
ELSE (
id != '11111111-1111-1111-1111-111111111111'
id != 'c0de2b07-0000-4000-A000-000000000000'
)
END
-- Filter by status
Expand Down
31 changes: 22 additions & 9 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,31 @@ import (
)

func TestSystemUser(t *testing.T) {
if !coderdtest.UseSQL() {
t.Skip("This test asserts that the system user is equivalent in SQL and the fake database.")
}

t.Parallel()
t.Run("SQLMatchesFake", func(t *testing.T) {
if !coderdtest.UseSQL() {
t.Skip("This test asserts that the system user is equivalent in SQL and the fake database.")
}

t.Parallel()

_, opts := coderdtest.NewWithAPI(t, nil)
fake := databasefake.New()
_, opts := coderdtest.NewWithAPI(t, nil)
fake := databasefake.New()

fakeUser, _ := fake.GetUserByID(context.Background(), database.SystemUserID)
sqlUser, _ := opts.Database.GetUserByID(context.Background(), database.SystemUserID)
require.Equal(t, fakeUser, sqlUser)
fakeUser, _ := fake.GetUserByID(context.Background(), database.SystemUserID)
sqlUser, _ := opts.Database.GetUserByID(context.Background(), database.SystemUserID)

// These fields are different as they use the actual timestamps at creation
fakeUser.CreatedAt, fakeUser.UpdatedAt = time.Time{}, time.Time{}
sqlUser.CreatedAt, sqlUser.UpdatedAt = time.Time{}, time.Time{}

require.Equal(t, fakeUser, sqlUser)
})
t.Run("ValidUUID", func(t *testing.T) {
t.Parallel()
require.Equal(t, uuid.Version(4), database.SystemUserID.Version())
require.Equal(t, uuid.RFC4122, database.SystemUserID.Variant())
})
}

func TestFirstUser(t *testing.T) {
Expand Down