2021-10-27 15:23:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
2021-11-11 11:23:49 +05:30
|
|
|
class PostgresForeignKey < SharedModel
|
2021-10-27 15:23:28 +05:30
|
|
|
self.primary_key = :oid
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
has_many :child_foreign_keys, class_name: 'Gitlab::Database::PostgresForeignKey', foreign_key: 'parent_oid'
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
# These values come from the possible confdeltype / confupdtype values in pg_constraint
|
|
|
|
ACTION_TYPES = {
|
2023-03-17 16:20:25 +05:30
|
|
|
restrict: 'r',
|
|
|
|
cascade: 'c',
|
|
|
|
nullify: 'n',
|
|
|
|
set_default: 'd',
|
|
|
|
no_action: 'a'
|
2023-04-23 21:23:45 +05:30
|
|
|
}.freeze
|
|
|
|
|
|
|
|
enum on_delete_action: ACTION_TYPES, _prefix: :on_delete
|
|
|
|
|
|
|
|
enum on_update_action: ACTION_TYPES, _prefix: :on_update
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
scope :by_referenced_table_identifier, ->(identifier) do
|
2023-04-23 21:23:45 +05:30
|
|
|
unless identifier =~ Database::FULLY_QUALIFIED_IDENTIFIER
|
|
|
|
raise ArgumentError, "Referenced table name is not fully qualified with a schema: #{identifier}"
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
|
|
|
|
where(referenced_table_identifier: identifier)
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
scope :by_referenced_table_name, ->(name) { where(referenced_table_name: name) }
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
scope :by_constrained_table_identifier, ->(identifier) do
|
2023-04-23 21:23:45 +05:30
|
|
|
unless identifier =~ Database::FULLY_QUALIFIED_IDENTIFIER
|
|
|
|
raise ArgumentError, "Constrained table name is not fully qualified with a schema: #{identifier}"
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
where(constrained_table_identifier: identifier)
|
|
|
|
end
|
2023-03-17 16:20:25 +05:30
|
|
|
|
|
|
|
scope :by_constrained_table_name, ->(name) { where(constrained_table_name: name) }
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
scope :by_constrained_table_name_or_identifier, ->(name) do
|
|
|
|
if name =~ Database::FULLY_QUALIFIED_IDENTIFIER
|
|
|
|
by_constrained_table_identifier(name)
|
|
|
|
else
|
|
|
|
by_constrained_table_name(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
scope :not_inherited, -> { where(is_inherited: false) }
|
|
|
|
|
|
|
|
scope :by_name, ->(name) { where(name: name) }
|
|
|
|
|
|
|
|
scope :by_constrained_columns, ->(cols) { where(constrained_columns: Array.wrap(cols)) }
|
|
|
|
|
|
|
|
scope :by_referenced_columns, ->(cols) { where(referenced_columns: Array.wrap(cols)) }
|
|
|
|
|
|
|
|
scope :by_on_delete_action, ->(on_delete) do
|
|
|
|
raise ArgumentError, "Invalid on_delete action #{on_delete}" unless on_delete_actions.key?(on_delete)
|
|
|
|
|
|
|
|
where(on_delete_action: on_delete)
|
|
|
|
end
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
scope :by_on_update_action, ->(on_update) do
|
|
|
|
raise ArgumentError, "Invalid on_update action #{on_update}" unless on_update_actions.key?(on_update)
|
|
|
|
|
|
|
|
where(on_update_action: on_update)
|
|
|
|
end
|
2021-10-27 15:23:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|