2018-11-13 02:27:31 -05:00
|
|
|
class RenameReservedProjectNames < ActiveRecord::Migration[4.2]
|
2016-12-21 12:31:30 -05:00
|
|
|
include Gitlab::Database::MigrationHelpers
|
|
|
|
|
|
|
|
DOWNTIME = false
|
|
|
|
|
2016-12-23 10:18:36 -05:00
|
|
|
THREAD_COUNT = 8
|
|
|
|
|
|
|
|
KNOWN_PATHS = %w(.well-known
|
|
|
|
all
|
|
|
|
blame
|
|
|
|
blob
|
|
|
|
commits
|
|
|
|
create
|
|
|
|
create_dir
|
|
|
|
edit
|
|
|
|
files
|
|
|
|
find_file
|
|
|
|
groups
|
|
|
|
hooks
|
|
|
|
issues
|
|
|
|
logs_tree
|
|
|
|
merge_requests
|
|
|
|
new
|
|
|
|
preview
|
|
|
|
projects
|
|
|
|
raw
|
|
|
|
repository
|
|
|
|
robots.txt
|
|
|
|
s
|
|
|
|
snippets
|
|
|
|
teams
|
|
|
|
tree
|
|
|
|
u
|
|
|
|
unsubscribes
|
|
|
|
update
|
|
|
|
users
|
2017-02-21 18:32:18 -05:00
|
|
|
wikis).freeze
|
2016-12-21 12:31:30 -05:00
|
|
|
|
|
|
|
def up
|
2016-12-23 10:18:36 -05:00
|
|
|
queues = Array.new(THREAD_COUNT) { Queue.new }
|
|
|
|
start = false
|
|
|
|
|
|
|
|
threads = Array.new(THREAD_COUNT) do |index|
|
2016-12-21 12:31:30 -05:00
|
|
|
Thread.new do
|
2016-12-23 10:18:36 -05:00
|
|
|
queue = queues[index]
|
|
|
|
|
|
|
|
# Wait until we have input to process.
|
|
|
|
until start; end
|
|
|
|
|
|
|
|
rename_projects(queue.pop) until queue.empty?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
enum = queues.each
|
|
|
|
|
|
|
|
reserved_projects.each_slice(100) do |slice|
|
|
|
|
begin
|
|
|
|
queue = enum.next
|
|
|
|
rescue StopIteration
|
|
|
|
enum.rewind
|
|
|
|
retry
|
2016-12-21 12:31:30 -05:00
|
|
|
end
|
2016-12-23 10:18:36 -05:00
|
|
|
|
|
|
|
queue << slice
|
2016-12-21 12:31:30 -05:00
|
|
|
end
|
|
|
|
|
2016-12-23 10:18:36 -05:00
|
|
|
start = true
|
|
|
|
|
2016-12-21 12:31:30 -05:00
|
|
|
threads.each(&:join)
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
# nothing to do here
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def reserved_projects
|
2017-06-21 09:48:12 -04:00
|
|
|
Project.unscoped
|
|
|
|
.includes(:namespace)
|
|
|
|
.where('EXISTS (SELECT 1 FROM namespaces WHERE projects.namespace_id = namespaces.id)')
|
|
|
|
.where('projects.path' => KNOWN_PATHS)
|
2016-12-21 12:31:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def route_exists?(full_path)
|
2016-12-23 10:18:36 -05:00
|
|
|
quoted_path = ActiveRecord::Base.connection.quote_string(full_path)
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
ActiveRecord::Base.connection
|
|
|
|
.select_all("SELECT id, path FROM routes WHERE path = '#{quoted_path}'").present?
|
2016-12-21 12:31:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Adds number to the end of the path that is not taken by other route
|
|
|
|
def rename_path(namespace_path, path_was)
|
|
|
|
counter = 0
|
|
|
|
path = "#{path_was}#{counter}"
|
|
|
|
|
|
|
|
while route_exists?("#{namespace_path}/#{path}")
|
|
|
|
counter += 1
|
|
|
|
path = "#{path_was}#{counter}"
|
|
|
|
end
|
|
|
|
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
|
|
|
def rename_projects(projects)
|
2016-12-23 10:18:36 -05:00
|
|
|
projects.each do |project|
|
|
|
|
id = project.id
|
|
|
|
path_was = project.path
|
|
|
|
namespace_path = project.namespace.path
|
2016-12-21 12:31:30 -05:00
|
|
|
path = rename_path(namespace_path, path_was)
|
|
|
|
|
|
|
|
begin
|
|
|
|
# Because project path update is quite complex operation we can't safely
|
|
|
|
# copy-paste all code from GitLab. As exception we use Rails code here
|
2018-10-17 07:33:54 -04:00
|
|
|
if rename_project_row(project, path)
|
|
|
|
Projects::AfterRenameService.new(project).execute
|
|
|
|
end
|
2016-12-23 10:18:36 -05:00
|
|
|
rescue Exception => e # rubocop: disable Lint/RescueException
|
|
|
|
Rails.logger.error "Exception when renaming project #{id}: #{e.message}"
|
2016-12-21 12:31:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-23 10:18:36 -05:00
|
|
|
|
|
|
|
def rename_project_row(project, path)
|
|
|
|
project.respond_to?(:update_attributes) &&
|
2018-07-25 04:59:23 -04:00
|
|
|
project.update(path: path) &&
|
2018-10-17 07:33:54 -04:00
|
|
|
defined?(Projects::AfterRenameService)
|
2016-12-23 10:18:36 -05:00
|
|
|
end
|
2016-12-21 12:31:30 -05:00
|
|
|
end
|