class Tablature::Adapters::Postgres::Handlers::List

@api private

Attributes

connection[R]

Public Class Methods

new(connection) click to toggle source
# File lib/tablature/adapters/postgres/handlers/list.rb, line 9
def initialize(connection)
  @connection = connection
end

Public Instance Methods

create_list_partition(table_name, options, &block) click to toggle source
# File lib/tablature/adapters/postgres/handlers/list.rb, line 13
def create_list_partition(table_name, options, &block)
  raise_unless_list_partition_supported

  # Postgres 10 does not handle indexes and therefore primary keys.
  # Therefore we manually create an `id` column.
  # TODO: Either make the library Postgres 11 only, or two code paths between Postgres 10
  # and Postgres 11.
  modified_options = options.except(:id, :primary_key, :partition_key)
  id_options = extract_primary_key!(options.slice(:id, :primary_key))
  partition_key = options.fetch(:partition_key)

  modified_options[:id]      = false
  modified_options[:options] = "PARTITION BY LIST (#{quote_partition_key(partition_key)})"

  create_partition(table_name, id_options, modified_options, &block)
end
create_list_partition_of(parent_table, options) click to toggle source
# File lib/tablature/adapters/postgres/handlers/list.rb, line 30
          def create_list_partition_of(parent_table, options)
            values = options.fetch(:values, [])
            raise MissingListPartitionValuesError if values.blank?

            name = options.fetch(:name, partition_name(parent_table, values))
            # TODO: Call `create_table` here instead of running the query.
            # TODO: Pass the options to `create_table` to allow further configuration of the table,
            # e.g. sub-partitioning the table.
            query = <<~SQL.strip
              CREATE TABLE #{quote_table_name(name)} PARTITION OF #{quote_table_name(parent_table)}
              FOR VALUES IN (#{quote_collection(values)})
            SQL

            execute(query)
          end

Private Instance Methods

partition_name(parent_table, values) click to toggle source

TODO: Better ?

# File lib/tablature/adapters/postgres/handlers/list.rb, line 58
def partition_name(parent_table, values)
  key = values.inspect
  "#{parent_table}_#{Digest::MD5.hexdigest(key)[0..6]}"
end
raise_unless_list_partition_supported() click to toggle source
# File lib/tablature/adapters/postgres/handlers/list.rb, line 53
def raise_unless_list_partition_supported
  raise ListPartitionsNotSupportedError unless connection.supports_list_partitions?
end