Class: PgDice::PartitionHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/pgdice/partition_helper.rb

Overview

Helps do high-level tasks like getting tables partitioned

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger:, approved_tables:, validation:, pg_slice_manager:) ⇒ PartitionHelper

Returns a new instance of PartitionHelper.



9
10
11
12
13
14
# File 'lib/pgdice/partition_helper.rb', line 9

def initialize(logger:, approved_tables:, validation:, pg_slice_manager:)
  @logger = logger
  @validation = validation
  @approved_tables = approved_tables
  @pg_slice_manager = pg_slice_manager
end

Instance Attribute Details

#approved_tablesObject (readonly)

Returns the value of attribute approved_tables.



7
8
9
# File 'lib/pgdice/partition_helper.rb', line 7

def approved_tables
  @approved_tables
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/pgdice/partition_helper.rb', line 7

def logger
  @logger
end

#pg_slice_managerObject (readonly)

Returns the value of attribute pg_slice_manager.



7
8
9
# File 'lib/pgdice/partition_helper.rb', line 7

def pg_slice_manager
  @pg_slice_manager
end

#validationObject (readonly)

Returns the value of attribute validation.



7
8
9
# File 'lib/pgdice/partition_helper.rb', line 7

def validation
  @validation
end

Instance Method Details

#partition_table(table_name, params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/pgdice/partition_helper.rb', line 16

def partition_table(table_name, params = {})
  table = approved_tables.fetch(table_name)
  all_params = table.smash(params)
  validation.validate_parameters(all_params)

  logger.info { "Preparing database for table: #{table}. Using parameters: #{all_params}" }

  prep_and_fill(all_params)
  swap_and_fill(all_params)
end

#undo_partitioning(table_name) ⇒ Object



41
42
43
44
45
46
# File 'lib/pgdice/partition_helper.rb', line 41

def undo_partitioning(table_name)
  undo_partitioning!(table_name)
rescue PgDice::PgSliceError => e
  logger.error { "Rescued PgSliceError: #{e}" }
  false
end

#undo_partitioning!(table_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pgdice/partition_helper.rb', line 27

def undo_partitioning!(table_name)
  approved_tables.fetch(table_name)
  logger.info { "Undoing partitioning for table: #{table_name}" }

  pg_slice_manager.analyze(table_name: table_name, swapped: true)
  unswap_results = unswap(table_name)
  unprep_results = unprep(table_name)
  if !unswap_results && !unprep_results
    raise PgDice::PgSliceError, "Unswapping and unprepping failed for table: #{table_name}"
  end

  true
end