class PgDice::PgSliceManager

PgSliceManager is a wrapper around PgSlice

Attributes

database_url[R]
logger[R]

Public Class Methods

new(logger:, database_url:, pg_slice_executor:, dry_run: false) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 11
def initialize(logger:, database_url:, pg_slice_executor:, dry_run: false)
  @logger = logger
  @database_url = database_url
  @dry_run = dry_run
  @pg_slice_executor = pg_slice_executor
end

Public Instance Methods

add_partitions(params = {}) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 46
def add_partitions(params = {})
  table_name = params.fetch(:table_name)
  future_tables = params.fetch(:future, nil)
  future_tables = "--future #{Integer(future_tables)}" if future_tables

  past_tables = params.fetch(:past, nil)
  past_tables = "--past #{Integer(past_tables)}" if past_tables

  intermediate = params.fetch(:intermediate, nil)
  intermediate = '--intermediate' if intermediate.to_s.casecmp('true').zero?

  run_pgslice("add_partitions #{table_name} #{intermediate} #{future_tables} #{past_tables}", params[:dry_run])
end
analyze(params = {}) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 33
def analyze(params = {})
  table_name = params.fetch(:table_name)
  swapped = params.fetch(:swapped, '')
  swapped = '--swapped' if swapped.to_s.casecmp('true').zero?

  run_pgslice("analyze #{table_name} #{swapped}", params[:dry_run])
end
fill(params = {}) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 25
def fill(params = {})
  table_name = params.fetch(:table_name)
  swapped = params.fetch(:swapped, '')
  swapped = '--swapped' if swapped.to_s.casecmp('true').zero?

  run_pgslice("fill #{table_name} #{swapped}", params[:dry_run])
end
prep(params = {}) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 18
def prep(params = {})
  table_name = params.fetch(:table_name)
  column_name = params.fetch(:column_name)
  period = params.fetch(:period)
  run_pgslice("prep #{table_name} #{column_name} #{period}", params[:dry_run])
end
swap(params = {}) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 41
def swap(params = {})
  table_name = params.fetch(:table_name)
  run_pgslice("swap #{table_name}", params[:dry_run])
end
unprep(params = {}) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 72
def unprep(params = {})
  table_name = params.fetch(:table_name)

  run_pgslice("unprep #{table_name}", params[:dry_run])
rescue PgSliceError => e
  logger.error { "Rescued PgSliceError: #{e}" }
  false
end
unprep!(params = {}) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 60
def unprep!(params = {})
  table_name = params.fetch(:table_name)

  run_pgslice("unprep #{table_name}", params[:dry_run])
end
unswap(params = {}) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 81
def unswap(params = {})
  table_name = params.fetch(:table_name)

  run_pgslice("unswap #{table_name}", params[:dry_run])
rescue PgSliceError => e
  logger.error { "Rescued PgSliceError: #{e}" }
  false
end
unswap!(params = {}) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 66
def unswap!(params = {})
  table_name = params.fetch(:table_name)

  run_pgslice("unswap #{table_name}", params[:dry_run])
end

Private Instance Methods

build_pg_slice_command(argument_string, dry_run) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 105
def build_pg_slice_command(argument_string, dry_run)
  argument_string = argument_string.strip
  $stdout.flush
  $stderr.flush
  command = "pgslice #{argument_string}"
  command += ' --dry-run true' if @dry_run || dry_run
  command = squish(command)
  logger.info { "Running pgslice command: '#{command}'" }
  command + " --url #{database_url}"
end
log_result(stdout, stderr) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 116
def log_result(stdout, stderr)
  logger.debug { "pgslice STDERR: #{stderr}" } unless blank?(stderr)
  logger.debug { "pgslice STDOUT: #{stdout}" } unless blank?(stdout)
end
log_status(status) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 121
def log_status(status)
  logger.debug { "pgslice exit status: #{status}" } unless blank?(status) || status.to_i.zero?
end
run_and_log(command) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 125
def run_and_log(command)
  PgDice::LogHelper.log_duration('PgSlice', logger) do
    stdout, stderr, status = @pg_slice_executor.call(command)
    log_result(stdout, stderr)
    log_status(status)
    [stdout, stderr, status]
  end
end
run_pgslice(argument_string, dry_run) click to toggle source
# File lib/pgdice/pg_slice_manager.rb, line 92
def run_pgslice(argument_string, dry_run)
  command = build_pg_slice_command(argument_string, dry_run)

  stdout, stderr, status = run_and_log(command)

  if status.to_i.positive?
    raise PgDice::PgSliceError,
          "pgslice with arguments: '#{argument_string}' failed with status: '#{status}' "\
             "STDOUT: '#{stdout}' STDERR: '#{stderr}'"
  end
  true
end