2016-12-26 09:14:41 -05:00
|
|
|
class RemoveDotGitFromUsernames < ActiveRecord::Migration
|
|
|
|
include Gitlab::Database::MigrationHelpers
|
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
|
|
|
DOWNTIME = false
|
|
|
|
|
|
|
|
def up
|
|
|
|
invalid_users.each do |user|
|
|
|
|
id = user['id']
|
|
|
|
namespace_id = user['namespace_id']
|
|
|
|
path_was = user['username']
|
|
|
|
path_was_wildcard = quote_string("#{path_was}/%")
|
2017-01-16 06:59:36 -05:00
|
|
|
path = quote_string(new_path(path_was))
|
2016-12-26 09:14:41 -05:00
|
|
|
|
2017-01-16 06:59:36 -05:00
|
|
|
move_namespace(namespace_id, path_was, path)
|
2017-01-16 03:11:36 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
execute "UPDATE routes SET path = '#{path}' WHERE source_type = 'Namespace' AND source_id = #{namespace_id}"
|
|
|
|
execute "UPDATE namespaces SET path = '#{path}' WHERE id = #{namespace_id}"
|
|
|
|
execute "UPDATE users SET username = '#{path}' WHERE id = #{id}"
|
|
|
|
|
|
|
|
select_all("SELECT id, path FROM routes WHERE path LIKE '#{path_was_wildcard}'").each do |route|
|
|
|
|
new_path = "#{path}/#{route['path'].split('/').last}"
|
|
|
|
execute "UPDATE routes SET path = '#{new_path}' WHERE id = #{route['id']}"
|
|
|
|
end
|
|
|
|
rescue => e
|
2017-01-16 03:49:58 -05:00
|
|
|
say("Couldn't update routes for path #{path_was} to #{path}")
|
2017-01-16 06:59:36 -05:00
|
|
|
# Move namespace back
|
|
|
|
move_namespace(namespace_id, path, path_was)
|
2017-01-16 03:11:36 -05:00
|
|
|
|
|
|
|
raise e
|
2016-12-26 09:14:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
# nothing to do here
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def invalid_users
|
|
|
|
select_all("SELECT u.id, u.username, n.path AS namespace_path, n.id AS namespace_id FROM users u
|
|
|
|
INNER JOIN namespaces n ON n.owner_id = u.id
|
|
|
|
WHERE n.type is NULL AND n.path LIKE '%.git'")
|
|
|
|
end
|
|
|
|
|
|
|
|
def route_exists?(path)
|
|
|
|
select_all("SELECT id, path FROM routes WHERE path = '#{quote_string(path)}'").present?
|
|
|
|
end
|
|
|
|
|
2018-04-13 06:57:19 -04:00
|
|
|
def path_exists?(shard, repository_storage_path)
|
|
|
|
repository_storage_path && gitlab_shell.exists?(shard, repository_storage_path)
|
2017-01-13 03:15:52 -05:00
|
|
|
end
|
|
|
|
|
2016-12-26 09:14:41 -05:00
|
|
|
# Accepts invalid path like test.git and returns test_git or
|
|
|
|
# test_git1 if test_git already taken
|
2017-01-16 06:59:36 -05:00
|
|
|
def new_path(path)
|
2016-12-26 09:14:41 -05:00
|
|
|
# To stay closer with original name and reduce risk of duplicates
|
|
|
|
# we rename suffix instead of removing it
|
|
|
|
path = path.sub(/\.git\z/, '_git')
|
|
|
|
|
2018-07-19 04:55:55 -04:00
|
|
|
check_routes(path.dup, 0, path)
|
2017-01-16 06:59:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_routes(base, counter, path)
|
2017-01-16 07:37:04 -05:00
|
|
|
route_exists = route_exists?(path)
|
|
|
|
|
2018-07-19 04:55:55 -04:00
|
|
|
Gitlab.config.repositories.storages.each do |shard, _storage|
|
|
|
|
if route_exists || path_exists?(shard, path)
|
2017-01-16 06:59:36 -05:00
|
|
|
counter += 1
|
|
|
|
path = "#{base}#{counter}"
|
2016-12-26 09:14:41 -05:00
|
|
|
|
2017-01-16 10:11:50 -05:00
|
|
|
return check_routes(base, counter, path)
|
2017-01-16 06:59:36 -05:00
|
|
|
end
|
2016-12-26 09:14:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
2017-01-16 06:59:36 -05:00
|
|
|
def move_namespace(namespace_id, path_was, path)
|
2018-04-13 06:57:19 -04:00
|
|
|
repository_storages = select_all("SELECT distinct(repository_storage) FROM projects WHERE namespace_id = #{namespace_id}").map do |row|
|
|
|
|
row['repository_storage']
|
2016-12-26 09:14:41 -05:00
|
|
|
end.compact
|
|
|
|
|
2018-04-13 06:57:19 -04:00
|
|
|
# Move the namespace directory in all storages used by member projects
|
|
|
|
repository_storages.each do |repository_storage|
|
2016-12-26 09:14:41 -05:00
|
|
|
# Ensure old directory exists before moving it
|
2018-04-13 06:57:19 -04:00
|
|
|
gitlab_shell.add_namespace(repository_storage, path_was)
|
2016-12-26 09:14:41 -05:00
|
|
|
|
2018-04-13 06:57:19 -04:00
|
|
|
unless gitlab_shell.mv_namespace(repository_storage, path_was, path)
|
|
|
|
Rails.logger.error "Exception moving on shard #{repository_storage} from #{path_was} to #{path}"
|
2016-12-26 09:14:41 -05:00
|
|
|
|
|
|
|
# if we cannot move namespace directory we should rollback
|
|
|
|
# db changes in order to prevent out of sync between db and fs
|
|
|
|
raise Exception.new('namespace directory cannot be moved')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-16 03:11:36 -05:00
|
|
|
begin
|
|
|
|
Gitlab::UploadsTransfer.new.rename_namespace(path_was, path)
|
|
|
|
rescue => e
|
|
|
|
if path.nil?
|
|
|
|
say("Couldn't find a storage path for #{namespace_id}, #{path_was} -- skipping")
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
2016-12-26 09:14:41 -05:00
|
|
|
end
|
|
|
|
end
|