From 397d3fd7d0cd352c94637a26b5ed25c025aa8bf9 Mon Sep 17 00:00:00 2001 From: Bob Van Landuyt Date: Mon, 26 Jun 2017 17:45:32 +0200 Subject: Only do one query for updating routes --- .../v1/rename_base.rb | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'lib/gitlab/database/rename_reserved_paths_migration') diff --git a/lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb b/lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb index c7ce9749eba..c696c8baf7e 100644 --- a/lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb +++ b/lib/gitlab/database/rename_reserved_paths_migration/v1/rename_base.rb @@ -8,6 +8,7 @@ module Gitlab delegate :update_column_in_batches, :execute, :replace_sql, + :quote_string, :say, to: :migration @@ -44,10 +45,18 @@ module Gitlab def rename_routes(old_full_path, new_full_path) routes = Route.arel_table - main_route_ids = routes.project(routes[:id]).where(routes[:path].matches(old_full_path)) - child_route_ids = routes.project(routes[:id]).where(routes[:path].matches("#{old_full_path}/%")) - matching_ids = main_route_ids.union(child_route_ids) - ids = execute(matching_ids.to_sql).map { |entry| entry['id'] } + + quoted_old_full_path = quote_string(old_full_path) + quoted_old_wildcard_path = quote_string("#{old_full_path}/%") + + filter = if Database.mysql? + "lower(routes.path) = lower('#{quoted_old_full_path}') "\ + "OR routes.path LIKE '#{quoted_old_wildcard_path}'" + else + "routes.id IN "\ + "( SELECT routes.id FROM routes WHERE lower(routes.path) = lower('#{quoted_old_full_path}') "\ + "UNION SELECT routes.id FROM routes WHERE routes.path ILIKE '#{quoted_old_wildcard_path}' )" + end replace_statement = replace_sql(Route.arel_table[:path], old_full_path, @@ -56,7 +65,8 @@ module Gitlab update = Arel::UpdateManager.new(ActiveRecord::Base) .table(routes) .set([[routes[:path], replace_statement]]) - .where(routes[:id].in(ids)) + .where(Arel::Nodes::SqlLiteral.new(filter)) + execute(update.to_sql) end -- cgit v1.2.3