Class: PartitionGardener::RunRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/partition_gardener/run_record.rb

Constant Summary collapse

PHASES =
%w[detach segments rows cleanup complete].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name:, phase:, plan_signature:, staging_row_count: 0) ⇒ RunRecord

Returns a new instance of RunRecord.



40
41
42
43
44
45
# File 'lib/partition_gardener/run_record.rb', line 40

def initialize(table_name:, phase:, plan_signature:, staging_row_count: 0)
  @table_name = table_name
  @phase = phase
  @plan_signature = plan_signature
  @staging_row_count = staging_row_count
end

Instance Attribute Details

#phaseObject (readonly)

Returns the value of attribute phase.



5
6
7
# File 'lib/partition_gardener/run_record.rb', line 5

def phase
  @phase
end

#plan_signatureObject (readonly)

Returns the value of attribute plan_signature.



5
6
7
# File 'lib/partition_gardener/run_record.rb', line 5

def plan_signature
  @plan_signature
end

#staging_row_countObject (readonly)

Returns the value of attribute staging_row_count.



5
6
7
# File 'lib/partition_gardener/run_record.rb', line 5

def staging_row_count
  @staging_row_count
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



5
6
7
# File 'lib/partition_gardener/run_record.rb', line 5

def table_name
  @table_name
end

Class Method Details

.clear(table_name) ⇒ Object



27
28
29
# File 'lib/partition_gardener/run_record.rb', line 27

def self.clear(table_name)
  PartitionGardener.configuration.run_record_store.clear(table_name)
end

.fetch_attribute(attributes, key) ⇒ Object



23
24
25
# File 'lib/partition_gardener/run_record.rb', line 23

def self.fetch_attribute(attributes, key)
  attributes[key] || attributes[key.to_s]
end

.from_h(attributes) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/partition_gardener/run_record.rb', line 14

def self.from_h(attributes)
  new(
    table_name: fetch_attribute(attributes, :table_name),
    phase: fetch_attribute(attributes, :phase),
    plan_signature: fetch_attribute(attributes, :plan_signature),
    staging_row_count: fetch_attribute(attributes, :staging_row_count) || 0
  )
end

.load(table_name) ⇒ Object



7
8
9
10
11
12
# File 'lib/partition_gardener/run_record.rb', line 7

def self.load(table_name)
  attributes = PartitionGardener.configuration.run_record_store.load(table_name)
  return unless attributes

  from_h(attributes)
end

.start(table_name:, plan_signature:) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/partition_gardener/run_record.rb', line 31

def self.start(table_name:, plan_signature:)
  new(
    table_name: table_name,
    phase: "detach",
    plan_signature: plan_signature,
    staging_row_count: 0
  ).tap(&:save!)
end

Instance Method Details

#advance!(phase, staging_row_count: nil) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/partition_gardener/run_record.rb', line 63

def advance!(phase, staging_row_count: nil)
  RunRecord.new(
    table_name: table_name,
    phase: phase,
    plan_signature: plan_signature,
    staging_row_count: staging_row_count.nil? ? self.staging_row_count : staging_row_count
  ).tap(&:save!)
end

#incomplete?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/partition_gardener/run_record.rb', line 47

def incomplete?
  Blank.present?(phase) && phase != "complete"
end

#phase_at_least?(name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/partition_gardener/run_record.rb', line 72

def phase_at_least?(name)
  PHASES.index(phase) >= PHASES.index(name)
end

#save!Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/partition_gardener/run_record.rb', line 51

def save!
  PartitionGardener.configuration.run_record_store.save(
    table_name,
    {
      table_name: table_name,
      phase: phase,
      plan_signature: plan_signature,
      staging_row_count: staging_row_count
    }
  )
end