debian-mirror-gitlab/lib/gitlab/database/postgres_partitioned_table.rb

45 lines
1.1 KiB
Ruby
Raw Normal View History

2021-01-29 00:20:46 +05:30
# frozen_string_literal: true
module Gitlab
module Database
2021-11-11 11:23:49 +05:30
class PostgresPartitionedTable < SharedModel
2021-01-29 00:20:46 +05:30
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
2023-04-23 21:23:45 +05:30
unless identifier =~ Gitlab::Database::FULLY_QUALIFIED_IDENTIFIER
raise ArgumentError, "Table name is not fully qualified with a schema: #{identifier}"
end
2021-01-29 00:20:46 +05:30
find(identifier)
end
def self.find_by_name_in_current_schema(name)
find_by("identifier = concat(current_schema(), '.', ?)", name)
end
2023-04-23 21:23:45 +05:30
def self.each_partition(table_name, &block)
find_by_name_in_current_schema(table_name)
.postgres_partitions
.order(:name)
.each(&block)
end
2021-01-29 00:20:46 +05:30
def dynamic?
DYNAMIC_PARTITION_STRATEGIES.include?(strategy)
end
def static?
!dynamic?
end
def to_s
name
end
end
end
end