Commit 60561aca authored by Bob Van Landuyt's avatar Bob Van Landuyt Committed by Bob Van Landuyt

Update routes directly by ID instead of filtering by path

parent 79cdacc5
...@@ -6,6 +6,7 @@ module Gitlab ...@@ -6,6 +6,7 @@ module Gitlab
attr_reader :paths, :migration attr_reader :paths, :migration
delegate :update_column_in_batches, delegate :update_column_in_batches,
:execute,
:replace_sql, :replace_sql,
:say, :say,
to: :migration to: :migration
...@@ -42,14 +43,21 @@ module Gitlab ...@@ -42,14 +43,21 @@ module Gitlab
end end
def rename_routes(old_full_path, new_full_path) 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'] }
replace_statement = replace_sql(Route.arel_table[:path], replace_statement = replace_sql(Route.arel_table[:path],
old_full_path, old_full_path,
new_full_path) new_full_path)
update_column_in_batches(:routes, :path, replace_statement) do |table, query| update = Arel::UpdateManager.new(ActiveRecord::Base)
path_or_children = table[:path].matches_any([old_full_path, "#{old_full_path}/%"]) .table(routes)
query.where(path_or_children) .set([[routes[:path], replace_statement]])
end .where(routes[:id].in(ids))
execute(update.to_sql)
end end
def rename_path(namespace_path, path_was) def rename_path(namespace_path, path_was)
......
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