Skip to content

Commit 336c7b8

Browse files
committed
rewrite queries, single upsert
1 parent 217a0d3 commit 336c7b8

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed
Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
-- name: InsertWorkspaceAppAuditSession :one
1+
-- name: UpsertWorkspaceAppAuditSession :one
2+
--
3+
-- Insert a new workspace app audit session or update an existing one, if
4+
-- started_at is updated, it means the session has been restarted.
25
INSERT INTO
36
workspace_app_audit_sessions (
47
agent_id,
58
app_id,
69
user_id,
710
ip,
11+
user_agent,
812
slug_or_port,
13+
status_code,
914
started_at,
1015
updated_at
1116
)
@@ -17,25 +22,20 @@ VALUES
1722
$4,
1823
$5,
1924
$6,
20-
$7
25+
$7,
26+
$8,
27+
$9
2128
)
29+
ON CONFLICT
30+
(agent_id, app_id, user_id, ip, user_agent, slug_or_port, status_code)
31+
DO
32+
UPDATE
33+
SET
34+
started_at = CASE
35+
WHEN workspace_app_audit_sessions.updated_at > NOW() - (@stale_interval_ms::bigint || ' ms')::interval
36+
THEN workspace_app_audit_sessions.started_at
37+
ELSE EXCLUDED.started_at
38+
END,
39+
updated_at = EXCLUDED.updated_at
2240
RETURNING
23-
id;
24-
25-
-- name: UpdateWorkspaceAppAuditSession :many
26-
--
27-
-- Return ID to determine if a row was updated or not. This table isn't strict
28-
-- about uniqueness, so we need to know if we updated an existing row or not.
29-
UPDATE
30-
workspace_app_audit_sessions
31-
SET
32-
updated_at = @updated_at
33-
WHERE
34-
agent_id = @agent_id
35-
AND app_id IS NOT DISTINCT FROM @app_id
36-
AND user_id IS NOT DISTINCT FROM @user_id
37-
AND ip IS NOT DISTINCT FROM @ip
38-
AND slug_or_port = @slug_or_port
39-
AND updated_at > NOW() - (@stale_interval_ms::bigint || ' ms')::interval
40-
RETURNING
41-
id;
41+
started_at;

0 commit comments

Comments
 (0)