feat(coderd/database): add UpsertProvisionerDaemons query (#11178)

Co-authored-by: Marcin Tojek <marcin@coder.com>
This commit is contained in:
Cian Johnston
2023-12-13 12:31:40 +00:00
committed by GitHub
co-authored by Marcin Tojek
parent ef4d1b68e1
commit 4f7ae6461b
15 changed files with 210 additions and 98 deletions
+29 -13
View File
@@ -4,19 +4,6 @@ SELECT
FROM
provisioner_daemons;
-- name: InsertProvisionerDaemon :one
INSERT INTO
provisioner_daemons (
id,
created_at,
"name",
provisioners,
tags,
last_seen_at
)
VALUES
($1, $2, $3, $4, $5, $6) RETURNING *;
-- name: DeleteOldProvisionerDaemons :exec
-- Delete provisioner daemons that have been created at least a week ago
-- and have not connected to coderd since a week.
@@ -26,3 +13,32 @@ DELETE FROM provisioner_daemons WHERE (
(created_at < (NOW() - INTERVAL '7 days') AND last_seen_at IS NULL) OR
(last_seen_at IS NOT NULL AND last_seen_at < (NOW() - INTERVAL '7 days'))
);
-- name: UpsertProvisionerDaemon :one
INSERT INTO
provisioner_daemons (
id,
created_at,
"name",
provisioners,
tags,
last_seen_at,
"version"
)
VALUES (
gen_random_uuid(),
@created_at,
@name,
@provisioners,
@tags,
@last_seen_at,
@version
) ON CONFLICT("name", lower((tags ->> 'owner'::text))) DO UPDATE SET
provisioners = @provisioners,
tags = @tags,
last_seen_at = @last_seen_at,
"version" = @version
WHERE
-- Only ones with the same tags are allowed clobber
provisioner_daemons.tags <@ @tags :: jsonb
RETURNING *;