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



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

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



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

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pg_party/model_decorator.rb', line 23

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
    end
  end
end

#list_partition_key_in(*values) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/pg_party/model_decorator.rb', line 70

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



48
49
50
51
52
53
54
# File 'lib/pg_party/model_decorator.rb', line 48

def partition_key_eq(value)
  if complex_partition_key
    complex_partition_key_query("(#{partition_key}) = (?)", value)
  else
    where(current_arel_table[partition_key].eq(value))
  end
end

#partition_primary_keyObject



7
8
9
10
11
12
13
14
15
# File 'lib/pg_party/model_decorator.rb', line 7

def partition_primary_key
  if self != base_class
    base_class.primary_key
  elsif partition_name = partitions.first
    in_partition(partition_name).get_primary_key(base_class.name)
  else
    get_primary_key(base_class.name)
  end
end

#partition_table_exists?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/pg_party/model_decorator.rb', line 17

def partition_table_exists?
  target_table = partitions.first || table_name

  connection.schema_cache.data_source_exists?(target_table)
end

#partitionsObject



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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pg_party/model_decorator.rb', line 56

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
    node = current_arel_table[partition_key]

    where(node.gteq(start_range).and(node.lt(end_range)))
  end
end