Class: AssociateStepsToRuns

Inherits:
Ductwork::Migration show all
Defined in:
lib/generators/ductwork/update/templates/db/associate_steps_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



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

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_steps, :pipeline, **options

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

  remove_index :ductwork_steps, %i[run_id status node]
  remove_index :ductwork_steps, %i[run_id node status]
  remove_index :ductwork_steps, %i[run_id status]
  remove_reference :ductwork_steps, :run, index: true, foreign_key: { to_table: :ductwork_runs }

  add_index :ductwork_steps, %i[pipeline_id status node]
  add_index :ductwork_steps, %i[pipeline_id node status]
  add_index :ductwork_steps, %i[pipeline_id status]
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
# File 'lib/generators/ductwork/update/templates/db/associate_steps_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_steps, :run, **options

  Ductwork::Pipeline.find_each do |pipeline|
    run = Ductwork::Run.find_by(pipeline_id: pipeline.id)
    pipeline.steps.update_all(run_id: run.id) if run
  end

  remove_index :ductwork_steps, %i[pipeline_id status node]
  remove_index :ductwork_steps, %i[pipeline_id node status]
  remove_index :ductwork_steps, %i[pipeline_id status]
  remove_reference :ductwork_steps, :pipeline, index: true, foreign_key: { to_table: :ductwork_pipelines }

  add_index :ductwork_steps, %i[run_id status node]
  add_index :ductwork_steps, %i[run_id node status]
  add_index :ductwork_steps, %i[run_id status]
end