2020-05-24 23:13:21 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
namespace :gitlab do
|
|
|
|
namespace :snippets do
|
|
|
|
DEFAULT_LIMIT = 100
|
|
|
|
|
|
|
|
# @example
|
|
|
|
# bundle exec rake gitlab:snippets:migrate SNIPPET_IDS=1,2,3,4
|
|
|
|
# bundle exec rake gitlab:snippets:migrate SNIPPET_IDS=1,2,3,4 LIMIT=50
|
|
|
|
desc 'GitLab | Migrate specific snippets to git'
|
|
|
|
task :migrate, [:ids] => :environment do |_, args|
|
|
|
|
unless ENV['SNIPPET_IDS'].presence
|
|
|
|
raise "Please supply the list of ids through the SNIPPET_IDS env var"
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
raise "Invalid limit value" if snippet_task_limit == 0
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
if migration_running?
|
|
|
|
raise "There are already snippet migrations running. Please wait until they are finished."
|
|
|
|
end
|
|
|
|
|
|
|
|
ids = parse_snippet_ids!
|
|
|
|
|
|
|
|
puts "Starting the migration..."
|
|
|
|
Gitlab::BackgroundMigration::BackfillSnippetRepositories.new.perform_by_ids(ids)
|
|
|
|
|
|
|
|
list_non_migrated = non_migrated_snippets.where(id: ids)
|
|
|
|
|
|
|
|
if list_non_migrated.exists?
|
|
|
|
puts "The following snippets couldn't be migrated:"
|
|
|
|
puts list_non_migrated.pluck(:id).join(',')
|
|
|
|
else
|
|
|
|
puts "All snippets were migrated successfully"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_snippet_ids!
|
|
|
|
ids = ENV['SNIPPET_IDS'].delete(' ').split(',').map do |id|
|
|
|
|
id.to_i.tap do |value|
|
2020-10-24 23:57:45 +05:30
|
|
|
raise "Invalid id provided" if value == 0
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
if ids.size > snippet_task_limit
|
|
|
|
raise "The number of ids provided is higher than #{snippet_task_limit}. You can update this limit by using the env var `LIMIT`"
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
ids
|
|
|
|
end
|
|
|
|
|
|
|
|
# @example
|
|
|
|
# bundle exec rake gitlab:snippets:migration_status
|
|
|
|
desc 'GitLab | Show whether there are snippet background migrations running'
|
|
|
|
task migration_status: :environment do
|
|
|
|
if migration_running?
|
|
|
|
puts "There are snippet migrations running"
|
|
|
|
else
|
|
|
|
puts "There are no snippet migrations running"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def migration_running?
|
2022-11-25 23:54:43 +05:30
|
|
|
Sidekiq::ScheduledSet.new.any? { |r| r.klass == 'BackgroundMigrationWorker' && r.args[0] == 'BackfillSnippetRepositories' } # rubocop:disable Cop/SidekiqApiUsage
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# @example
|
|
|
|
# bundle exec rake gitlab:snippets:list_non_migrated
|
|
|
|
# bundle exec rake gitlab:snippets:list_non_migrated LIMIT=50
|
|
|
|
desc 'GitLab | Show non migrated snippets'
|
|
|
|
task list_non_migrated: :environment do
|
2021-03-11 19:13:27 +05:30
|
|
|
raise "Invalid limit value" if snippet_task_limit == 0
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
non_migrated_count = non_migrated_snippets.count
|
2020-10-24 23:57:45 +05:30
|
|
|
if non_migrated_count == 0
|
2020-05-24 23:13:21 +05:30
|
|
|
puts "All snippets have been successfully migrated"
|
|
|
|
else
|
|
|
|
puts "There are #{non_migrated_count} snippets that haven't been migrated. Showing a batch of ids of those snippets:\n"
|
2021-03-11 19:13:27 +05:30
|
|
|
puts non_migrated_snippets.limit(snippet_task_limit).pluck(:id).join(',')
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def non_migrated_snippets
|
|
|
|
@non_migrated_snippets ||= Snippet.select(:id).where.not(id: SnippetRepository.select(:snippet_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
# There are problems with the specs if we memoize this value
|
2021-03-11 19:13:27 +05:30
|
|
|
def snippet_task_limit
|
2020-05-24 23:13:21 +05:30
|
|
|
ENV['LIMIT'] ? ENV['LIMIT'].to_i : DEFAULT_LIMIT
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|