Class: CreateDuctworkRuns

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

Instance Method Summary collapse

Methods included from Ductwork::MigrationHelper

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

Instance Method Details

#changeObject



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

def change
  create_ductwork_table :ductwork_runs do |table|
    belongs_to(
      table,
      :pipeline,
      index: true,
      null: false,
      foreign_key: { to_table: :ductwork_pipelines }
    )
    table.string :pipeline_klass, null: false
    table.text :definition, null: false
    table.string :definition_sha1, null: false
    table.string :status, null: false
    table.datetime :triggered_at, null: false
    table.datetime :started_at, null: false
    table.datetime :completed_at
    table.datetime :halted_at
    table.datetime :on_halt_dispatched_at
    table.timestamps null: false
  end

  add_index :ductwork_runs, %i[pipeline_id status]

  if mysql?
    reversible do |direction|
      direction.up do
        execute <<~SQL
          ALTER TABLE ductwork_runs
            ADD COLUMN active_pipeline_id VARCHAR(36)
              GENERATED ALWAYS AS (
                IF(status IN ('in_progress', 'paused'), pipeline_id, NULL)
              )
        SQL
        add_index :ductwork_runs, :active_pipeline_id,
                  unique: true,
                  name: :idx_unique_active_run
      end

      direction.down do
        remove_index :ductwork_runs, name: :idx_unique_active_run
        remove_column :ductwork_runs, :active_pipeline_id
      end
    end
  else
    add_index :ductwork_runs,
              :pipeline_id,
              unique: true,
              where: "status IN ('in_progress', 'paused')",
              name: :idx_unique_active_run
  end
end