Class: AssociateBranchesToRuns

Inherits:
Ductwork::Migration show all
Defined in:
lib/generators/ductwork/update/templates/db/associate_branches_to_runs.rb

Instance Method Summary collapse

Methods included from Ductwork::MigrationHelper

#belongs_to, #create_ductwork_table, #mysql?, #postgresql?, #uuid_column_type

Instance Method Details

#downObject



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
# File 'lib/generators/ductwork/update/templates/db/associate_branches_to_runs.rb', line 45

def down
  options = if postgresql?
              {
                type: uuid_column_type,
                index: true,
                null: true,
                foreign_key: { to_table: :ductwork_pipelines },
              }
            else
              {
                type: uuid_column_type,
                limit: 36,
                index: true,
                null: true,
                foreign_key: { to_table: :ductwork_pipelines },
              }
            end

  add_reference :ductwork_branches, :pipeline, **options

  Ductwork::Run.find_each do |run|
    Ductwork::Branch.where(run_id: run.id).update_all(pipeline_id: run.pipeline_id)
  end

  remove_index :ductwork_branches, %w[run_id started_at]
  remove_reference :ductwork_branches, :run, index: true, foreign_key: { to_table: :ductwork_runs }

  add_index :ductwork_branches, %w[pipeline_id started_at]
end

#upObject



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
# File 'lib/generators/ductwork/update/templates/db/associate_branches_to_runs.rb', line 4

def up
  options = if postgresql?
              {
                type: uuid_column_type,
                index: true,
                null: true,
                foreign_key: { to_table: :ductwork_runs },
              }
            else
              {
                type: uuid_column_type,
                limit: 36,
                index: true,
                null: true,
                foreign_key: { to_table: :ductwork_runs },
              }
            end

  add_reference :ductwork_branches, :run, **options

  Ductwork::Pipeline.find_each do |pipeline|
    run = Ductwork::Run.create!(
      pipeline: pipeline,
      pipeline_klass: pipeline.klass,
      definition: pipeline.definition,
      definition_sha1: pipeline.definition_sha1,
      status: pipeline.status,
      triggered_at: pipeline.triggered_at,
      started_at: pipeline.started_at,
      completed_at: pipeline.completed_at,
      halted_at: pipeline.halted_at
    )
    pipeline.branches.update_all(run_id: run.id)
  end

  remove_index :ductwork_branches, %w[pipeline_id started_at]
  remove_reference :ductwork_branches, :pipeline, index: true, foreign_key: { to_table: :ductwork_pipelines }

  add_index :ductwork_branches, %w[run_id started_at]
end