2022-02-23 07:13:44 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rake_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'dev rake tasks' do
|
|
|
|
before do
|
|
|
|
Rake.application.rake_require 'tasks/gitlab/setup'
|
|
|
|
Rake.application.rake_require 'tasks/gitlab/shell'
|
|
|
|
Rake.application.rake_require 'tasks/dev'
|
2022-03-28 23:08:22 -04:00
|
|
|
Rake.application.rake_require 'active_record/railties/databases'
|
|
|
|
Rake.application.rake_require 'tasks/gitlab/db'
|
2022-08-03 05:12:13 -04:00
|
|
|
Rake.application.rake_require 'tasks/seed_fu'
|
2022-02-23 07:13:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'setup' do
|
2022-04-18 23:09:43 -04:00
|
|
|
around do |example|
|
|
|
|
old_force_value = ENV['force']
|
|
|
|
|
|
|
|
# setup rake task sets the force env var, so reset it
|
|
|
|
example.run
|
|
|
|
|
|
|
|
ENV['force'] = old_force_value # rubocop:disable RSpec/EnvAssignment
|
|
|
|
end
|
|
|
|
|
2022-02-23 07:13:44 -05:00
|
|
|
subject(:setup_task) { run_rake_task('dev:setup') }
|
|
|
|
|
2022-07-19 02:10:05 -04:00
|
|
|
let(:connections) { Gitlab::Database.database_base_models_with_gitlab_shared.values.map(&:connection) }
|
2022-02-23 07:13:44 -05:00
|
|
|
|
|
|
|
it 'sets up the development environment', :aggregate_failures do
|
|
|
|
expect(Rake::Task['gitlab:setup']).to receive(:invoke)
|
|
|
|
|
2022-03-28 20:09:12 -04:00
|
|
|
expect(connections).to all(receive(:execute).with('SET statement_timeout TO 0'))
|
2022-02-23 07:13:44 -05:00
|
|
|
expect(connections).to all(receive(:execute).with('ANALYZE'))
|
2022-03-28 20:09:12 -04:00
|
|
|
expect(connections).to all(receive(:execute).with('RESET statement_timeout'))
|
2022-02-23 07:13:44 -05:00
|
|
|
|
|
|
|
expect(Rake::Task['gitlab:shell:setup']).to receive(:invoke)
|
|
|
|
|
|
|
|
setup_task
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-03 05:12:13 -04:00
|
|
|
describe 'fixtures:load' do
|
|
|
|
subject(:load_task) { run_rake_task('dev:fixtures:load', task_param) }
|
|
|
|
|
|
|
|
context 'by name' do
|
|
|
|
let(:task_param) { ['fixture_name'] }
|
|
|
|
|
|
|
|
it 'loads fixture' do
|
|
|
|
expect(Rake::Task['db:seed_fu']).to receive(:invoke)
|
|
|
|
|
|
|
|
load_task
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'by empty name' do
|
|
|
|
let(:task_param) { '' }
|
|
|
|
|
|
|
|
it 'does not load fixture' do
|
|
|
|
expect(Rake::Task['db:seed_fu']).not_to receive(:invoke)
|
|
|
|
|
|
|
|
expect { load_task }.to output(/No fixture name was provided/).to_stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-23 07:13:44 -05:00
|
|
|
describe 'load' do
|
|
|
|
subject(:load_task) { run_rake_task('dev:load') }
|
|
|
|
|
|
|
|
it 'eager loads the application', :aggregate_failures do
|
|
|
|
expect(Rails.configuration).to receive(:eager_load=).with(true)
|
|
|
|
expect(Rails.application).to receive(:eager_load!)
|
|
|
|
|
|
|
|
load_task
|
|
|
|
end
|
|
|
|
end
|
2022-03-28 23:08:22 -04:00
|
|
|
|
2022-04-06 11:08:23 -04:00
|
|
|
describe 'terminate_all_connections' do
|
2022-06-26 20:08:19 -04:00
|
|
|
before do
|
|
|
|
allow(ActiveRecord::Base).to receive(:clear_all_connections!)
|
|
|
|
end
|
|
|
|
|
2022-04-06 11:08:23 -04:00
|
|
|
let(:connections) do
|
2022-07-19 02:10:05 -04:00
|
|
|
Gitlab::Database.database_base_models_with_gitlab_shared.values.filter_map do |model|
|
2022-04-06 11:08:23 -04:00
|
|
|
model.connection if Gitlab::Database.db_config_share_with(model.connection_db_config).nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_connections_to_be_terminated
|
|
|
|
expect(Gitlab::Database::EachDatabase).to receive(:each_database_connection)
|
|
|
|
.with(include_shared: false)
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
expect(connections).to all(receive(:execute).with(/SELECT pg_terminate_backend/))
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_connections_not_to_be_terminated
|
|
|
|
connections.each do |connection|
|
|
|
|
expect(connection).not_to receive(:execute)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject(:terminate_task) { run_rake_task('dev:terminate_all_connections') }
|
|
|
|
|
|
|
|
it 'terminates all connections' do
|
|
|
|
expect_connections_to_be_terminated
|
|
|
|
|
2022-06-26 20:08:19 -04:00
|
|
|
expect(ActiveRecord::Base).to receive(:clear_all_connections!)
|
|
|
|
|
2022-04-06 11:08:23 -04:00
|
|
|
terminate_task
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in the production environment' do
|
|
|
|
it 'does not terminate connections' do
|
|
|
|
expect(Rails.env).to receive(:production?).and_return(true)
|
|
|
|
expect_connections_not_to_be_terminated
|
2022-06-26 20:08:19 -04:00
|
|
|
expect(ActiveRecord::Base).not_to receive(:clear_all_connections!)
|
2022-04-06 11:08:23 -04:00
|
|
|
|
|
|
|
terminate_task
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a database is not found' do
|
|
|
|
before do
|
|
|
|
skip_if_multiple_databases_not_setup
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'continues to next connection' do
|
|
|
|
expect(connections.first).to receive(:execute).and_raise(ActiveRecord::NoDatabaseError)
|
|
|
|
expect(connections.second).to receive(:execute).with(/SELECT pg_terminate_backend/)
|
|
|
|
|
|
|
|
terminate_task
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-31 08:08:17 -04:00
|
|
|
context 'multiple databases' do
|
2022-03-28 23:08:22 -04:00
|
|
|
before do
|
|
|
|
skip_if_multiple_databases_not_setup
|
|
|
|
end
|
|
|
|
|
2022-03-31 08:08:17 -04:00
|
|
|
context 'with a valid database' do
|
|
|
|
describe 'copy_db:ci' do
|
|
|
|
before do
|
2022-04-06 11:08:23 -04:00
|
|
|
allow(Rake::Task['dev:terminate_all_connections']).to receive(:invoke)
|
|
|
|
|
2022-03-31 08:08:17 -04:00
|
|
|
configurations = instance_double(ActiveRecord::DatabaseConfigurations)
|
|
|
|
allow(ActiveRecord::Base).to receive(:configurations).and_return(configurations)
|
|
|
|
allow(configurations).to receive(:configs_for).with(env_name: Rails.env, name: 'ci').and_return(ci_configuration)
|
|
|
|
end
|
2022-03-28 23:08:22 -04:00
|
|
|
|
2022-04-19 23:08:31 -04:00
|
|
|
subject(:load_task) { run_rake_task('dev:copy_db:ci') }
|
2022-03-28 23:08:22 -04:00
|
|
|
|
2022-03-31 08:08:17 -04:00
|
|
|
let(:ci_configuration) { instance_double(ActiveRecord::DatabaseConfigurations::HashConfig, name: 'ci', database: '__test_db_ci') }
|
2022-03-28 23:08:22 -04:00
|
|
|
|
2022-03-31 08:08:17 -04:00
|
|
|
it 'creates the database from main' do
|
|
|
|
expect(ApplicationRecord.connection).to receive(:create_database).with(
|
|
|
|
ci_configuration.database,
|
|
|
|
template: ApplicationRecord.connection_db_config.database
|
|
|
|
)
|
|
|
|
|
2022-04-06 11:08:23 -04:00
|
|
|
expect(Rake::Task['dev:terminate_all_connections']).to receive(:invoke)
|
|
|
|
|
2022-04-19 23:08:31 -04:00
|
|
|
load_task
|
2022-03-31 08:08:17 -04:00
|
|
|
end
|
2022-03-28 23:08:22 -04:00
|
|
|
|
2022-03-31 08:08:17 -04:00
|
|
|
context 'when the database already exists' do
|
|
|
|
it 'prints out a warning' do
|
|
|
|
expect(ApplicationRecord.connection).to receive(:create_database).and_raise(ActiveRecord::DatabaseAlreadyExists)
|
|
|
|
|
2022-04-19 23:08:31 -04:00
|
|
|
expect { load_task }.to output(/Database '#{ci_configuration.database}' already exists/).to_stderr
|
2022-03-31 08:08:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-03-28 23:08:22 -04:00
|
|
|
|
2022-03-31 08:08:17 -04:00
|
|
|
context 'with an invalid database' do
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { run_rake_task('dev:copy_db:foo') }.to raise_error(RuntimeError, /Don't know how to build task/)
|
2022-03-28 23:08:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-02-23 07:13:44 -05:00
|
|
|
end
|