Module: DurableFlow::Schema

Defined in:
lib/durable_flow/schema.rb

Class Method Summary collapse

Class Method Details

.create_workflow_events(connection) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/durable_flow/schema.rb', line 85

def create_workflow_events(connection)
  return if connection.data_source_exists?(:durable_flow_workflow_events)

  connection.create_table :durable_flow_workflow_events do |t|
    t.string :name, null: false
    t.json :payload
    t.json :tags
    t.json :context
    t.json :source_location
    t.datetime :occurred_at, null: false
    t.timestamps
  end

  connection.add_index :durable_flow_workflow_events, [ :name, :occurred_at ], name: "idx_durable_flow_events_on_name_time"
end

.create_workflow_logs(connection) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/durable_flow/schema.rb', line 124

def create_workflow_logs(connection)
  return if connection.data_source_exists?(:durable_flow_workflow_logs)

  connection.create_table :durable_flow_workflow_logs do |t|
    t.references :workflow_run, null: false, index: false
    t.references :workflow_step, index: false
    t.string :level, null: false
    t.string :message, null: false
    t.json :data
    t.timestamps
  end

  connection.add_index :durable_flow_workflow_logs,
    [ :workflow_run_id, :created_at ],
    name: "idx_durable_flow_logs_on_run_time"
  connection.add_index :durable_flow_workflow_logs,
    [ :workflow_step_id, :created_at ],
    name: "idx_durable_flow_logs_on_step_time"
end

.create_workflow_runs(connection) ⇒ Object



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
# File 'lib/durable_flow/schema.rb', line 15

def create_workflow_runs(connection)
  if connection.data_source_exists?(:durable_flow_workflow_runs)
    ensure_workflow_run_lock_columns(connection)
    return
  end

  connection.create_table :durable_flow_workflow_runs do |t|
    t.string :run_id, null: false
    t.string :job_id, null: false
    t.string :workflow_class, null: false
    t.string :status, null: false, default: "enqueued"
    t.string :queue_name
    t.integer :priority
    t.json :arguments
    t.json :serialized_job
    t.json :last_error
    t.datetime :started_at
    t.datetime :interrupted_at
    t.datetime :completed_at
    t.datetime :failed_at
    t.string :execution_locked_by
    t.datetime :execution_locked_at
    t.datetime :execution_lock_expires_at
    t.timestamps
  end

  connection.add_index :durable_flow_workflow_runs, :run_id, unique: true
  connection.add_index :durable_flow_workflow_runs, [ :workflow_class, :status ], name: "idx_durable_flow_runs_on_class_status"
  connection.add_index :durable_flow_workflow_runs, :execution_lock_expires_at, name: "idx_durable_flow_runs_on_lock_expiry"
end

.create_workflow_steps(connection) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/durable_flow/schema.rb', line 64

def create_workflow_steps(connection)
  return if connection.data_source_exists?(:durable_flow_workflow_steps)

  connection.create_table :durable_flow_workflow_steps do |t|
    t.references :workflow_run, null: false, index: false
    t.string :name, null: false
    t.string :status, null: false, default: "pending"
    t.integer :attempts, null: false, default: 0
    t.json :result
    t.json :metadata
    t.datetime :started_at
    t.datetime :completed_at
    t.timestamps
  end

  connection.add_index :durable_flow_workflow_steps,
    [ :workflow_run_id, :name ],
    unique: true,
    name: "idx_durable_flow_steps_on_run_and_name"
end

.create_workflow_waits(connection) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/durable_flow/schema.rb', line 101

def create_workflow_waits(connection)
  return if connection.data_source_exists?(:durable_flow_workflow_waits)

  connection.create_table :durable_flow_workflow_waits do |t|
    t.references :workflow_run, null: false, index: false
    t.references :workflow_step, null: false, index: false
    t.references :workflow_event, index: false
    t.string :event_name, null: false
    t.string :status, null: false, default: "pending"
    t.json :match
    t.datetime :timeout_at
    t.timestamps
  end

  connection.add_index :durable_flow_workflow_waits,
    [ :workflow_run_id, :workflow_step_id ],
    unique: true,
    name: "idx_durable_flow_waits_on_run_and_step"
  connection.add_index :durable_flow_workflow_waits,
    [ :event_name, :status ],
    name: "idx_durable_flow_waits_on_event_status"
end

.define(connection = ActiveRecord::Base.connection) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/durable_flow/schema.rb', line 7

def define(connection = ActiveRecord::Base.connection)
  create_workflow_runs(connection)
  create_workflow_steps(connection)
  create_workflow_events(connection)
  create_workflow_waits(connection)
  create_workflow_logs(connection)
end

.ensure_workflow_run_lock_columns(connection) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/durable_flow/schema.rb', line 46

def ensure_workflow_run_lock_columns(connection)
  unless connection.column_exists?(:durable_flow_workflow_runs, :execution_locked_by)
    connection.add_column :durable_flow_workflow_runs, :execution_locked_by, :string
  end

  unless connection.column_exists?(:durable_flow_workflow_runs, :execution_locked_at)
    connection.add_column :durable_flow_workflow_runs, :execution_locked_at, :datetime
  end

  unless connection.column_exists?(:durable_flow_workflow_runs, :execution_lock_expires_at)
    connection.add_column :durable_flow_workflow_runs, :execution_lock_expires_at, :datetime
  end

  unless connection.index_exists?(:durable_flow_workflow_runs, :execution_lock_expires_at, name: "idx_durable_flow_runs_on_lock_expiry")
    connection.add_index :durable_flow_workflow_runs, :execution_lock_expires_at, name: "idx_durable_flow_runs_on_lock_expiry"
  end
end