Commit a9709aaf authored by Thong Kuah's avatar Thong Kuah

Merge branch 'setup_ci_db' into 'master'

Add task to copy CI db in development

See merge request gitlab-org/gitlab!82364
parents 43292e03 b97a8226
......@@ -26,4 +26,21 @@ namespace :dev do
Rails.configuration.eager_load = true
Rails.application.eager_load!
end
databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
namespace :copy_db do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
next if name == 'main'
desc "Copies the #{name} database from the main database"
task name => :environment do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
ApplicationRecord.connection.create_database(db_config.database, template: ApplicationRecord.connection_db_config.database)
rescue ActiveRecord::DatabaseAlreadyExists
warn "Database '#{db_config.database}' already exists"
end
end
end
end
......@@ -7,6 +7,8 @@ RSpec.describe 'dev rake tasks' do
Rake.application.rake_require 'tasks/gitlab/setup'
Rake.application.rake_require 'tasks/gitlab/shell'
Rake.application.rake_require 'tasks/dev'
Rake.application.rake_require 'active_record/railties/databases'
Rake.application.rake_require 'tasks/gitlab/db'
end
describe 'setup' do
......@@ -37,4 +39,35 @@ RSpec.describe 'dev rake tasks' do
load_task
end
end
describe 'copy_db:ci' do
before do
skip_if_multiple_databases_not_setup
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
subject(:load_task) { run_rake_task('dev:setup_ci_db') }
let(:ci_configuration) { instance_double(ActiveRecord::DatabaseConfigurations::HashConfig, name: 'ci', database: '__test_db_ci') }
it 'creates the database from main' do
expect(ApplicationRecord.connection).to receive(:create_database).with(
ci_configuration.database,
template: ApplicationRecord.connection_db_config.database
)
run_rake_task('dev:copy_db:ci')
end
context 'when the database already exists' do
it 'prints out a warning' do
expect(ApplicationRecord.connection).to receive(:create_database).and_raise(ActiveRecord::DatabaseAlreadyExists)
expect { run_rake_task('dev:copy_db:ci') }.to output(/Database '#{ci_configuration.database}' already exists/).to_stderr
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment