Class: Gitlab::Database::Partitioning::MonthlyStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/partitioning/monthly_strategy.rb

Constant Summary collapse

HEADROOM =

We create this many partitions in the future

6.months

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, partitioning_key) ⇒ MonthlyStrategy

Returns a new instance of MonthlyStrategy.


14
15
16
17
# File 'lib/gitlab/database/partitioning/monthly_strategy.rb', line 14

def initialize(model, partitioning_key)
  @model = model
  @partitioning_key = partitioning_key
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model


7
8
9
# File 'lib/gitlab/database/partitioning/monthly_strategy.rb', line 7

def model
  @model
end

#partitioning_keyObject (readonly)

Returns the value of attribute partitioning_key


7
8
9
# File 'lib/gitlab/database/partitioning/monthly_strategy.rb', line 7

def partitioning_key
  @partitioning_key
end

Instance Method Details

#current_partitionsObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gitlab/database/partitioning/monthly_strategy.rb', line 19

def current_partitions
  result = connection.select_all(<<~SQL)
    select
      pg_class.relname,
      parent_class.relname as base_table,
      pg_get_expr(pg_class.relpartbound, inhrelid) as condition
    from pg_class
    inner join pg_inherits i on pg_class.oid = inhrelid
    inner join pg_class parent_class on parent_class.oid = inhparent
    inner join pg_namespace ON pg_namespace.oid = pg_class.relnamespace
    where pg_namespace.nspname = #{connection.quote(Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA)}
      and parent_class.relname = #{connection.quote(table_name)}
      and pg_class.relispartition
    order by pg_class.relname
  SQL

  result.map do |record|
    TimePartition.from_sql(table_name, record['relname'], record['condition'])
  end
end

#missing_partitionsObject

Check the currently existing partitions and determine which ones are missing


41
42
43
# File 'lib/gitlab/database/partitioning/monthly_strategy.rb', line 41

def missing_partitions
  desired_partitions - current_partitions
end