Skip to content

Commit 1f4dfc7

Browse files
committed
Add queries for workspace logs
1 parent c79653e commit 1f4dfc7

File tree

7 files changed

+197
-15
lines changed

7 files changed

+197
-15
lines changed

database/databasefake/databasefake.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,23 @@ func (q *fakeQuerier) GetWorkspaceHistoryByWorkspaceIDWithoutAfter(_ context.Con
188188
return database.WorkspaceHistory{}, sql.ErrNoRows
189189
}
190190

191+
func (q *fakeQuerier) GetWorkspaceHistoryLogsByIDBefore(ctx context.Context, arg database.GetWorkspaceHistoryLogsByIDBeforeParams) ([]database.WorkspaceHistoryLog, error) {
192+
logs := make([]database.WorkspaceHistoryLog, 0)
193+
for _, workspaceHistoryLog := range q.workspaceHistoryLog {
194+
if workspaceHistoryLog.WorkspaceHistoryID.String() != arg.WorkspaceHistoryID.String() {
195+
continue
196+
}
197+
if workspaceHistoryLog.CreatedAt.After(arg.CreatedAt) {
198+
continue
199+
}
200+
logs = append(logs, workspaceHistoryLog)
201+
}
202+
if len(logs) == 0 {
203+
return nil, sql.ErrNoRows
204+
}
205+
return logs, nil
206+
}
207+
191208
func (q *fakeQuerier) GetWorkspaceHistoryByWorkspaceID(_ context.Context, workspaceID uuid.UUID) ([]database.WorkspaceHistory, error) {
192209
history := make([]database.WorkspaceHistory, 0)
193210
for _, workspaceHistory := range q.workspaceHistory {
@@ -201,6 +218,19 @@ func (q *fakeQuerier) GetWorkspaceHistoryByWorkspaceID(_ context.Context, worksp
201218
return history, nil
202219
}
203220

221+
func (q *fakeQuerier) GetWorkspaceHistoryByWorkspaceIDAndName(ctx context.Context, arg database.GetWorkspaceHistoryByWorkspaceIDAndNameParams) (database.WorkspaceHistory, error) {
222+
for _, workspaceHistory := range q.workspaceHistory {
223+
if workspaceHistory.WorkspaceID.String() != arg.WorkspaceID.String() {
224+
continue
225+
}
226+
if !strings.EqualFold(workspaceHistory.Name, arg.Name) {
227+
continue
228+
}
229+
return workspaceHistory, nil
230+
}
231+
return database.WorkspaceHistory{}, sql.ErrNoRows
232+
}
233+
204234
func (q *fakeQuerier) GetWorkspacesByProjectAndUserID(_ context.Context, arg database.GetWorkspacesByProjectAndUserIDParams) ([]database.Workspace, error) {
205235
workspaces := make([]database.Workspace, 0)
206236
for _, workspace := range q.workspace {
@@ -636,6 +666,7 @@ func (q *fakeQuerier) InsertWorkspaceHistory(_ context.Context, arg database.Ins
636666
CreatedAt: arg.CreatedAt,
637667
UpdatedAt: arg.UpdatedAt,
638668
WorkspaceID: arg.WorkspaceID,
669+
Name: arg.Name,
639670
ProjectHistoryID: arg.ProjectHistoryID,
640671
BeforeID: arg.BeforeID,
641672
Transition: arg.Transition,

database/dump.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ CREATE TABLE workspace_history (
246246
completed_at timestamp with time zone,
247247
workspace_id uuid NOT NULL,
248248
project_history_id uuid NOT NULL,
249+
name character varying(64) NOT NULL,
249250
before_id uuid,
250251
after_id uuid,
251252
transition workspace_transition NOT NULL,
@@ -318,9 +319,15 @@ ALTER TABLE ONLY workspace_history
318319
ALTER TABLE ONLY workspace_history_log
319320
ADD CONSTRAINT workspace_history_log_id_key UNIQUE (id);
320321

322+
ALTER TABLE ONLY workspace_history
323+
ADD CONSTRAINT workspace_history_workspace_id_name_key UNIQUE (workspace_id, name);
324+
321325
ALTER TABLE ONLY workspace
322326
ADD CONSTRAINT workspace_id_key UNIQUE (id);
323327

328+
ALTER TABLE ONLY workspace
329+
ADD CONSTRAINT workspace_owner_id_name_key UNIQUE (owner_id, name);
330+
324331
ALTER TABLE ONLY workspace_resource
325332
ADD CONSTRAINT workspace_resource_id_key UNIQUE (id);
326333

database/migrations/000003_workspaces.up.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ CREATE TABLE workspace (
44
updated_at timestamptz NOT NULL,
55
owner_id text NOT NULL,
66
project_id uuid NOT NULL REFERENCES project (id),
7-
name varchar(64) NOT NULL
7+
name varchar(64) NOT NULL,
8+
UNIQUE(owner_id, name)
89
);
910

1011
CREATE TYPE workspace_transition AS ENUM (
@@ -22,14 +23,16 @@ CREATE TABLE workspace_history (
2223
completed_at timestamptz,
2324
workspace_id uuid NOT NULL REFERENCES workspace (id) ON DELETE CASCADE,
2425
project_history_id uuid NOT NULL REFERENCES project_history (id) ON DELETE CASCADE,
26+
name varchar(64) NOT NULL,
2527
before_id uuid,
2628
after_id uuid,
2729
transition workspace_transition NOT NULL,
2830
initiator varchar(255) NOT NULL,
2931
-- State stored by the provisioner
3032
provisioner_state bytea,
3133
-- Job ID of the action
32-
provision_job_id uuid NOT NULL
34+
provision_job_id uuid NOT NULL,
35+
UNIQUE(workspace_id, name)
3336
);
3437

3538
-- Cloud resources produced by a provision job.

database/models.go

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

database/querier.go

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

database/query.sql

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,15 @@ WHERE
261261
LIMIT
262262
1;
263263

264+
-- name: GetWorkspaceHistoryByWorkspaceIDAndName :one
265+
SELECT
266+
*
267+
FROM
268+
workspace_history
269+
WHERE
270+
workspace_id = $1
271+
AND name = $2;
272+
264273
-- name: GetWorkspaceHistoryByWorkspaceID :many
265274
SELECT
266275
*
@@ -280,6 +289,17 @@ WHERE
280289
LIMIT
281290
1;
282291

292+
-- name: GetWorkspaceHistoryLogsByIDBefore :many
293+
SELECT
294+
*
295+
FROM
296+
workspace_history_log
297+
WHERE
298+
workspace_history_id = $1
299+
AND created_at <= $2
300+
ORDER BY
301+
created_at;
302+
283303
-- name: GetWorkspaceResourcesByHistoryID :many
284304
SELECT
285305
*
@@ -523,12 +543,13 @@ INSERT INTO
523543
workspace_id,
524544
project_history_id,
525545
before_id,
546+
name,
526547
transition,
527548
initiator,
528549
provision_job_id
529550
)
530551
VALUES
531-
($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *;
552+
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *;
532553

533554
-- name: InsertWorkspaceHistoryLogs :many
534555
INSERT INTO

0 commit comments

Comments
 (0)