Files
coder/coderd/database/migrations/000315_preset_prebuilds.up.sql
Sas Swart 99c6f235eb 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>
2025-04-03 10:58:30 +02:00

20 lines
887 B
SQL

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);