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.
2
5
INSERT INTO
3
6
workspace_app_audit_sessions (
4
7
agent_id,
5
8
app_id,
6
9
user_id,
7
10
ip,
11
+ user_agent,
8
12
slug_or_port,
13
+ status_code,
9
14
started_at,
10
15
updated_at
11
16
)
@@ -17,25 +22,20 @@ VALUES
17
22
$4 ,
18
23
$5 ,
19
24
$6 ,
20
- $7
25
+ $7 ,
26
+ $8 ,
27
+ $9
21
28
)
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
+ WHENworkspace_app_audit_sessions .updated_at > NOW()- (@stale_interval_ms::bigint || ' ms' )::interval
36
+ THENworkspace_app_audit_sessions .started_at
37
+ ELSEEXCLUDED .started_at
38
+ END,
39
+ updated_at= EXCLUDED .updated_at
22
40
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 DISTINCTFROM @app_id
36
- AND user_id IS NOT DISTINCTFROM @user_id
37
- AND ip IS NOT DISTINCTFROM @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;