class PgDice::PeriodFetcher

Used to find the period of a postgres table using the comment on the table created by pgslice

Public Class Methods

new(query_executor:) click to toggle source
# File lib/pgdice/period_fetcher.rb, line 6
def initialize(query_executor:)
  @query_executor = query_executor
end

Public Instance Methods

call(params) click to toggle source
# File lib/pgdice/period_fetcher.rb, line 10
def call(params)
  sql = build_table_comment_sql(params.fetch(:table_name), params.fetch(:schema))
  values = @query_executor.call(sql)
  convert_comment_to_hash(values.first)[:period]
end

Private Instance Methods

build_table_comment_sql(table_name, schema) click to toggle source
# File lib/pgdice/period_fetcher.rb, line 27
def build_table_comment_sql(table_name, schema)
  "SELECT obj_description('#{schema}.#{table_name}'::REGCLASS) AS comment"
end
convert_comment_to_hash(comment) click to toggle source
# File lib/pgdice/period_fetcher.rb, line 18
def convert_comment_to_hash(comment)
  return {} unless comment

  comment.split(',').reduce({}) do |hash, key_value_pair|
    key, value = key_value_pair.split(':')
    hash.merge(key.to_sym => value)
  end
end