Class: PgParty::ModelDecorator

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

Instance Method Summary collapse

Instance Method Details

#create_default_partition(**options) ⇒ Object



104
105
106
107
108
109
# File 'lib/pg_party/model_decorator.rb', line 104

def create_default_partition(**options)
  modified_options = options.merge(
    primary_key: primary_key,
  )
  create_partition(:create_default_partition_of, table_name, **modified_options)
end

#create_hash_partition(modulus:, remainder:, **options) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/pg_party/model_decorator.rb', line 94

def create_hash_partition(modulus:, remainder:, **options)
  modified_options = options.merge(
    modulus: modulus,
    remainder: remainder,
    primary_key: primary_key,
  )

  create_partition(:create_hash_partition_of, table_name, **modified_options)
end

#create_list_partition(values:, **options) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/pg_party/model_decorator.rb', line 85

def create_list_partition(values:, **options)
  modified_options = options.merge(
    values: values,
    primary_key: primary_key,
  )

  create_partition(:create_list_partition_of, table_name, **modified_options)
end

#create_range_partition(start_range:, end_range:, **options) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/pg_party/model_decorator.rb', line 75

def create_range_partition(start_range:, end_range:, **options)
  modified_options = options.merge(
    start_range: start_range,
    end_range: end_range,
    primary_key: primary_key,
  )

  create_partition(:create_range_partition_of, table_name, **modified_options)
end

#in_partition(child_table_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pg_party/model_decorator.rb', line 5

def in_partition(child_table_name)
  PgParty.cache.fetch_model(cache_key, child_table_name) do
    Class.new(__getobj__) do
      self.table_name = child_table_name

      # to avoid argument errors when calling model_name
      def self.name
        superclass.name
      end

      # when returning records from a query, Rails
      # allocates objects first, then initializes
      def self.allocate
        superclass.allocate
      end

      # creating and persisting new records from a child partition
      # will ultimately insert into the parent partition table
      def self.new(*args, &blk)
        superclass.new(*args, &blk)
      end

      # to avoid unnecessary db lookups
      def self.partitions
        []
      end
    end
  end
end

#list_partition_key_in(*values) ⇒ Object Also known as: hash_partition_key_in



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

def list_partition_key_in(*values)
  if complex_partition_key
    complex_partition_key_query("(#{partition_key}) IN (?)", values.flatten)
  else
    where(current_arel_table[partition_key].in(values.flatten))
  end
end

#partition_key_eq(value) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/pg_party/model_decorator.rb', line 35

def partition_key_eq(value)
  if complex_partition_key
    complex_partition_key_query("(#{partition_key}) = (?)", value)
  else
    where_partition_key(:eq, value)
  end
end

#partitions(include_subpartitions: PgParty.config.include_subpartitions_in_partition_list) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/pg_party/model_decorator.rb', line 67

def partitions(include_subpartitions: PgParty.config.include_subpartitions_in_partition_list)
  PgParty.cache.fetch_partitions(cache_key, include_subpartitions) do
    connection.partitions_for_table_name(table_name, include_subpartitions: include_subpartitions)
  end
rescue
  []
end

#range_partition_key_in(start_range, end_range) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pg_party/model_decorator.rb', line 43

def range_partition_key_in(start_range, end_range)
  if complex_partition_key
    complex_partition_key_query(
      "(#{partition_key}) >= (?) AND (#{partition_key}) < (?)",
      start_range,
      end_range
    )
  else
    where_partition_key(:gteq, start_range).merge(
      where_partition_key(:lt, end_range)
    )
  end
end