From f1e963bd89e737868a3a49358d714a288738c2e8 Mon Sep 17 00:00:00 2001 From: Michael Kozono Date: Thu, 7 Sep 2017 18:50:03 -0700 Subject: Add specs for deleting conflicting redirects --- .../delete_conflicting_redirect_routes.rb | 52 ---------------------- .../delete_conflicting_redirect_routes_range.rb | 52 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 52 deletions(-) delete mode 100644 lib/gitlab/background_migration/delete_conflicting_redirect_routes.rb create mode 100644 lib/gitlab/background_migration/delete_conflicting_redirect_routes_range.rb (limited to 'lib') diff --git a/lib/gitlab/background_migration/delete_conflicting_redirect_routes.rb b/lib/gitlab/background_migration/delete_conflicting_redirect_routes.rb deleted file mode 100644 index c33b6d513da..00000000000 --- a/lib/gitlab/background_migration/delete_conflicting_redirect_routes.rb +++ /dev/null @@ -1,52 +0,0 @@ -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 diff --git a/lib/gitlab/background_migration/delete_conflicting_redirect_routes_range.rb b/lib/gitlab/background_migration/delete_conflicting_redirect_routes_range.rb new file mode 100644 index 00000000000..a45fb6b6d0c --- /dev/null +++ b/lib/gitlab/background_migration/delete_conflicting_redirect_routes_range.rb @@ -0,0 +1,52 @@ +module Gitlab + module BackgroundMigration + class DeleteConflictingRedirectRoutesRange + 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::DeleteConflictingRedirectRoutesRange [#{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 -- cgit v1.2.1