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

52 lines
1.5 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 PostgresPartition < SharedModel
2021-01-29 00:20:46 +05:30
self.primary_key = :identifier
belongs_to :postgres_partitioned_table, foreign_key: 'parent_identifier', primary_key: 'identifier'
2023-05-27 22:25:52 +05:30
# identifier includes the partition schema.
# For example 'gitlab_partitions_static.events_03', or 'gitlab_partitions_dynamic.logs_03'
2021-10-27 15:23:28 +05:30
scope :for_identifier, ->(identifier) do
2023-04-23 21:23:45 +05:30
unless identifier =~ Gitlab::Database::FULLY_QUALIFIED_IDENTIFIER
raise ArgumentError, "Partition name is not fully qualified with a schema: #{identifier}"
end
2021-01-29 00:20:46 +05:30
2021-10-27 15:23:28 +05:30
where(primary_key => identifier)
end
scope :by_identifier, ->(identifier) do
for_identifier(identifier).first!
2021-01-29 00:20:46 +05:30
end
2023-05-27 22:25:52 +05:30
scope :for_parent_table, ->(parent_table) do
if parent_table =~ Database::FULLY_QUALIFIED_IDENTIFIER
where(parent_identifier: parent_table).order(:name)
else
where("parent_identifier = concat(current_schema(), '.', ?)", parent_table).order(:name)
end
2023-03-17 16:20:25 +05:30
end
2021-01-29 00:20:46 +05:30
2023-01-13 00:05:48 +05:30
def self.partition_exists?(table_name)
where("identifier = concat(current_schema(), '.', ?)", table_name).exists?
end
def self.legacy_partition_exists?(table_name)
result = connection.select_value(<<~SQL)
SELECT true FROM pg_class
WHERE relname = '#{table_name}'
AND relispartition = true;
SQL
!!result
end
2021-01-29 00:20:46 +05:30
def to_s
name
end
end
end
end