diff options
author | Michael Kozono <mkozono@gmail.com> | 2017-09-07 20:58:50 +0300 |
---|---|---|
committer | Michael Kozono <mkozono@gmail.com> | 2017-09-15 00:17:22 +0300 |
commit | bedcb7f43ddb8d6e2cce9424e3d988fe1d5a7cc8 (patch) | |
tree | 4fc98ac4b7c4f66f7e3c617da59c6e79cffbfbaf /lib/gitlab/background_migration | |
parent | ec3b3797e789d82200de2694458cc7cde2093549 (diff) |
Delete conflicting redirects in background
Diffstat (limited to 'lib/gitlab/background_migration')
-rw-r--r-- | lib/gitlab/background_migration/delete_conflicting_redirect_routes.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/delete_conflicting_redirect_routes.rb b/lib/gitlab/background_migration/delete_conflicting_redirect_routes.rb new file mode 100644 index 00000000000..c33b6d513da --- /dev/null +++ b/lib/gitlab/background_migration/delete_conflicting_redirect_routes.rb @@ -0,0 +1,52 @@ +module Gitlab + module BackgroundMigration + class DeleteConflictingRedirectRoutes + class Route < ActiveRecord::Base + self.table_name = 'routes' + end + + class RedirectRoute < ActiveRecord::Base + self.table_name = 'redirect_routes' + end + + # start_id - The start ID of the range of events to process + # end_id - The end ID of the range to process. + def perform(start_id, end_id) + return unless migrate? + + conflicts = RedirectRoute.where(routes_match_redirects_clause(start_id, end_id)) + num_rows = conflicts.delete_all + + Rails.logger.info("Gitlab::BackgroundMigration::DeleteConflictingRedirectRoutes [#{start_id}, #{end_id}] - Deleted #{num_rows} redirect routes that were conflicting with routes.") + end + + def migrate? + Route.table_exists? && RedirectRoute.table_exists? + end + + def routes_match_redirects_clause(start_id, end_id) + <<~ROUTES_MATCH_REDIRECTS + EXISTS ( + SELECT 1 FROM routes + WHERE (#{route_paths_match_redirects}) + AND routes.id BETWEEN #{start_id} AND #{end_id} + ) + ROUTES_MATCH_REDIRECTS + end + + def route_paths_match_redirects + if Gitlab::Database.postgresql? + <<~ROUTE_PATHS_MATCH_REDIRECTS + LOWER(redirect_routes.path) = LOWER(routes.path) + OR LOWER(redirect_routes.path) LIKE LOWER(CONCAT(routes.path, '/%')) + ROUTE_PATHS_MATCH_REDIRECTS + else + <<~ROUTE_PATHS_MATCH_REDIRECTS + redirect_routes.path = routes.path + OR redirect_routes.path LIKE CONCAT(routes.path, '/%') + ROUTE_PATHS_MATCH_REDIRECTS + end + end + end + end +end |