Class: PgParty::AdapterDecorator

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/pg_party/adapter_decorator.rb

Instance Method Summary collapse

Constructor Details

#initialize(adapter) ⇒ AdapterDecorator

Returns a new instance of AdapterDecorator.



7
8
9
10
11
# File 'lib/pg_party/adapter_decorator.rb', line 7

def initialize(adapter)
  super(adapter)

  raise "Partitioning only supported in PostgreSQL >= 10.0" unless supports_partitions?
end

Instance Method Details

#attach_list_partition(parent_table_name, child_table_name, values:) ⇒ Object



53
54
55
# File 'lib/pg_party/adapter_decorator.rb', line 53

def attach_list_partition(parent_table_name, child_table_name, values:)
  attach_partition(parent_table_name, child_table_name, list_constraint_clause(values))
end

#attach_range_partition(parent_table_name, child_table_name, start_range:, end_range:) ⇒ Object



49
50
51
# File 'lib/pg_party/adapter_decorator.rb', line 49

def attach_range_partition(parent_table_name, child_table_name, start_range:, end_range:)
  attach_partition(parent_table_name, child_table_name, range_constraint_clause(start_range, end_range))
end

#create_list_partition(table_name, partition_key:, **options, &blk) ⇒ Object



17
18
19
# File 'lib/pg_party/adapter_decorator.rb', line 17

def create_list_partition(table_name, partition_key:, **options, &blk)
  create_partition(table_name, :list, partition_key, **options, &blk)
end

#create_list_partition_of(table_name, values:, **options) ⇒ Object



25
26
27
# File 'lib/pg_party/adapter_decorator.rb', line 25

def create_list_partition_of(table_name, values:, **options)
  create_partition_of(table_name, list_constraint_clause(values), **options)
end

#create_range_partition(table_name, partition_key:, **options, &blk) ⇒ Object



13
14
15
# File 'lib/pg_party/adapter_decorator.rb', line 13

def create_range_partition(table_name, partition_key:, **options, &blk)
  create_partition(table_name, :range, partition_key, **options, &blk)
end

#create_range_partition_of(table_name, start_range:, end_range:, **options) ⇒ Object



21
22
23
# File 'lib/pg_party/adapter_decorator.rb', line 21

def create_range_partition_of(table_name, start_range:, end_range:, **options)
  create_partition_of(table_name, range_constraint_clause(start_range, end_range), **options)
end

#create_table_like(table_name, new_table_name, **options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pg_party/adapter_decorator.rb', line 29

def create_table_like(table_name, new_table_name, **options)
  primary_key = options.fetch(:primary_key) { calculate_primary_key(table_name) }

  validate_primary_key(primary_key)

  execute("    CREATE TABLE \#{quote_table_name(new_table_name)} (\n      LIKE \#{quote_table_name(table_name)} INCLUDING ALL\n    )\n  SQL\n\n  return if !primary_key\n  return if has_primary_key?(new_table_name)\n\n  execute(<<-SQL)\n    ALTER TABLE \#{quote_table_name(new_table_name)}\n    ADD PRIMARY KEY (\#{quote_column_name(primary_key)})\n  SQL\nend\n")

#detach_partition(parent_table_name, child_table_name) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/pg_party/adapter_decorator.rb', line 57

def detach_partition(parent_table_name, child_table_name)
  execute("    ALTER TABLE \#{quote_table_name(parent_table_name)}\n    DETACH PARTITION \#{quote_table_name(child_table_name)}\n  SQL\n\n  PgParty.cache.clear!\nend\n")