diff options
author | Bob Van Landuyt <bob@gitlab.com> | 2017-04-14 20:29:22 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@gitlab.com> | 2017-05-01 12:14:24 +0300 |
commit | 27f54bebb29a1e56251ac2d669c2883aeaf1cb1c (patch) | |
tree | 44468ae50d5c43d83ae02085d3cb340b88c29351 /lib/gitlab | |
parent | 0369ef14525cac86b015a21fa0d01b1cad627fc1 (diff) |
Use objects for renaming namespaces and projects
Diffstat (limited to 'lib/gitlab')
-rw-r--r-- | lib/gitlab/database/rename_reserved_paths_migration.rb | 97 | ||||
-rw-r--r-- | lib/gitlab/database/rename_reserved_paths_migration/migration_classes.rb | 1 | ||||
-rw-r--r-- | lib/gitlab/database/rename_reserved_paths_migration/rename_base.rb | 103 | ||||
-rw-r--r-- | lib/gitlab/database/rename_reserved_paths_migration/rename_namespaces.rb (renamed from lib/gitlab/database/rename_reserved_paths_migration/namespaces.rb) | 10 | ||||
-rw-r--r-- | lib/gitlab/database/rename_reserved_paths_migration/rename_projects.rb (renamed from lib/gitlab/database/rename_reserved_paths_migration/projects.rb) | 8 |
5 files changed, 119 insertions, 100 deletions
diff --git a/lib/gitlab/database/rename_reserved_paths_migration.rb b/lib/gitlab/database/rename_reserved_paths_migration.rb index 5314f933435..0507ae4da51 100644 --- a/lib/gitlab/database/rename_reserved_paths_migration.rb +++ b/lib/gitlab/database/rename_reserved_paths_migration.rb @@ -1,104 +1,19 @@ module Gitlab module Database module RenameReservedPathsMigration - include MigrationHelpers - include Namespaces - include Projects + def self.included(kls) + kls.include(MigrationHelpers) + end def rename_wildcard_paths(one_or_more_paths) paths = Array(one_or_more_paths) - rename_namespaces(paths, type: :wildcard) - rename_projects(paths) + RenameNamespaces.new(paths, self).rename_namespaces(type: :wildcard) + RenameProjects.new(paths, self).rename_projects end def rename_root_paths(paths) paths = Array(paths) - rename_namespaces(paths, type: :top_level) - end - - def rename_path_for_routable(routable) - old_path = routable.path - old_full_path = routable.full_path - # Only remove the last occurrence of the path name to get the parent namespace path - namespace_path = remove_last_occurrence(old_full_path, old_path) - new_path = rename_path(namespace_path, old_path) - new_full_path = join_routable_path(namespace_path, new_path) - - # skips callbacks & validations - routable.class.where(id: routable). - update_all(path: new_path) - - rename_routes(old_full_path, new_full_path) - - [old_full_path, new_full_path] - end - - def rename_routes(old_full_path, new_full_path) - replace_statement = replace_sql(Route.arel_table[:path], - old_full_path, - new_full_path) - - update_column_in_batches(:routes, :path, replace_statement) do |table, query| - query.where(MigrationClasses::Route.arel_table[:path].matches("#{old_full_path}%")) - end - end - - def rename_path(namespace_path, path_was) - counter = 0 - path = "#{path_was}#{counter}" - - while route_exists?(join_routable_path(namespace_path, path)) - counter += 1 - path = "#{path_was}#{counter}" - end - - path - end - - def remove_last_occurrence(string, pattern) - string.reverse.sub(pattern.reverse, "").reverse - end - - def join_routable_path(namespace_path, top_level) - if namespace_path.present? - File.join(namespace_path, top_level) - else - top_level - end - end - - def route_exists?(full_path) - MigrationClasses::Route.where(Route.arel_table[:path].matches(full_path)).any? - end - - def move_pages(old_path, new_path) - move_folders(pages_dir, old_path, new_path) - end - - def move_uploads(old_path, new_path) - return unless file_storage? - - move_folders(uploads_dir, old_path, new_path) - end - - def move_folders(directory, old_relative_path, new_relative_path) - old_path = File.join(directory, old_relative_path) - return unless File.directory?(old_path) - - new_path = File.join(directory, new_relative_path) - FileUtils.mv(old_path, new_path) - end - - def file_storage? - CarrierWave::Uploader::Base.storage == CarrierWave::Storage::File - end - - def uploads_dir - File.join(CarrierWave.root, "uploads") - end - - def pages_dir - Settings.pages.path + RenameNamespaces.new(paths, self).rename_namespaces(type: :top_level) end end end diff --git a/lib/gitlab/database/rename_reserved_paths_migration/migration_classes.rb b/lib/gitlab/database/rename_reserved_paths_migration/migration_classes.rb index d725402ace4..9133b97d239 100644 --- a/lib/gitlab/database/rename_reserved_paths_migration/migration_classes.rb +++ b/lib/gitlab/database/rename_reserved_paths_migration/migration_classes.rb @@ -76,6 +76,7 @@ module Gitlab class Project < ActiveRecord::Base include MigrationClasses::Routable + has_one :route, as: :source self.table_name = 'projects' def repository_storage_path diff --git a/lib/gitlab/database/rename_reserved_paths_migration/rename_base.rb b/lib/gitlab/database/rename_reserved_paths_migration/rename_base.rb new file mode 100644 index 00000000000..367348a9a42 --- /dev/null +++ b/lib/gitlab/database/rename_reserved_paths_migration/rename_base.rb @@ -0,0 +1,103 @@ +module Gitlab + module Database + module RenameReservedPathsMigration + class RenameBase + attr_reader :paths, :migration + + delegate :update_column_in_batches, + :replace_sql, + to: :migration + + def initialize(paths, migration) + @paths = paths + @migration = migration + end + + def rename_path_for_routable(routable) + old_path = routable.path + old_full_path = routable.full_path + # Only remove the last occurrence of the path name to get the parent namespace path + namespace_path = remove_last_occurrence(old_full_path, old_path) + new_path = rename_path(namespace_path, old_path) + new_full_path = join_routable_path(namespace_path, new_path) + + # skips callbacks & validations + routable.class.where(id: routable). + update_all(path: new_path) + + rename_routes(old_full_path, new_full_path) + + [old_full_path, new_full_path] + end + + def rename_routes(old_full_path, new_full_path) + replace_statement = replace_sql(Route.arel_table[:path], + old_full_path, + new_full_path) + + update_column_in_batches(:routes, :path, replace_statement) do |table, query| + query.where(MigrationClasses::Route.arel_table[:path].matches("#{old_full_path}%")) + end + end + + def rename_path(namespace_path, path_was) + counter = 0 + path = "#{path_was}#{counter}" + + while route_exists?(join_routable_path(namespace_path, path)) + counter += 1 + path = "#{path_was}#{counter}" + end + + path + end + + def remove_last_occurrence(string, pattern) + string.reverse.sub(pattern.reverse, "").reverse + end + + def join_routable_path(namespace_path, top_level) + if namespace_path.present? + File.join(namespace_path, top_level) + else + top_level + end + end + + def route_exists?(full_path) + MigrationClasses::Route.where(Route.arel_table[:path].matches(full_path)).any? + end + + def move_pages(old_path, new_path) + move_folders(pages_dir, old_path, new_path) + end + + def move_uploads(old_path, new_path) + return unless file_storage? + + move_folders(uploads_dir, old_path, new_path) + end + + def move_folders(directory, old_relative_path, new_relative_path) + old_path = File.join(directory, old_relative_path) + return unless File.directory?(old_path) + + new_path = File.join(directory, new_relative_path) + FileUtils.mv(old_path, new_path) + end + + def file_storage? + CarrierWave::Uploader::Base.storage == CarrierWave::Storage::File + end + + def uploads_dir + File.join(CarrierWave.root, "uploads") + end + + def pages_dir + Settings.pages.path + end + end + end + end +end diff --git a/lib/gitlab/database/rename_reserved_paths_migration/namespaces.rb b/lib/gitlab/database/rename_reserved_paths_migration/rename_namespaces.rb index 2ef2629f4c2..80e8135ea93 100644 --- a/lib/gitlab/database/rename_reserved_paths_migration/namespaces.rb +++ b/lib/gitlab/database/rename_reserved_paths_migration/rename_namespaces.rb @@ -1,16 +1,16 @@ module Gitlab module Database module RenameReservedPathsMigration - module Namespaces + class RenameNamespaces < RenameBase include Gitlab::ShellAdapter - def rename_namespaces(paths, type:) - namespaces_for_paths(paths, type: type).each do |namespace| + def rename_namespaces(type:) + namespaces_for_paths(type: type).each do |namespace| rename_namespace(namespace) end end - def namespaces_for_paths(paths, type:) + def namespaces_for_paths(type:) namespaces = if type == :wildcard MigrationClasses::Namespace.where.not(parent_id: nil) elsif type == :top_level @@ -52,7 +52,7 @@ module Gitlab namespace_or_children = MigrationClasses::Project. arel_table[:namespace_id]. in(namespace_ids) - MigrationClasses::Project.unscoped.where(namespace_or_children) + MigrationClasses::Project.where(namespace_or_children) end # This won't scale to huge trees, but it should do for a handful of diff --git a/lib/gitlab/database/rename_reserved_paths_migration/projects.rb b/lib/gitlab/database/rename_reserved_paths_migration/rename_projects.rb index a2c9354e430..02f10d8e951 100644 --- a/lib/gitlab/database/rename_reserved_paths_migration/projects.rb +++ b/lib/gitlab/database/rename_reserved_paths_migration/rename_projects.rb @@ -1,11 +1,11 @@ module Gitlab module Database module RenameReservedPathsMigration - module Projects + class RenameProjects < RenameBase include Gitlab::ShellAdapter - def rename_projects(paths) - projects_for_paths(paths).each do |project| + def rename_projects + projects_for_paths.each do |project| rename_project(project) end end @@ -27,7 +27,7 @@ module Gitlab end end - def projects_for_paths(paths) + def projects_for_paths with_paths = MigrationClasses::Project.arel_table[:path] .matches_any(paths) MigrationClasses::Project.where(with_paths) |