Skip to content

Commit 3ca3b12

Browse files
committed
WIP
1 parent ae47e03 commit 3ca3b12

File tree

12 files changed

+335
-349
lines changed

12 files changed

+335
-349
lines changed

coderd/autobuild/executor/lifecycle_executor.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,17 @@ func (e *Executor) runOnce(t time.Time) Stats {
101101
// NOTE: If a workspace build is created with a given TTL and then the user either
102102
// changes or unsets the TTL, the deadline for the workspace build will not
103103
// have changed. This behavior is as expected per #2229.
104-
workspaceRows, err := e.db.GetWorkspaces(e.ctx, database.GetWorkspacesParams{
104+
workspaces, err := e.db.GetWorkspaces(e.ctx, database.GetWorkspacesParams{
105105
Deleted: false,
106106
})
107107
if err != nil {
108108
e.log.Error(e.ctx, "get workspaces for autostart or autostop", slog.Error(err))
109109
return stats
110110
}
111-
workspaces := database.ConvertWorkspaceRows(workspaceRows)
112111

113112
var eligibleWorkspaceIDs []uuid.UUID
114113
for _, ws := range workspaces {
115-
if isEligibleForAutoStartStop(ws) {
114+
if isEligibleForAutoStartStop(ws.Workspace) {
116115
eligibleWorkspaceIDs = append(eligibleWorkspaceIDs, ws.ID)
117116
}
118117
}

coderd/database/bindvars.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
)
1717

1818
var nameRegex = regexp.MustCompile(`@([a-zA-Z0-9_]+)`)
19+
20+
// dbmapper grabs struct 'db' tags.
1921
var dbmapper = reflectx.NewMapper("db")
2022
var sqlValuer = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
2123

@@ -26,7 +28,7 @@ var sqlValuer = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
2628
//
2729
// 1. SQLx does not reuse arguments, so "@arg, @arg" will result in two arguments
2830
// "$1, $2" instead of "$1, $1".
29-
// 2. SQLx does not handle generic array types
31+
// 2. SQLx does not handle uuid arrays.
3032
// 3. SQLx only supports ":name" style arguments and breaks "::" type casting.
3133
func bindNamed(query string, arg interface{}) (newQuery string, args []interface{}, err error) {
3234
// We do not need to implement a sql parser to extract and replace the variable names.
@@ -39,13 +41,14 @@ func bindNamed(query string, arg interface{}) (newQuery string, args []interface
3941
for i, name := range names {
4042
rpl := fmt.Sprintf("$%d", i+1)
4143
query = strings.ReplaceAll(query, name, rpl)
44+
// Remove the "@" prefix to match to the "db" struct tag.
4245
names[i] = strings.TrimPrefix(name, "@")
4346
}
4447

4548
arglist := make([]interface{}, 0, len(names))
4649

47-
// This comes straight from SQLx
48-
// grab the indirected value of arg
50+
// This comes straight from SQLx's implementation to get the values
51+
// of the struct fields.
4952
v := reflect.ValueOf(arg)
5053
for v = reflect.ValueOf(arg); v.Kind() == reflect.Ptr; {
5154
v = v.Elem()
@@ -60,6 +63,7 @@ func bindNamed(query string, arg interface{}) (newQuery string, args []interface
6063

6164
// Handle some custom types to make arguments easier to use.
6265
switch val.Interface().(type) {
66+
// Feel free to add more types here as needed.
6367
case []uuid.UUID:
6468
arglist = append(arglist, pq.Array(val.Interface()))
6569
default:
@@ -74,15 +78,3 @@ func bindNamed(query string, arg interface{}) (newQuery string, args []interface
7478

7579
return query, arglist, nil
7680
}
77-
78-
type UUIDs []uuid.UUID
79-
80-
func (ids UUIDs) Value() (driver.Value, error) {
81-
v := pq.Array(ids)
82-
return v.Value()
83-
}
84-
85-
func (ids *UUIDs) Scan(src interface{}) error {
86-
v := pq.Array(ids)
87-
return v.Scan(src)
88-
}

coderd/database/dbauthz/querier.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,9 +1147,12 @@ func (q *querier) GetAuthorizedWorkspaces(ctx context.Context, arg database.GetW
11471147
return q.db.GetAuthorizedWorkspaces(ctx, arg, prep)
11481148
}
11491149

1150-
func (q *querier) GetWorkspaces(_ context.Context, _ database.GetWorkspacesParams) ([]database.GetWorkspacesRow, error) {
1151-
// Always call GetAuthorizedWorkspaces.
1152-
panic("not implemented")
1150+
func (q *querier) GetWorkspaces(ctx context.Context, arg database.GetWorkspacesParams) ([]database.WorkspaceWithData, error) {
1151+
prep, err := prepareSQLFilter(ctx, q.auth, rbac.ActionRead, rbac.ResourceWorkspace.Type)
1152+
if err != nil {
1153+
return nil, xerrors.Errorf("(dev error) prepare sql filter: %w", err)
1154+
}
1155+
return q.db.GetAuthorizedWorkspaces(ctx, arg, prep)
11531156
}
11541157

11551158
func (q *querier) GetLatestWorkspaceBuildByWorkspaceID(ctx context.Context, workspaceID uuid.UUID) (database.WorkspaceBuild, error) {

0 commit comments

Comments
 (0)