forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplateversions.sql
More file actions
129 lines (119 loc) · 2.44 KB
/
templateversions.sql
File metadata and controls
129 lines (119 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
-- name: GetTemplateVersionsByTemplateID :many
SELECT
*
FROM
template_versions
WHERE
template_id = @template_id :: uuid
AND CASE
-- This allows using the last element on a page as effectively a cursor.
-- This is an important option for scripts that need to paginate without
-- duplicating or missing data.
WHEN @after_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN (
-- The pagination cursor is the last ID of the previous page.
-- The query is ordered by the created_at field, so select all
-- rows after the cursor.
(created_at, id) > (
SELECT
created_at, id
FROM
template_versions
WHERE
id = @after_id
)
)
ELSE true
END
ORDER BY
-- Deterministic and consistent ordering of all rows, even if they share
-- a timestamp. This is to ensure consistent pagination.
(created_at, id) ASC OFFSET @offset_opt
LIMIT
-- A null limit means "no limit", so 0 means return all
NULLIF(@limit_opt :: int, 0);
-- name: GetTemplateVersionByJobID :one
SELECT
*
FROM
template_versions
WHERE
job_id = $1;
-- name: GetTemplateVersionsCreatedAfter :many
SELECT * FROM template_versions WHERE created_at > $1;
-- name: GetTemplateVersionByTemplateIDAndName :one
SELECT
*
FROM
template_versions
WHERE
template_id = $1
AND "name" = $2;
-- name: GetTemplateVersionByID :one
SELECT
*
FROM
template_versions
WHERE
id = $1;
-- name: GetTemplateVersionsByIDs :many
SELECT
*
FROM
template_versions
WHERE
id = ANY(@ids :: uuid [ ]);
-- name: InsertTemplateVersion :one
INSERT INTO
template_versions (
id,
template_id,
organization_id,
created_at,
updated_at,
"name",
readme,
job_id,
created_by
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *;
-- name: UpdateTemplateVersionByID :one
UPDATE
template_versions
SET
template_id = $2,
updated_at = $3,
name = $4
WHERE
id = $1 RETURNING *;
-- name: UpdateTemplateVersionDescriptionByJobID :exec
UPDATE
template_versions
SET
readme = $2,
updated_at = $3
WHERE
job_id = $1;
-- name: UpdateTemplateVersionGitAuthProvidersByJobID :exec
UPDATE
template_versions
SET
git_auth_providers = $2,
updated_at = $3
WHERE
job_id = $1;
-- name: GetPreviousTemplateVersion :one
SELECT
*
FROM
template_versions
WHERE
created_at < (
SELECT created_at
FROM template_versions AS tv
WHERE tv.organization_id = $1 AND tv.name = $2 AND tv.template_id = $3
)
AND organization_id = $1
AND template_id = $3
ORDER BY created_at DESC
LIMIT 1;