Class: PgParty::ModelDecorator

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

Instance Method Summary collapse

Instance Method Details

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



89
90
91
92
93
94
95
96
# File 'lib/pg_party/model_decorator.rb', line 89

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



79
80
81
82
83
84
85
86
87
# File 'lib/pg_party/model_decorator.rb', line 79

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



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

#partitionsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pg_party/model_decorator.rb', line 65

def partitions
  PgParty.cache.fetch_partitions(cache_key) do
    connection.select_values(<<-SQL)
      SELECT pg_inherits.inhrelid::regclass::text
      FROM pg_tables
      INNER JOIN pg_inherits
        ON pg_tables.tablename::regclass = pg_inherits.inhparent::regclass
      WHERE pg_tables.tablename = #{connection.quote(table_name)}
    SQL
  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