summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170523083112_migrate_old_artifacts.rb
blob: f2690bd00170edda00f7f658dd696bbd032b4194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class MigrateOldArtifacts < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  # This uses special heuristic to find potential candidates for data migration
  # Read more about this here: https://gitlab.com/gitlab-org/gitlab-ce/issues/32036#note_30422345
  
  def up
    builds_with_artifacts.find_each do |build|
      build.migrate_artifacts!
    end
  end

  def down
  end

  private

  def builds_with_artifacts
    Build.with_artifacts
      .joins('JOIN projects ON projects.id = ci_builds.project_id')
      .where('ci_builds.id < ?', min_id)
      .where('projects.ci_id IS NOT NULL')
      .select('id', 'created_at', 'project_id', 'projects.ci_id AS ci_id')
  end

  def min_id
    Build.joins('JOIN projects ON projects.id = ci_builds.project_id')
      .where('projects.ci_id IS NULL')
      .pluck('coalesce(min(ci_builds.id), 0)')
      .first
  end

  class Build < ActiveRecord::Base
    self.table_name = 'ci_builds'

    scope :with_artifacts, -> { where.not(artifacts_file: [nil, '']) }

    def migrate_artifacts!
      return unless File.exist?(source_artifacts_path)
      return if File.exist?(target_artifacts_path)

      ensure_target_path

      FileUtils.move(source_artifacts_path, target_artifacts_path)
    end

    private

    def source_artifacts_path
      @source_artifacts_path ||= 
        File.join(Gitlab.config.artifacts.path,
          created_at.utc.strftime('%Y_%m'),
          ci_id.to_s, id.to_s)
    end

    def target_artifacts_path
      @target_artifacts_path ||= 
        File.join(Gitlab.config.artifacts.path,
          created_at.utc.strftime('%Y_%m'),
          project_id.to_s, id.to_s)
    end

    def ensure_target_path
      directory = File.dirname(target_artifacts_path)
      FileUtils.mkdir_p(directory) unless Dir.exist?(directory)
    end
  end
end