2020-10-23 11:08:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
2021-09-16 14:11:32 -04:00
|
|
|
class PostgresPartition < SharedModel
|
2020-10-23 11:08:42 -04:00
|
|
|
self.primary_key = :identifier
|
|
|
|
|
|
|
|
belongs_to :postgres_partitioned_table, foreign_key: 'parent_identifier', primary_key: 'identifier'
|
|
|
|
|
2021-08-10 20:10:03 -04:00
|
|
|
scope :for_identifier, ->(identifier) do
|
2020-10-23 11:08:42 -04:00
|
|
|
raise ArgumentError, "Partition name is not fully qualified with a schema: #{identifier}" unless identifier =~ /^\w+\.\w+$/
|
|
|
|
|
2021-08-10 20:10:03 -04:00
|
|
|
where(primary_key => identifier)
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :by_identifier, ->(identifier) do
|
|
|
|
for_identifier(identifier).first!
|
2020-10-23 11:08:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
scope :for_parent_table, ->(name) { where("parent_identifier = concat(current_schema(), '.', ?)", name).order(:name) }
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|