Skip to content

Commit 89ebab2

Browse files
committed
Provide basic durability against multiple deletions
1 parent 89f0a11 commit 89ebab2

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

coderd/database/queries.sql.go

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

coderd/database/queries/insights.sql

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -816,15 +816,15 @@ latest_status_before_range AS (
816816
SELECT
817817
DISTINCT usc.user_id,
818818
usc.new_status,
819-
usc.changed_at
819+
usc.changed_at,
820+
ud.deleted
820821
FROM user_status_changes usc
821822
LEFT JOIN LATERAL (
822823
SELECT COUNT(*) > 0 AS deleted
823824
FROM user_deleted ud
824-
WHERE ud.user_id = usc.user_id AND ud.changed_at < usc.changed_at
825-
) AS ud ON usc.user_id = ud.user_id
825+
WHERE ud.user_id = usc.user_id AND (ud.deleted_at < usc.changed_at OR ud.deleted_at < @start_time)
826+
) AS ud ON true
826827
WHERE usc.changed_at < @start_time::timestamptz
827-
AND NOT ud.deleted
828828
ORDER BY usc.user_id, usc.changed_at DESC
829829
),
830830
-- status_changes_during_range defines the status of each user during the start_time and end_time.
@@ -835,16 +835,16 @@ status_changes_during_range AS (
835835
SELECT
836836
usc.user_id,
837837
usc.new_status,
838-
usc.changed_at
838+
usc.changed_at,
839+
ud.deleted
839840
FROM user_status_changes usc
840841
LEFT JOIN LATERAL (
841842
SELECT COUNT(*) > 0 AS deleted
842843
FROM user_deleted ud
843-
WHERE ud.user_id = usc.user_id AND ud.changed_at < usc.changed_at
844-
) AS ud ON usc.user_id = ud.user_id
844+
WHERE ud.user_id = usc.user_id AND ud.deleted_at < usc.changed_at
845+
) AS ud ON true
845846
WHERE usc.changed_at >= @start_time::timestamptz
846847
AND usc.changed_at <= @end_time::timestamptz
847-
AND NOT deleted
848848
),
849849
-- relevant_status_changes defines the status of each user at any point in time.
850850
-- It includes the status of each user before the start_time, and the status of each user during the start_time and end_time.
@@ -854,6 +854,7 @@ relevant_status_changes AS (
854854
new_status,
855855
changed_at
856856
FROM latest_status_before_range
857+
WHERE NOT deleted
857858

858859
UNION ALL
859860

@@ -862,6 +863,7 @@ relevant_status_changes AS (
862863
new_status,
863864
changed_at
864865
FROM status_changes_during_range
866+
WHERE NOT deleted
865867
),
866868
-- statuses defines all the distinct statuses that were present just before and during the time range.
867869
-- This is used to ensure that we have a series for every relevant status.

0 commit comments

Comments
 (0)