diff --git a/coderd/database/dump.sql b/coderd/database/dump.sql index a96c622a03463..50a27bc0ede48 100644 --- a/coderd/database/dump.sql +++ b/coderd/database/dump.sql @@ -889,7 +889,7 @@ CREATE INDEX workspace_agents_auth_token_idx ON workspace_agents USING btree (au CREATE INDEX workspace_agents_resource_id_idx ON workspace_agents USING btree (resource_id); -CREATE UNIQUE INDEX workspace_proxies_name_idx ON workspace_proxies USING btree (name) WHERE (deleted = false); +CREATE UNIQUE INDEX workspace_proxies_lower_name_idx ON workspace_proxies USING btree (lower(name)) WHERE (deleted = false); CREATE INDEX workspace_resources_job_id_idx ON workspace_resources USING btree (job_id); diff --git a/coderd/database/migrations/000119_workspace_proxy_name_idx.down.sql b/coderd/database/migrations/000119_workspace_proxy_name_idx.down.sql new file mode 100644 index 0000000000000..3311a6cd7ce8c --- /dev/null +++ b/coderd/database/migrations/000119_workspace_proxy_name_idx.down.sql @@ -0,0 +1,8 @@ +BEGIN; + +DROP INDEX IF EXISTS workspace_proxies_lower_name_idx; + +-- Enforces no active proxies have the same name. +CREATE UNIQUE INDEX ON workspace_proxies (name) WHERE deleted = FALSE; + +COMMIT; diff --git a/coderd/database/migrations/000119_workspace_proxy_name_idx.up.sql b/coderd/database/migrations/000119_workspace_proxy_name_idx.up.sql new file mode 100644 index 0000000000000..0101905491672 --- /dev/null +++ b/coderd/database/migrations/000119_workspace_proxy_name_idx.up.sql @@ -0,0 +1,11 @@ +BEGIN; + +-- No one is using this feature yet as of writing this migration, so this is +-- fine. Just delete all workspace proxies to prevent the new index from having +-- conflicts. +DELETE FROM workspace_proxies; + +DROP INDEX IF EXISTS workspace_proxies_name_idx; +CREATE UNIQUE INDEX workspace_proxies_lower_name_idx ON workspace_proxies USING btree (lower(name)) WHERE deleted = FALSE; + +COMMIT; diff --git a/coderd/database/migrations/testdata/fixtures/000118_workspace_proxy_token.up.sql b/coderd/database/migrations/testdata/fixtures/000119_workspace_proxy_token.up.sql similarity index 100% rename from coderd/database/migrations/testdata/fixtures/000118_workspace_proxy_token.up.sql rename to coderd/database/migrations/testdata/fixtures/000119_workspace_proxy_token.up.sql diff --git a/coderd/database/unique_constraint.go b/coderd/database/unique_constraint.go index f0e564c81a5ee..f0ba6c702ac93 100644 --- a/coderd/database/unique_constraint.go +++ b/coderd/database/unique_constraint.go @@ -31,6 +31,6 @@ const ( UniqueTemplatesOrganizationIDNameIndex UniqueConstraint = "templates_organization_id_name_idx" // CREATE UNIQUE INDEX templates_organization_id_name_idx ON templates USING btree (organization_id, lower((name)::text)) WHERE (deleted = false); UniqueUsersEmailLowerIndex UniqueConstraint = "users_email_lower_idx" // CREATE UNIQUE INDEX users_email_lower_idx ON users USING btree (lower(email)) WHERE (deleted = false); UniqueUsersUsernameLowerIndex UniqueConstraint = "users_username_lower_idx" // CREATE UNIQUE INDEX users_username_lower_idx ON users USING btree (lower(username)) WHERE (deleted = false); - UniqueWorkspaceProxiesNameIndex UniqueConstraint = "workspace_proxies_name_idx" // CREATE UNIQUE INDEX workspace_proxies_name_idx ON workspace_proxies USING btree (name) WHERE (deleted = false); + UniqueWorkspaceProxiesLowerNameIndex UniqueConstraint = "workspace_proxies_lower_name_idx" // CREATE UNIQUE INDEX workspace_proxies_lower_name_idx ON workspace_proxies USING btree (lower(name)) WHERE (deleted = false); UniqueWorkspacesOwnerIDLowerIndex UniqueConstraint = "workspaces_owner_id_lower_idx" // CREATE UNIQUE INDEX workspaces_owner_id_lower_idx ON workspaces USING btree (owner_id, lower((name)::text)) WHERE (deleted = false); )