Welcome to mirror list, hosted at ThFree Co, Russian Federation.

postgres_partitioned_table.rb « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fead7379e431b37c9a14745ba91c9a600922fb43 (plain)
1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
# frozen_string_literal: true

module Gitlab
  module Database
    class PostgresPartitionedTable < SharedModel
      DYNAMIC_PARTITION_STRATEGIES = %w[range list].freeze

      self.primary_key = :identifier

      has_many :postgres_partitions, foreign_key: 'parent_identifier', primary_key: 'identifier'

      scope :by_identifier, ->(identifier) do
        unless identifier =~ Gitlab::Database::FULLY_QUALIFIED_IDENTIFIER
          raise ArgumentError, "Table name is not fully qualified with a schema: #{identifier}"
        end

        find(identifier)
      end

      def self.find_by_name_in_current_schema(name)
        find_by("identifier = concat(current_schema(), '.', ?)", name)
      end

      def self.each_partition(table_name, &block)
        find_by_name_in_current_schema(table_name)
        .postgres_partitions
        .order(:name)
        .each(&block)
      end

      def dynamic?
        DYNAMIC_PARTITION_STRATEGIES.include?(strategy)
      end

      def static?
        !dynamic?
      end

      def to_s
        name
      end
    end
  end
end