2021-09-23 11:11:29 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Database::MultipleDatabases' do
|
|
|
|
describe '.with_reestablished_active_record_base' do
|
|
|
|
context 'when doing establish_connection' do
|
|
|
|
context 'on ActiveRecord::Base' do
|
|
|
|
it 'raises exception' do
|
2021-12-22 01:13:39 -05:00
|
|
|
expect { ActiveRecord::Base.establish_connection(:main) }.to raise_error /Cannot re-establish/ # rubocop: disable Database/EstablishConnection
|
2021-09-23 11:11:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using with_reestablished_active_record_base' do
|
|
|
|
it 'does not raise exception' do
|
|
|
|
with_reestablished_active_record_base do
|
2021-12-22 01:13:39 -05:00
|
|
|
expect { ActiveRecord::Base.establish_connection(:main) }.not_to raise_error # rubocop: disable Database/EstablishConnection
|
2021-09-23 11:11:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-02 23:13:15 -04:00
|
|
|
context 'on Ci::ApplicationRecord' do
|
2021-09-23 11:11:29 -04:00
|
|
|
before do
|
|
|
|
skip_if_multiple_databases_not_setup
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises exception' do
|
2021-12-22 01:13:39 -05:00
|
|
|
expect { Ci::ApplicationRecord.establish_connection(:ci) }.to raise_error /Cannot re-establish/ # rubocop: disable Database/EstablishConnection
|
2021-09-23 11:11:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when using with_reestablished_active_record_base' do
|
|
|
|
it 'does not raise exception' do
|
|
|
|
with_reestablished_active_record_base do
|
2021-12-22 01:13:39 -05:00
|
|
|
expect { Ci::ApplicationRecord.establish_connection(:main) }.not_to raise_error # rubocop: disable Database/EstablishConnection
|
2021-09-23 11:11:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when trying to access connection' do
|
|
|
|
context 'when reconnect is true' do
|
|
|
|
it 'does not raise exception' do
|
|
|
|
with_reestablished_active_record_base(reconnect: true) do
|
2021-12-20 07:10:29 -05:00
|
|
|
expect { ApplicationRecord.connection.execute("SELECT 1") }.not_to raise_error
|
2021-09-23 11:11:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when reconnect is false' do
|
|
|
|
it 'does raise exception' do
|
|
|
|
with_reestablished_active_record_base(reconnect: false) do
|
2021-12-20 07:10:29 -05:00
|
|
|
expect { ApplicationRecord.connection.execute("SELECT 1") }.to raise_error(ActiveRecord::ConnectionNotEstablished)
|
2021-09-23 11:11:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-11-23 04:10:20 -05:00
|
|
|
|
|
|
|
describe '.with_added_ci_connection' do
|
|
|
|
context 'when only a single database is setup' do
|
|
|
|
before do
|
|
|
|
skip_if_multiple_databases_are_setup
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'connects Ci::ApplicationRecord to the main database for the duration of the block', :aggregate_failures do
|
|
|
|
main_database = current_database(ActiveRecord::Base)
|
|
|
|
original_database = current_database(Ci::ApplicationRecord)
|
|
|
|
|
|
|
|
with_added_ci_connection do
|
|
|
|
expect(current_database(Ci::ApplicationRecord)).to eq(main_database)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(current_database(Ci::ApplicationRecord)).to eq(original_database)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when multiple databases are setup' do
|
|
|
|
before do
|
|
|
|
skip_if_multiple_databases_not_setup
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not mock the original Ci::ApplicationRecord connection', :aggregate_failures do
|
|
|
|
original_database = current_database(Ci::ApplicationRecord)
|
|
|
|
|
|
|
|
with_added_ci_connection do
|
|
|
|
expect(current_database(Ci::ApplicationRecord)).to eq(original_database)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(current_database(Ci::ApplicationRecord)).to eq(original_database)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_database(connection_class)
|
|
|
|
connection_class.retrieve_connection.execute('select current_database()').first
|
|
|
|
end
|
|
|
|
end
|
2021-09-23 11:11:29 -04:00
|
|
|
end
|