diff options
Diffstat (limited to 'lib/tasks/ci/migrate.rake')
-rw-r--r-- | lib/tasks/ci/migrate.rake | 70 |
1 files changed, 43 insertions, 27 deletions
diff --git a/lib/tasks/ci/migrate.rake b/lib/tasks/ci/migrate.rake index 7d99664dcf3..2760c503e22 100644 --- a/lib/tasks/ci/migrate.rake +++ b/lib/tasks/ci/migrate.rake @@ -1,38 +1,54 @@ namespace :ci do - namespace :migrate do - def list_objects(type) - ids = ActiveRecord::Base.connection.select_all( - 'select distinct taggable_id from ci_taggings where taggable_type = $1', - nil, [[nil, type]] - ) - ids.map { |id| id['taggable_id'] } + desc 'GitLab | Import and migrate CI database' + task migrate: :environment do + unless ENV['force'] == 'yes' + puts "This will truncate all CI tables and restore it from provided backup." + puts "You will lose any previous CI data stored in the database." + ask_to_continue + puts "" end - def list_tags(type, id) - tags = ActiveRecord::Base.connection.select_all( - 'select ci_tags.name from ci_tags ' + - 'join ci_taggings on ci_tags.id = ci_taggings.tag_id ' + - 'where taggable_type = $1 and taggable_id = $2 and context = $3', - nil, [[nil, type], [nil, id], [nil, 'tags']] - ) - tags.map { |tag| tag['name'] } + Rake::Task["ci:migrate:db"].invoke + Rake::Task["ci:migrate:autoincrements"].invoke + Rake::Task["ci:migrate:tags"].invoke + end + + namespace :migrate do + desc 'GitLab | Import CI database' + task db: :environment do + if ENV["CI_DUMP"].nil? + puts "No CI SQL dump specified:" + puts "rake gitlab:backup:restore CI_DUMP=ci_dump.sql" + exit 1 + end + + ci_dump = ENV["CI_DUMP"] + unless File.exists?(ci_dump) + puts "The specified sql dump doesn't exist!" + exit 1 + end + + ::Ci::Migrate::Database.new.restore(ci_dump) end desc 'GitLab | Migrate CI tags' task tags: :environment do - list_objects('Runner').each do |id| - runner = Ci::Runner.find_by_id(id) - if runner - tags = list_tags('Runner', id) - runner.update_attributes(tag_list: tags) - end - end + ::Ci::Migrate::Tags.new.restore + end - list_objects('Build').each do |id| - build = Ci::Build.find_by_id(id) - if build - tags = list_tags('Build', id) - build.update_attributes(tag_list: tags) + desc 'GitLab | Migrate CI auto-increments' + task autoincrements: :environment do + c = ActiveRecord::Base.connection + c.tables.select { |t| t.start_with?('ci_') }.each do |table| + result = c.select_one("SELECT id FROM #{table} ORDER BY id DESC LIMIT 1") + if result + ai_val = result['id'].to_i + 1 + puts "Resetting auto increment ID for #{table} to #{ai_val}" + if c.adapter_name == 'PostgreSQL' + c.execute("ALTER SEQUENCE #{table}_id_seq RESTART WITH #{ai_val}") + else + c.execute("ALTER TABLE #{table} AUTO_INCREMENT = #{ai_val}") + end end end end |