gitlab-org--gitlab-foss/spec/lib/gitlab/database/no_cross_db_foreign_keys_sp...

35 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'cross-database foreign keys' do
# Since we don't expect to have any cross-database foreign keys
# this is empty. If we will have an entry like
# `ci_daily_build_group_report_results.project_id`
# should be added.
let(:allowed_cross_database_foreign_keys) do
%w[].freeze
end
def foreign_keys_for(table_name)
ApplicationRecord.connection.foreign_keys(table_name)
end
def is_cross_db?(fk_record)
Gitlab::Database::GitlabSchema.table_schemas([fk_record.from_table, fk_record.to_table]).many?
end
it 'onlies have allowed list of cross-database foreign keys', :aggregate_failures do
all_tables = ApplicationRecord.connection.data_sources
all_tables.each do |table|
foreign_keys_for(table).each do |fk|
if is_cross_db?(fk)
column = "#{fk.from_table}.#{fk.column}"
expect(allowed_cross_database_foreign_keys).to include(column), "Found extra cross-database foreign key #{column} referencing #{fk.to_table} with constraint name #{fk.name}. When a foreign key references another database you must use a Loose Foreign Key instead https://docs.gitlab.com/ee/development/database/loose_foreign_keys.html ."
end
end
end
end
end