Skip to content

Commit 86ac8cf

Browse files
fix: begin impl of reducing autostart false-positive rate
1 parent fbe2fa6 commit 86ac8cf

File tree

19 files changed

+276
-25
lines changed

19 files changed

+276
-25
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4044,6 +4044,13 @@ func (q *querier) UpdateWorkspaceLastUsedAt(ctx context.Context, arg database.Up
40444044
return update(q.log, q.auth, fetch, q.db.UpdateWorkspaceLastUsedAt)(ctx, arg)
40454045
}
40464046

4047+
func (q *querier) UpdateWorkspaceNextStartAt(ctx context.Context, arg database.UpdateWorkspaceNextStartAtParams) error {
4048+
fetch := func(ctx context.Context, arg database.UpdateWorkspaceNextStartAtParams) (database.Workspace, error) {
4049+
return q.db.GetWorkspaceByID(ctx, arg.ID)
4050+
}
4051+
return update(q.log, q.auth, fetch, q.db.UpdateWorkspaceNextStartAt)(ctx, arg)
4052+
}
4053+
40474054
func (q *querier) UpdateWorkspaceProxy(ctx context.Context, arg database.UpdateWorkspaceProxyParams) (database.WorkspaceProxy, error) {
40484055
fetch := func(ctx context.Context, arg database.UpdateWorkspaceProxyParams) (database.WorkspaceProxy, error) {
40494056
return q.db.GetWorkspaceProxyByID(ctx, arg.ID)

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,13 @@ func (s *MethodTestSuite) TestWorkspace() {
18871887
ID: ws.ID,
18881888
}).Asserts(ws, policy.ActionUpdate).Returns()
18891889
}))
1890+
s.Run("UpdateWorkspaceNextStartAt", s.Subtest(func(db database.Store, check *expects) {
1891+
ws := dbgen.Workspace(s.T(), db, database.WorkspaceTable{})
1892+
check.Args(database.UpdateWorkspaceNextStartAtParams{
1893+
ID: ws.ID,
1894+
NextStartAt: sql.NullTime{Valid: true, Time: dbtime.Now()},
1895+
}).Asserts(ws, policy.ActionUpdate)
1896+
}))
18901897
s.Run("BatchUpdateWorkspaceLastUsedAt", s.Subtest(func(db database.Store, check *expects) {
18911898
ws1 := dbgen.Workspace(s.T(), db, database.WorkspaceTable{})
18921899
ws2 := dbgen.Workspace(s.T(), db, database.WorkspaceTable{})

coderd/database/dbmem/dbmem.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9950,6 +9950,29 @@ func (q *FakeQuerier) UpdateWorkspaceLastUsedAt(_ context.Context, arg database.
99509950
return sql.ErrNoRows
99519951
}
99529952

9953+
func (q *FakeQuerier) UpdateWorkspaceNextStartAt(ctx context.Context, arg database.UpdateWorkspaceNextStartAtParams) error {
9954+
err := validateDatabaseType(arg)
9955+
if err != nil {
9956+
return err
9957+
}
9958+
9959+
q.mutex.Lock()
9960+
defer q.mutex.Unlock()
9961+
9962+
for index, workspace := range q.workspaces {
9963+
if workspace.ID != arg.ID {
9964+
continue
9965+
}
9966+
9967+
workspace.NextStartAt = arg.NextStartAt
9968+
q.workspaces[index] = workspace
9969+
9970+
return nil
9971+
}
9972+
9973+
return sql.ErrNoRows
9974+
}
9975+
99539976
func (q *FakeQuerier) UpdateWorkspaceProxy(_ context.Context, arg database.UpdateWorkspaceProxyParams) (database.WorkspaceProxy, error) {
99549977
q.mutex.Lock()
99559978
defer q.mutex.Unlock()

coderd/database/dbmetrics/querymetrics.go

Lines changed: 7 additions & 0 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: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dump.sql

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
DROP VIEW workspaces_expanded;
2+
3+
ALTER TABLE ONLY workspaces DROP COLUMN IF EXISTS next_start_at;
4+
5+
CREATE VIEW
6+
workspaces_expanded
7+
AS
8+
SELECT
9+
workspaces.*,
10+
-- Owner
11+
visible_users.avatar_url AS owner_avatar_url,
12+
visible_users.username AS owner_username,
13+
-- Organization
14+
organizations.name AS organization_name,
15+
organizations.display_name AS organization_display_name,
16+
organizations.icon AS organization_icon,
17+
organizations.description AS organization_description,
18+
-- Template
19+
templates.name AS template_name,
20+
templates.display_name AS template_display_name,
21+
templates.icon AS template_icon,
22+
templates.description AS template_description
23+
FROM
24+
workspaces
25+
INNER JOIN
26+
visible_users
27+
ON
28+
workspaces.owner_id = visible_users.id
29+
INNER JOIN
30+
organizations
31+
ON workspaces.organization_id = organizations.id
32+
INNER JOIN
33+
templates
34+
ON workspaces.template_id = templates.id
35+
;
36+
37+
COMMENT ON VIEW workspaces_expanded IS 'Joins in the display name information such as username, avatar, and organization name.';
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
ALTER TABLE ONLY workspaces ADD COLUMN IF NOT EXISTS next_start_at TIMESTAMPTZ DEFAULT NULL;
2+
3+
-- Recreate view
4+
DROP VIEW workspaces_expanded;
5+
6+
CREATE VIEW
7+
workspaces_expanded
8+
AS
9+
SELECT
10+
workspaces.*,
11+
-- Owner
12+
visible_users.avatar_url AS owner_avatar_url,
13+
visible_users.username AS owner_username,
14+
-- Organization
15+
organizations.name AS organization_name,
16+
organizations.display_name AS organization_display_name,
17+
organizations.icon AS organization_icon,
18+
organizations.description AS organization_description,
19+
-- Template
20+
templates.name AS template_name,
21+
templates.display_name AS template_display_name,
22+
templates.icon AS template_icon,
23+
templates.description AS template_description
24+
FROM
25+
workspaces
26+
INNER JOIN
27+
visible_users
28+
ON
29+
workspaces.owner_id = visible_users.id
30+
INNER JOIN
31+
organizations
32+
ON workspaces.organization_id = organizations.id
33+
INNER JOIN
34+
templates
35+
ON workspaces.template_id = templates.id
36+
;
37+
38+
COMMENT ON VIEW workspaces_expanded IS 'Joins in the display name information such as username, avatar, and organization name.';

coderd/database/modelmethods.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func (w Workspace) WorkspaceTable() WorkspaceTable {
214214
DeletingAt: w.DeletingAt,
215215
AutomaticUpdates: w.AutomaticUpdates,
216216
Favorite: w.Favorite,
217+
NextStartAt: w.NextStartAt,
217218
}
218219
}
219220

@@ -438,6 +439,7 @@ func ConvertWorkspaceRows(rows []GetWorkspacesRow) []Workspace {
438439
TemplateDisplayName: r.TemplateDisplayName,
439440
TemplateIcon: r.TemplateIcon,
440441
TemplateDescription: r.TemplateDescription,
442+
NextStartAt: r.NextStartAt,
441443
}
442444
}
443445

coderd/database/modelqueries.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
290290
&i.DeletingAt,
291291
&i.AutomaticUpdates,
292292
&i.Favorite,
293+
&i.NextStartAt,
293294
&i.OwnerAvatarUrl,
294295
&i.OwnerUsername,
295296
&i.OrganizationName,

coderd/database/models.go

Lines changed: 3 additions & 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 & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)