Module: Parts::PartitionAdapter

Defined in:
lib/parts/partition_adapter.rb

Instance Method Summary collapse

Instance Method Details

#attach_child_to(parent_table_name, child_table_name, constraint_clause, lock_timeout: 5) ⇒ Object



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

def attach_child_to(parent_table_name, child_table_name, constraint_clause, lock_timeout: 5)
  execute <<~SQL
    SET LOCAL lock_timeout TO '#{lock_timeout}s';
    ALTER TABLE #{quote_table_name(parent_table_name)}
    ATTACH PARTITION #{quote_table_name(child_table_name)}
    FOR VALUES #{constraint_clause};
  SQL
end

#attach_hash_child_to(parent_table_name, child_table_name, modulus:, remainder:, **options) ⇒ Object



53
54
55
# File 'lib/parts/partition_adapter.rb', line 53

def attach_hash_child_to(parent_table_name, child_table_name, modulus:, remainder:, **options)
  attach_child_to(parent_table_name, child_table_name, hash_constraint_clause(modulus, remainder), **options)
end

#attach_list_child_to(parent_table_name, child_table_name, values, **options) ⇒ Object



45
46
47
# File 'lib/parts/partition_adapter.rb', line 45

def attach_list_child_to(parent_table_name, child_table_name, values, **options)
  attach_child_to(parent_table_name, child_table_name, list_constraint_clause(values), **options)
end

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



49
50
51
# File 'lib/parts/partition_adapter.rb', line 49

def attach_range_child_to(parent_table_name, child_table_name, start_range:, end_range:)
  attach_child_to(parent_table_name, child_table_name, range_constraint_clause(start_range, end_range), **options)
end

#create_hash_child_of(parent_table_name, table_name, modulus:, remainder:, **options) ⇒ Object



25
26
27
# File 'lib/parts/partition_adapter.rb', line 25

def create_hash_child_of(parent_table_name, table_name, modulus:, remainder:, **options)
  create_child_of(parent_table_name, table_name, hash_constraint_clause(modulus, remainder), **options)
end

#create_hash_children_of(parent_table_name, total, **options) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/parts/partition_adapter.rb', line 29

def create_hash_children_of(parent_table_name, total, **options)
  raise "'total' must be an Integer" unless total.is_a?(Integer)

  total.times do |t|
    create_hash_child_of(parent_table_name, "#{parent_table_name}_#{t}", modulus: total, remainder: t, **options)
  end
end

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



13
14
15
# File 'lib/parts/partition_adapter.rb', line 13

def create_hash_parent(table_name, partition_key, **options, &blk)
  create_parent(table_name, :hash, partition_key, options, &blk)
end

#create_list_child_of(parent_table_name, table_name, values, **options) ⇒ Object



21
22
23
# File 'lib/parts/partition_adapter.rb', line 21

def create_list_child_of(parent_table_name, table_name, values, **options)
  create_child_of(parent_table_name, table_name, list_constraint_clause(values), **options)
end

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



9
10
11
# File 'lib/parts/partition_adapter.rb', line 9

def create_list_parent(table_name, partition_key, **options, &blk)
  create_parent(table_name, :list, partition_key, options, &blk)
end

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



17
18
19
# File 'lib/parts/partition_adapter.rb', line 17

def create_range_child_of(parent_table_name, table_name, start_range:, end_range:, **options)
  create_child_of(parent_table_name, table_name, range_constraint_clause(start_range, end_range), **options)
end

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



5
6
7
# File 'lib/parts/partition_adapter.rb', line 5

def create_range_parent(table_name, partition_key, **options, &blk)
  create_parent(table_name, :range, partition_key, options, &blk)
end

#create_sub_parent(table_name, parent_table_name:, parent_type:, parent_values:, type:, partition_key:, lock_timeout: 5) ⇒ Object

rubocop:disable Metrics/ParameterLists



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/parts/partition_adapter.rb', line 66

def create_sub_parent(table_name, parent_table_name:, parent_type:, parent_values:, type:, partition_key:, lock_timeout: 5) # rubocop:disable Metrics/ParameterLists

  constraint_clause = case parent_type
  when :list
    list_constraint_clause(parent_values)
  when :range
    range_constraint_clause(parent_values[0], parent_values[1])
  when :hash
    hash_constraint_clause(parent_values[0], parent_values[1])
  end

  execute <<~SQL
    SET LOCAL lock_timeout TO '#{lock_timeout}s';
    CREATE TABLE #{table_name} PARTITION OF #{parent_table_name} FOR VALUES #{constraint_clause} PARTITION BY #{type.to_s.upcase} (#{partition_key});
  SQL
end

#detach_child_from(parent_table_name, child_table_name, lock_timeout: 5) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/parts/partition_adapter.rb', line 37

def detach_child_from(parent_table_name, child_table_name, lock_timeout: 5)
  execute <<~SQL
    SET LOCAL lock_timeout TO '#{lock_timeout}s';
    ALTER TABLE #{quote_table_name(parent_table_name)}
    DETACH PARTITION #{quote_table_name(child_table_name)};
  SQL
end