From 8f569dfc7057b25dedb2edd9ef170eac2540b5ec Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Tue, 1 Nov 2022 16:32:29 -0500 Subject: [PATCH 1/2] fix: actually fix template version created by migration --- ...000068_update_template_version_created_by.up.sql | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/coderd/database/migrations/000068_update_template_version_created_by.up.sql b/coderd/database/migrations/000068_update_template_version_created_by.up.sql index a32163034383f..90d746ab557ef 100644 --- a/coderd/database/migrations/000068_update_template_version_created_by.up.sql +++ b/coderd/database/migrations/000068_update_template_version_created_by.up.sql @@ -1,6 +1,17 @@ BEGIN; -UPDATE template_versions SET created_by = '00000000-0000-0000-0000-000000000000'::uuid WHERE created_by IS NULL; +UPDATE + template_versions +SET + created_by = COALESCE( + -- Best effort to convert all unowned template versions to the first owner. + (SELECT id FROM users WHERE rbac_roles @> '{owner}' AND deleted = 'f' ORDER BY created_at ASC LIMIT 1), + -- If there are no owners, assign to the first user. + (SELECT id FROM users WHERE deleted = 'f' ORDER BY created_at ASC LIMIT 1) + ) +WHERE + created_by IS NULL; + ALTER TABLE template_versions ALTER COLUMN created_by SET NOT NULL; COMMIT; From 911d6e4f42116d2601ec70e0ff967115c4fce5c4 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Tue, 1 Nov 2022 16:38:45 -0500 Subject: [PATCH 2/2] fixup! fix: actually fix template version created by migration --- .../migrations/000068_update_template_version_created_by.up.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/coderd/database/migrations/000068_update_template_version_created_by.up.sql b/coderd/database/migrations/000068_update_template_version_created_by.up.sql index 90d746ab557ef..faeeb1bea637b 100644 --- a/coderd/database/migrations/000068_update_template_version_created_by.up.sql +++ b/coderd/database/migrations/000068_update_template_version_created_by.up.sql @@ -8,6 +8,7 @@ SET (SELECT id FROM users WHERE rbac_roles @> '{owner}' AND deleted = 'f' ORDER BY created_at ASC LIMIT 1), -- If there are no owners, assign to the first user. (SELECT id FROM users WHERE deleted = 'f' ORDER BY created_at ASC LIMIT 1) + -- If you have no users I'm not sure what else to tell you. ) WHERE created_by IS NULL;