migrate.rake 2.2 KB
Newer Older
1
namespace :ci do
2 3
  desc 'GitLab | Import and migrate CI database'
  task migrate: :environment do
4 5 6
    warn_user_is_not_gitlab
    configure_cron_mode

7
    unless ENV['force'] == 'yes'
8
      puts 'This will remove all CI related data and restore it from the provided backup.'
9
      ask_to_continue
10
      puts ''
11 12
    end

13 14 15 16 17 18 19 20 21
    migrate = Ci::Migrate::Manager.new
    migrate.unpack

    Rake::Task['ci:migrate:db'].invoke
    Rake::Task['ci:migrate:builds'].invoke
    Rake::Task['ci:migrate:tags'].invoke
    Rake::Task['ci:migrate:services'].invoke

    migrate.cleanup
22 23 24 25 26
  end

  namespace :migrate do
    desc 'GitLab | Import CI database'
    task db: :environment do
27 28 29 30 31
      configure_cron_mode
      $progress.puts 'Restoring database ... '.blue
      Ci::Migrate::Database.new.restore
      $progress.puts 'done'.green
    end
32

33 34 35 36 37 38
    desc 'GitLab | Import CI builds'
    task builds: :environment do
      configure_cron_mode
      $progress.puts 'Restoring builds ... '.blue
      Ci::Migrate::Builds.new.restore
      $progress.puts 'done'.green
39 40
    end

41
    desc 'GitLab | Migrate CI tags'
42
    task tags: :environment do
43 44
      configure_cron_mode
      $progress.puts 'Migrating tags ... '.blue
45
      ::Ci::Migrate::Tags.new.restore
46
      $progress.puts 'done'.green
47
    end
48

49 50 51 52 53 54 55 56 57 58 59 60 61
    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
62 63 64
        end
      end
    end
K
Kamil Trzcinski 已提交
65 66 67

    desc 'GitLab | Migrate CI services'
    task services: :environment do
68
      $progress.puts 'Migrating services ... '.blue
K
Kamil Trzcinski 已提交
69
      c = ActiveRecord::Base.connection
K
Kamil Trzcinski 已提交
70
      c.execute("UPDATE ci_services SET type=CONCAT('Ci::', type) WHERE type NOT LIKE 'Ci::%'")
71
      $progress.puts 'done'.green
K
Kamil Trzcinski 已提交
72
    end
73 74
  end
end