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



102
103
104
105
106
107
# File 'lib/pg_party/model_decorator.rb', line 102

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



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

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



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

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



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

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



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

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_arel(:eq, value))
  end
end

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



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

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
# 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_arel(:gteq, start_range).and(partition_key_arel(:lt, end_range)))
  end
end