Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20170523085324_migrate_old_traces.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 473a1bebf468f2f83f0976104fa1d166cb484a38 (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
73
74
75
76
77
78
79
require 'work_queue'

class MigrateOldTraces < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    builds_with_traces(min_id || 0).each do |build|
      work_queue.enqueue_b do
        migrate_trace(build)
      end
    end

  ensure
    work_queue.join
  end

  def down
  end

  private

  def migrate_trace(build)
    source = source_trace(build)
    target = target_trace(build)
    return unless File.exists?(source)

    ensure_path(target)
    FileUtils.move(source, target)
  end

  def ensure_path(path)
    directory = File.dirname(path)
    unless Dir.exist?(default_directory)
      FileUtils.mkdir_p(directory)
    end
  end

  def source_trace(build)
    File.join(Gitlab.config.gitlab_ci.builds_path,
      build['created_at'].utc.strftime('%Y_%m'),
      build['ci_id'].to_s, 
      build['id'].to_s + ".log")
  end

  def target_trace(build)
    File.join(Gitlab.config.gitlab_ci.builds_path,
      build['created_at'].utc.strftime('%Y_%m'),
      build['project_id'].to_s, 
      build['id'].to_s + ".log")
  end

  def min_id
    select_value <<-SQL.strip_heredoc
      SELECT min(ci_builds.id) as min_id FROM ci_builds
        JOIN projects ON ci_builds.project_id = projects.id 
         AND projects.ci_id IS NULL
    SQL
  end

  def builds_with_traces(before_id)
    select_all <<-SQL.strip_heredoc
      SELECT ci_builds.id, ci_builds.project_id,
             projects.ci_id, ci_builds.created_at
        FROM ci_builds
        JOIN projects ON ci_builds.project_id = projects.id 
       WHERE ci_builds.id < #{before_id}
         AND ci_builds.erased_at IS NULL
         AND projects.ci_id IS NOT NULL
    SQL
  end

  def work_queue
    @work_queue ||= WorkQueue.new(10)
  end
end