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

Skip to content

Commit 3e52186

Browse files
fix: handle presets with the same tv.id and name
1 parent be27a98 commit 3e52186

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

coderd/database/migrations/000314_preset_prebuilds.up.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,18 @@ ALTER TABLE template_version_presets
22
ADD COLUMN desired_instances INT NULL,
33
ADD COLUMN invalidate_after_secs INT NULL DEFAULT 0;
44

5+
-- Ensure that the idx_unique_preset_name index creation won't fail.
6+
-- This is necessary because presets were released before the index was introduced,
7+
-- so existing data might violate the uniqueness constraint.
8+
WITH ranked AS (
9+
SELECT id, name, template_version_id,
10+
ROW_NUMBER() OVER (PARTITION BY name, template_version_id ORDER BY id) AS row_num
11+
FROM template_version_presets
12+
)
13+
UPDATE template_version_presets
14+
SET name = ranked.name || '_auto_' || row_num
15+
FROM ranked
16+
WHERE template_version_presets.id = ranked.id AND row_num > 1;
17+
518
-- We should not be able to have presets with the same name for a particular template version.
619
CREATE UNIQUE INDEX idx_unique_preset_name ON template_version_presets (name, template_version_id);

coderd/database/migrations/testdata/fixtures/000293_workspace_parameter_presets.up.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,26 @@ INSERT INTO public.template_versions (id, template_id, organization_id, created_
77

88
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');
99

10+
-- Add presets with the same template version ID and name
11+
-- to ensure they're correctly handled by the 00031*_preset_prebuilds migration.
12+
INSERT INTO public.template_version_presets (
13+
id, template_version_id, name, created_at
14+
)
15+
VALUES (
16+
'c9dd1a63-f0cf-446e-8d6f-2d29d7c8e38b',
17+
'af58bd62-428c-4c33-849b-d43a3be07d93',
18+
'duplicate_name',
19+
'0001-01-01 00:00:00.000000 +00:00'
20+
);
21+
22+
INSERT INTO public.template_version_presets (
23+
id, template_version_id, name, created_at
24+
)
25+
VALUES (
26+
'80f93d57-3948-487a-8990-bb011fb80a18',
27+
'af58bd62-428c-4c33-849b-d43a3be07d93',
28+
'duplicate_name',
29+
'0001-01-01 00:00:00.000000 +00:00'
30+
);
31+
1032
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 commit comments

Comments
 (0)