diff options
author | Bob Van Landuyt <bob@gitlab.com> | 2017-04-13 17:54:48 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@gitlab.com> | 2017-05-01 12:14:24 +0300 |
commit | 7508ee56670dd960275b6438be91471020ea62ab (patch) | |
tree | 7e0363a1c0726da36275deab494daa5b63514eaf /lib | |
parent | e3d6957812e6cf399c208b1109ccc81ee1ff9144 (diff) |
Make renaming records in the database reusable
So we can use it for projects
Diffstat (limited to 'lib')
3 files changed, 71 insertions, 48 deletions
diff --git a/lib/gitlab/database/rename_reserved_paths_migration.rb b/lib/gitlab/database/rename_reserved_paths_migration.rb index ad5570b4c72..2cfc01ab2f5 100644 --- a/lib/gitlab/database/rename_reserved_paths_migration.rb +++ b/lib/gitlab/database/rename_reserved_paths_migration.rb @@ -15,11 +15,38 @@ module Gitlab 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?(File.join(namespace_path, path)) + while route_exists?(join_routable_path(namespace_path, path)) counter += 1 path = "#{path_was}#{counter}" end @@ -27,6 +54,18 @@ module Gitlab 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 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 a919d250541..d725402ace4 100644 --- a/lib/gitlab/database/rename_reserved_paths_migration/migration_classes.rb +++ b/lib/gitlab/database/rename_reserved_paths_migration/migration_classes.rb @@ -2,26 +2,7 @@ module Gitlab module Database module RenameReservedPathsMigration module MigrationClasses - class User < ActiveRecord::Base - self.table_name = 'users' - end - - class Namespace < ActiveRecord::Base - self.table_name = 'namespaces' - belongs_to :parent, - class_name: "#{MigrationClasses.name}::Namespace" - has_one :route, as: :source - has_many :children, - class_name: "#{MigrationClasses.name}::Namespace", - foreign_key: :parent_id - belongs_to :owner, - class_name: "#{MigrationClasses.name}::User" - - # Overridden to have the correct `source_type` for the `route` relation - def self.name - 'Namespace' - end - + module Routable def full_path if route && route.path.present? @full_path ||= route.path @@ -66,17 +47,45 @@ module Gitlab end end + class User < ActiveRecord::Base + self.table_name = 'users' + end + + class Namespace < ActiveRecord::Base + include MigrationClasses::Routable + self.table_name = 'namespaces' + belongs_to :parent, + class_name: "#{MigrationClasses.name}::Namespace" + has_one :route, as: :source + has_many :children, + class_name: "#{MigrationClasses.name}::Namespace", + foreign_key: :parent_id + belongs_to :owner, + class_name: "#{MigrationClasses.name}::User" + + # Overridden to have the correct `source_type` for the `route` relation + def self.name + 'Namespace' + end + end + class Route < ActiveRecord::Base self.table_name = 'routes' belongs_to :source, polymorphic: true end class Project < ActiveRecord::Base + include MigrationClasses::Routable self.table_name = 'projects' def repository_storage_path Gitlab.config.repositories.storages[repository_storage]['path'] end + + # Overridden to have the correct `source_type` for the `route` relation + def self.name + 'Project' + end end end end diff --git a/lib/gitlab/database/rename_reserved_paths_migration/namespaces.rb b/lib/gitlab/database/rename_reserved_paths_migration/namespaces.rb index f8fbeaa990a..b4f2a67fd06 100644 --- a/lib/gitlab/database/rename_reserved_paths_migration/namespaces.rb +++ b/lib/gitlab/database/rename_reserved_paths_migration/namespaces.rb @@ -16,32 +16,11 @@ module Gitlab elsif type == :top_level MigrationClasses::Namespace.where(parent_id: nil) end - namespaces.where(path: paths.map(&:downcase)) + namespaces.where('lower(path) in (?)', paths.map(&:downcase)) end def rename_namespace(namespace) - old_path = namespace.path - old_full_path = namespace.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 = if namespace_path.present? - File.join(namespace_path, new_path) - else - new_path - end - - # skips callbacks & validations - MigrationClasses::Namespace.where(id: namespace). - update_all(path: new_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 + old_full_path, new_full_path = rename_path_for_routable(namespace) move_repositories(namespace, old_full_path, new_full_path) move_namespace_folders(uploads_dir, old_full_path, new_full_path) if file_storage? @@ -92,10 +71,6 @@ module Gitlab ids end - def remove_last_occurrence(string, pattern) - string.reverse.sub(pattern.reverse, "").reverse - end - def file_storage? CarrierWave::Uploader::Base.storage == CarrierWave::Storage::File end |