feat: add migrations and queries to support prebuilds (#16891)

Depends on https://github.com/coder/coder/pull/16916 _(change base to
`main` once it is merged)_

Closes https://github.com/coder/internal/issues/514

_This is one of several PRs to decompose the `dk/prebuilds` feature
branch into separate PRs to merge into `main`._

---------

Signed-off-by: Danny Kopping <dannykopping@gmail.com>
Co-authored-by: Danny Kopping <dannykopping@gmail.com>
Co-authored-by: evgeniy-scherbina <evgeniy.shcherbina.es@gmail.com>
This commit is contained in:
Sas Swart
2025-04-03 10:58:30 +02:00
committed by GitHub
co-authored by Danny Kopping evgeniy-scherbina
parent 4aa45a5c43
commit 99c6f235eb
22 changed files with 2110 additions and 59 deletions
@@ -0,0 +1,4 @@
-- Revert prebuild views
DROP VIEW IF EXISTS workspace_prebuild_builds;
DROP VIEW IF EXISTS workspace_prebuilds;
DROP VIEW IF EXISTS workspace_latest_builds;
@@ -0,0 +1,62 @@
-- workspace_latest_builds contains latest build for every workspace
CREATE VIEW workspace_latest_builds AS
SELECT DISTINCT ON (workspace_id)
wb.id,
wb.workspace_id,
wb.template_version_id,
wb.job_id,
wb.template_version_preset_id,
wb.transition,
wb.created_at,
pj.job_status
FROM workspace_builds wb
INNER JOIN provisioner_jobs pj ON wb.job_id = pj.id
ORDER BY wb.workspace_id, wb.build_number DESC;
-- workspace_prebuilds contains all prebuilt workspaces with corresponding agent information
-- (including lifecycle_state which indicates is agent ready or not) and corresponding preset_id for prebuild
CREATE VIEW workspace_prebuilds AS
WITH
-- All workspaces owned by the "prebuilds" user.
all_prebuilds AS (
SELECT w.id, w.name, w.template_id, w.created_at
FROM workspaces w
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0' -- The system user responsible for prebuilds.
),
-- We can't rely on the template_version_preset_id in the workspace_builds table because this value is only set on the
-- initial workspace creation. Subsequent stop/start transitions will not have a value for template_version_preset_id,
-- and therefore we can't rely on (say) the latest build's chosen template_version_preset_id.
--
-- See https://github.com/coder/internal/issues/398
workspaces_with_latest_presets AS (
SELECT DISTINCT ON (workspace_id) workspace_id, template_version_preset_id
FROM workspace_builds
WHERE template_version_preset_id IS NOT NULL
ORDER BY workspace_id, build_number DESC
),
-- workspaces_with_agents_status contains workspaces owned by the "prebuilds" user,
-- along with the readiness status of their agents.
-- A workspace is marked as 'ready' only if ALL of its agents are ready.
workspaces_with_agents_status AS (
SELECT w.id AS workspace_id,
BOOL_AND(wa.lifecycle_state = 'ready'::workspace_agent_lifecycle_state) AS ready
FROM workspaces w
INNER JOIN workspace_latest_builds wlb ON wlb.workspace_id = w.id
INNER JOIN workspace_resources wr ON wr.job_id = wlb.job_id
INNER JOIN workspace_agents wa ON wa.resource_id = wr.id
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0' -- The system user responsible for prebuilds.
GROUP BY w.id
),
current_presets AS (SELECT w.id AS prebuild_id, wlp.template_version_preset_id
FROM workspaces w
INNER JOIN workspaces_with_latest_presets wlp ON wlp.workspace_id = w.id
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0') -- The system user responsible for prebuilds.
SELECT p.id, p.name, p.template_id, p.created_at, COALESCE(a.ready, false) AS ready, cp.template_version_preset_id AS current_preset_id
FROM all_prebuilds p
LEFT JOIN workspaces_with_agents_status a ON a.workspace_id = p.id
INNER JOIN current_presets cp ON cp.prebuild_id = p.id;
CREATE VIEW workspace_prebuild_builds AS
SELECT id, workspace_id, template_version_id, transition, job_id, template_version_preset_id, build_number
FROM workspace_builds
WHERE initiator_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'; -- The system user responsible for prebuilds.
@@ -0,0 +1,5 @@
ALTER TABLE template_version_presets
DROP COLUMN desired_instances,
DROP COLUMN invalidate_after_secs;
DROP INDEX IF EXISTS idx_unique_preset_name;
@@ -0,0 +1,19 @@
ALTER TABLE template_version_presets
ADD COLUMN desired_instances INT NULL,
ADD COLUMN invalidate_after_secs INT NULL DEFAULT 0;
-- Ensure that the idx_unique_preset_name index creation won't fail.
-- This is necessary because presets were released before the index was introduced,
-- so existing data might violate the uniqueness constraint.
WITH ranked AS (
SELECT id, name, template_version_id,
ROW_NUMBER() OVER (PARTITION BY name, template_version_id ORDER BY id) AS row_num
FROM template_version_presets
)
UPDATE template_version_presets
SET name = ranked.name || '_auto_' || row_num
FROM ranked
WHERE template_version_presets.id = ranked.id AND row_num > 1;
-- We should not be able to have presets with the same name for a particular template version.
CREATE UNIQUE INDEX idx_unique_preset_name ON template_version_presets (name, template_version_id);
@@ -7,4 +7,26 @@ INSERT INTO public.template_versions (id, template_id, organization_id, created_
INSERT INTO public.template_version_presets (id, template_version_id, name, created_at) VALUES ('28b42cc0-c4fe-4907-a0fe-e4d20f1e9bfe', 'af58bd62-428c-4c33-849b-d43a3be07d93', 'test', '0001-01-01 00:00:00.000000 +00:00');
-- Add presets with the same template version ID and name
-- to ensure they're correctly handled by the 00031*_preset_prebuilds migration.
INSERT INTO public.template_version_presets (
id, template_version_id, name, created_at
)
VALUES (
'c9dd1a63-f0cf-446e-8d6f-2d29d7c8e38b',
'af58bd62-428c-4c33-849b-d43a3be07d93',
'duplicate_name',
'0001-01-01 00:00:00.000000 +00:00'
);
INSERT INTO public.template_version_presets (
id, template_version_id, name, created_at
)
VALUES (
'80f93d57-3948-487a-8990-bb011fb80a18',
'af58bd62-428c-4c33-849b-d43a3be07d93',
'duplicate_name',
'0001-01-01 00:00:00.000000 +00:00'
);
INSERT INTO public.template_version_preset_parameters (id, template_version_preset_id, name, value) VALUES ('ea90ccd2-5024-459e-87e4-879afd24de0f', '28b42cc0-c4fe-4907-a0fe-e4d20f1e9bfe', 'test', 'test');
@@ -0,0 +1,3 @@
UPDATE template_version_presets
SET desired_instances = 1
WHERE id = '28b42cc0-c4fe-4907-a0fe-e4d20f1e9bfe';