Class: Tablature::Adapters::Postgres

Inherits:
Object
  • Object
show all
Defined in:
lib/tablature/adapters/postgres.rb,
lib/tablature/adapters/postgres/uuid.rb,
lib/tablature/adapters/postgres/errors.rb,
lib/tablature/adapters/postgres/quoting.rb,
lib/tablature/adapters/postgres/connection.rb,
lib/tablature/adapters/postgres/handlers/base.rb,
lib/tablature/adapters/postgres/handlers/list.rb,
lib/tablature/adapters/postgres/handlers/range.rb,
lib/tablature/adapters/postgres/partitioned_tables.rb

Overview

An adapter for managing Postgres views.

These methods are used internally by Tablature and are not intended for direct use. Methods that alter database schema are intended to be called via Statements.

The methods are documented here for insight into specifics of how Tablature integrates with Postgres and the responsibilities of Tablature::Adapters.

Defined Under Namespace

Modules: Handlers, Quoting, UUID Classes: Connection, ListPartitionsNotSupportedError, MissingListPartitionValuesError, MissingRangePartitionBoundsError, PartitionedTables, RangePartitionsNotSupportedError

Instance Method Summary collapse

Constructor Details

#initialize(connectable = ActiveRecord::Base) ⇒ Postgres

Creates an instance of the Tablature Postgres adapter.

This is the default adapter for Tablature. Configuring it via Tablature.configure is not required, but the example below shows how one would explicitly set it.

Examples:

Tablature.configure do |config|
  config.database = Tablature::Adapters::Postgres.new
end

Parameters:

  • connectable (#connection) (defaults to: ActiveRecord::Base)

    An object that returns the connection for Tablature to use. Defaults to ActiveRecord::Base.



38
39
40
# File 'lib/tablature/adapters/postgres.rb', line 38

def initialize(connectable = ActiveRecord::Base)
  @connectable = connectable
end

Instance Method Details

#create_list_partition(table_name, options) {|td| ... } ⇒ Object

Creates a partitioned table using the list partition method.

This is called in a migration via Statements#create_list_partition.

Examples:

create_list_partition :events, partition_key: :id
create_list_partition :events, partition_key: :date do |t|
  t.date :date, null: false
end

Parameters:

  • table_name (String, Symbol)

    The name of the table to partition.

  • options (Hash)

    The options to create the partition. Keys besides :partition_key will be passed to create_table.

Options Hash (options):

  • :partition_key (String, Symbol, #call)

    The partition key.

Yields:

  • (td)

    A TableDefinition object. This allows creating the table columns the same way as Rails’s create_table does.



61
# File 'lib/tablature/adapters/postgres.rb', line 61

delegate :create_list_partition, to: :list_handler

#create_list_partition_of(parent_table_name, options) ⇒ Object

Creates a partition of a parent by specifying the key values appearing in the partition.

Examples:

# With a table :events partitioned using the list method on the partition key `date`:
create_list_partition_of :events, name: "events_2018-W49", values: [
  "2018-12-03", "2018-12-04", "2018-12-05", "2018-12-06", "2018-12-07", "2018-12-08", "2018-12-09"
]

Parameters:

  • parent_table_name (String, Symbol)

    The name of the parent table.

  • options (Hash)

    The options to create the partition.

Options Hash (options):

  • :values (String, Symbol)

    The values appearing in the partition.

  • :name (String, Symbol)

    The name of the partition. If it is not given, this will be randomly generated.



77
# File 'lib/tablature/adapters/postgres.rb', line 77

delegate :create_list_partition_of, to: :list_handler

#create_range_partition(table_name, options) {|td| ... } ⇒ Object

Creates a partitioned table using the range partition method.

This is called in a migration via Statements#create_range_partition.

Examples:

create_range_partition :events, partition_key: :id
create_range_partition :events, partition_key: :date do |t|
  t.date :date, null: false
end

Parameters:

  • table_name (String, Symbol)

    The name of the table to partition.

  • options (Hash)

    The options to create the partition. Keys besides :partition_key will be passed to create_table.

Yields:

  • (td)

    A TableDefinition object. This allows creating the table columns the same way as Rails’s create_table does.



97
# File 'lib/tablature/adapters/postgres.rb', line 97

delegate :create_range_partition, to: :range_handler

#create_range_partition_of(parent_table_name, options) ⇒ Object

Creates a partition of a parent by specifying the bound of the values appearing in the

partition.

Examples:

# With a table :events partitioned using the range method on the partition key `date`:
create_range_partition_of :events, name: "events_2018-W49", range_start: '2018-12-03',
                                                            range_end: '2018-12-10'

Parameters:

  • parent_table_name (String, Symbol)

    The name of the parent table.

  • options (Hash)

    The options to create the partition.

Options Hash (options):

  • :range_start (String, Symbol)

    The start of the range of values appearing in the partition.

  • :range_end (String, Symbol)

    The end of the range of values appearing in the partition.

  • :name (String, Symbol)

    The name of the partition. If it is not given, this will be randomly generated.



116
# File 'lib/tablature/adapters/postgres.rb', line 116

delegate :create_range_partition_of, to: :range_handler

#partitioned_tablesArray<Tablature::PartitionedTable]

Returns an array of partitioned tables in the database.

This collection of tables is used by the [Tablature::SchemaDumper] to populate the schema.rb file.

Returns:



124
125
126
# File 'lib/tablature/adapters/postgres.rb', line 124

def partitioned_tables
  PartitionedTables.new(connection).all
end