Class: Tablature::Adapters::Postgres::Handlers::Range Private

Inherits:
Base
  • Object
show all
Defined in:
lib/tablature/adapters/postgres/handlers/range.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Methods included from UUID

#uuid_function

Methods included from Quoting

#quote_collection, #quote_partition_key

Constructor Details

#initialize(connection) ⇒ Range

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Range.



9
10
11
# File 'lib/tablature/adapters/postgres/handlers/range.rb', line 9

def initialize(connection)
  @connection = connection
end

Instance Method Details

#create_range_partition(table_name, options, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tablature/adapters/postgres/handlers/range.rb', line 13

def create_range_partition(table_name, options, &block)
  raise_unless_range_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 RANGE (#{quote_partition_key(partition_key)})"

  create_partition(table_name, id_options, modified_options, &block)
end

#create_range_partition_of(parent_table, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tablature/adapters/postgres/handlers/range.rb', line 31

def create_range_partition_of(parent_table, options)
  range_start = options.fetch(:range_start, nil)
  range_end = options.fetch(:range_end, nil)

  raise MissingRangePartitionBoundsError if range_start.nil? || range_end.nil?

  name = options.fetch(:name, partition_name(parent_table, range_start, range_end))
  # 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 = "    CREATE TABLE \#{quote_table_name(name)} PARTITION OF \#{quote_table_name(parent_table)}\n    FOR VALUES FROM (\#{quote(range_start)}) TO (\#{quote(range_end)});\n  SQL\n\n  execute(query)\nend\n".strip