Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b2f831

Browse files
committed
Fix: refactor CASE logic
1 parent ec2d571 commit 4b2f831

File tree

2 files changed

+30
-46
lines changed

2 files changed

+30
-46
lines changed

coderd/database/queries.sql.go

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

coderd/database/queries/workspaces.sql

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -184,29 +184,21 @@ WHERE
184184
WHEN @has_agent :: text != '' THEN
185185
latest_build.transition = 'start'::workspace_transition
186186
AND CASE
187-
WHEN @has_agent = 'timeout' THEN
188-
latest_build.agent_first_connected_at IS NULL AND
189-
(latest_build.agent_created_at + latest_build.agent_connection_timeout_seconds * INTERVAL '1 second' < NOW())
190-
WHEN @has_agent = 'connecting' THEN
191-
latest_build.agent_first_connected_at IS NULL AND
192-
(latest_build.agent_created_at + latest_build.agent_connection_timeout_seconds * INTERVAL '1 second' >= NOW())
193-
WHEN @has_agent = 'disconnected' THEN
194-
(
195-
latest_build.agent_disconnected_at IS NOT NULL AND
196-
latest_build.agent_disconnected_at > latest_build.agent_last_connected_at
197-
) OR (
198-
latest_build.agent_last_connected_at IS NOT NULL AND
199-
latest_build.agent_last_connected_at + INTERVAL '1 second' * @agent_inactive_disconnect_timeout_seconds :: bigint < NOW()
200-
)
201-
WHEN @has_agent = 'connected' THEN
202-
(
203-
latest_build.agent_disconnected_at IS NOT NULL AND
204-
latest_build.agent_disconnected_at <= latest_build.agent_last_connected_at
205-
) OR (
206-
latest_build.agent_last_connected_at IS NOT NULL AND
207-
latest_build.agent_last_connected_at + INTERVAL '1 second' * @agent_inactive_disconnect_timeout_seconds :: bigint >= NOW()
208-
)
209-
ELSE true
187+
WHEN latest_build.agent_first_connected_at IS NULL THEN
188+
CASE
189+
WHEN latest_build.agent_connection_timeout_seconds > 0 AND NOW() - latest_build.agent_created_at > latest_build.agent_connection_timeout_seconds * INTERVAL '1 second' THEN
190+
CASE WHEN @has_agent :: text = 'timeout' THEN true ELSE false END
191+
ELSE
192+
CASE WHEN @has_agent :: text = 'connecting' THEN true ELSE false END
193+
END
194+
WHEN latest_build.agent_disconnected_at > latest_build.agent_last_connected_at THEN
195+
CASE WHEN @has_agent :: text = 'disconnected' THEN true ELSE false END
196+
WHEN NOW() - latest_build.agent_last_connected_at > INTERVAL '1 second' * @agent_inactive_disconnect_timeout_seconds :: bigint THEN
197+
CASE WHEN @has_agent :: text = 'disconnected' THEN true ELSE false END
198+
WHEN latest_build.agent_last_connected_at IS NOT NULL THEN
199+
CASE WHEN @has_agent :: text = 'connected' THEN true ELSE false END
200+
ELSE
201+
true
210202
END
211203
ELSE true
212204
END

0 commit comments

Comments
 (0)