Commit 397d3fd7 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Only do one query for updating routes

parent 60561aca
...@@ -8,6 +8,7 @@ module Gitlab ...@@ -8,6 +8,7 @@ module Gitlab
delegate :update_column_in_batches, delegate :update_column_in_batches,
:execute, :execute,
:replace_sql, :replace_sql,
:quote_string,
:say, :say,
to: :migration to: :migration
...@@ -44,10 +45,18 @@ module Gitlab ...@@ -44,10 +45,18 @@ module Gitlab
def rename_routes(old_full_path, new_full_path) def rename_routes(old_full_path, new_full_path)
routes = Route.arel_table 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}/%")) quoted_old_full_path = quote_string(old_full_path)
matching_ids = main_route_ids.union(child_route_ids) quoted_old_wildcard_path = quote_string("#{old_full_path}/%")
ids = execute(matching_ids.to_sql).map { |entry| entry['id'] }
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], replace_statement = replace_sql(Route.arel_table[:path],
old_full_path, old_full_path,
...@@ -56,7 +65,8 @@ module Gitlab ...@@ -56,7 +65,8 @@ module Gitlab
update = Arel::UpdateManager.new(ActiveRecord::Base) update = Arel::UpdateManager.new(ActiveRecord::Base)
.table(routes) .table(routes)
.set([[routes[:path], replace_statement]]) .set([[routes[:path], replace_statement]])
.where(routes[:id].in(ids)) .where(Arel::Nodes::SqlLiteral.new(filter))
execute(update.to_sql) execute(update.to_sql)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment