2021-11-24 07:10:21 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module LooseForeignKeys
|
|
|
|
def self.definitions_by_table
|
2021-12-01 10:13:55 -05:00
|
|
|
@definitions_by_table ||= definitions.group_by(&:to_table).with_indifferent_access.freeze
|
2021-11-24 07:10:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.definitions
|
2021-12-01 10:13:55 -05:00
|
|
|
@definitions ||= loose_foreign_keys_yaml.flat_map do |child_table_name, configs|
|
|
|
|
configs.map { |config| build_definition(child_table_name, config) }
|
2021-11-24 07:10:21 -05:00
|
|
|
end.freeze
|
|
|
|
end
|
|
|
|
|
2021-12-01 10:13:55 -05:00
|
|
|
def self.build_definition(child_table_name, config)
|
|
|
|
parent_table_name = config.fetch('table')
|
2021-11-24 07:10:21 -05:00
|
|
|
|
|
|
|
ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(
|
2021-12-01 10:13:55 -05:00
|
|
|
child_table_name,
|
2021-11-24 07:10:21 -05:00
|
|
|
parent_table_name,
|
|
|
|
{
|
|
|
|
column: config.fetch('column'),
|
|
|
|
on_delete: config.fetch('on_delete').to_sym,
|
2021-12-01 10:13:55 -05:00
|
|
|
gitlab_schema: GitlabSchema.table_schema(child_table_name)
|
2021-11-24 07:10:21 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.loose_foreign_keys_yaml
|
2022-01-26 10:12:36 -05:00
|
|
|
@loose_foreign_keys_yaml ||= YAML.load_file(self.loose_foreign_keys_yaml_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.loose_foreign_keys_yaml_path
|
2022-02-13 22:12:20 -05:00
|
|
|
@loose_foreign_keys_yaml_path ||= Rails.root.join('config/gitlab_loose_foreign_keys.yml')
|
2021-11-24 07:10:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :build_definition
|
|
|
|
private_class_method :loose_foreign_keys_yaml
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|