class PgDice::PartitionHelper

Helps do high-level tasks like getting tables partitioned

Attributes

approved_tables[R]
logger[R]
pg_slice_manager[R]
validation[R]

Public Class Methods

new(logger:, approved_tables:, validation:, pg_slice_manager:) click to toggle source
# 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

Public Instance Methods

partition_table(table_name, params = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

prep_and_fill(params) click to toggle source
# File lib/pgdice/partition_helper.rb, line 62
def prep_and_fill(params)
  pg_slice_manager.prep(params)
  pg_slice_manager.add_partitions(params.merge!(intermediate: true))
  pg_slice_manager.fill(params) if params[:fill]
end
swap_and_fill(params) click to toggle source
# File lib/pgdice/partition_helper.rb, line 68
def swap_and_fill(params)
  pg_slice_manager.analyze(params)
  pg_slice_manager.swap(params)
  pg_slice_manager.fill(params.merge!(swapped: true)) if params[:fill]
end
unprep(table_name) click to toggle source
# File lib/pgdice/partition_helper.rb, line 56
def unprep(table_name)
  unprep_results = pg_slice_manager.unprep(table_name: table_name)
  logger.warn { "Unprepping #{table_name} was not successful." } unless unprep_results
  unprep_results
end
unswap(table_name) click to toggle source
# File lib/pgdice/partition_helper.rb, line 50
def unswap(table_name)
  unswap_results = pg_slice_manager.unswap(table_name: table_name)
  logger.warn { "Unswapping #{table_name} was not successful. " } unless unswap_results
  unswap_results
end