chore: improve build deadline code (#19203)

- Adds/improves a lot of comments to make the autostop calculation code
clearer
- Changes the behavior of the enterprise template schedule store to
match the behavior of the workspace TTL endpoint when the new TTL is
zero
- Fixes a bug in the workspace TTL endpoint where it could unset the
build deadline, even though a max_deadline was specified
- Adds a new constraint to the workspace_builds table that enforces the
deadline is non-zero and below the max_deadline if it is set
- Adds CHECK constraint enum generation to scripts/dbgen, used for
testing the above constraint
- Adds Dean and Danielle as CODEOWNERS for the autostop calculation code
This commit is contained in:
Dean Sheather
2025-08-07 11:00:31 +10:00
committed by GitHub
parent ec3b8cea91
commit dc598856e3
16 changed files with 660 additions and 258 deletions
@@ -0,0 +1,2 @@
ALTER TABLE workspace_builds
DROP CONSTRAINT workspace_builds_deadline_below_max_deadline;
@@ -0,0 +1,20 @@
-- New constraint: (deadline IS NOT zero AND deadline <= max_deadline) UNLESS max_deadline is zero.
-- Unfortunately, "zero" here means `time.Time{}`...
-- Update previous builds that would fail this new constraint. This matches the
-- intended behaviour of the autostop algorithm.
UPDATE
workspace_builds
SET
deadline = max_deadline
WHERE
deadline > max_deadline
AND max_deadline != '0001-01-01 00:00:00+00';
-- Add the new constraint.
ALTER TABLE workspace_builds
ADD CONSTRAINT workspace_builds_deadline_below_max_deadline
CHECK (
(deadline != '0001-01-01 00:00:00+00'::timestamptz AND deadline <= max_deadline)
OR max_deadline = '0001-01-01 00:00:00+00'::timestamptz
);