diff options
Diffstat (limited to 'spec/tasks/gitlab/db_rake_spec.rb')
-rw-r--r-- | spec/tasks/gitlab/db_rake_spec.rb | 115 |
1 files changed, 87 insertions, 28 deletions
diff --git a/spec/tasks/gitlab/db_rake_spec.rb b/spec/tasks/gitlab/db_rake_spec.rb index c3fd8135ae0..8d3ec7b1ee2 100644 --- a/spec/tasks/gitlab/db_rake_spec.rb +++ b/spec/tasks/gitlab/db_rake_spec.rb @@ -20,6 +20,14 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do allow(Rake::Task['db:seed_fu']).to receive(:invoke).and_return(true) end + describe 'clear_all_connections' do + it 'calls clear_all_connections!' do + expect(ActiveRecord::Base).to receive(:clear_all_connections!) + + run_rake_task('gitlab:db:clear_all_connections') + end + end + describe 'mark_migration_complete' do context 'with a single database' do let(:main_model) { ActiveRecord::Base } @@ -253,45 +261,78 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do end describe 'drop_tables' do - subject { run_rake_task('gitlab:db:drop_tables') } - - let(:tables) { %w(one two) } + let(:tables) { %w(one two schema_migrations) } let(:views) { %w(three four) } - let(:connection) { ActiveRecord::Base.connection } + let(:schemas) { Gitlab::Database::EXTRA_SCHEMAS } - before do - allow(connection).to receive(:execute).and_return(nil) + context 'with a single database' do + let(:connection) { ActiveRecord::Base.connection } + + before do + skip_if_multiple_databases_are_setup + + allow(connection).to receive(:execute).and_return(nil) + + allow(connection).to receive(:tables).and_return(tables) + allow(connection).to receive(:views).and_return(views) + end + + it 'drops all objects for the database', :aggregate_failures do + expect_objects_to_be_dropped(connection) - allow(connection).to receive(:tables).and_return(tables) - allow(connection).to receive(:views).and_return(views) + run_rake_task('gitlab:db:drop_tables') + end end - it 'drops all tables, except schema_migrations' do - expect(connection).to receive(:execute).with('DROP TABLE IF EXISTS "one" CASCADE') - expect(connection).to receive(:execute).with('DROP TABLE IF EXISTS "two" CASCADE') + context 'with multiple databases', :aggregate_failures do + let(:main_model) { double(:model, connection: double(:connection, tables: tables, views: views)) } + let(:ci_model) { double(:model, connection: double(:connection, tables: tables, views: views)) } + let(:base_models) { { 'main' => main_model, 'ci' => ci_model } } - subject + before do + skip_if_multiple_databases_not_setup + + allow(Gitlab::Database).to receive(:database_base_models).and_return(base_models) + + allow(main_model.connection).to receive(:table_exists?).with('schema_migrations').and_return(true) + allow(ci_model.connection).to receive(:table_exists?).with('schema_migrations').and_return(true) + + (tables + views + schemas).each do |name| + allow(main_model.connection).to receive(:quote_table_name).with(name).and_return("\"#{name}\"") + allow(ci_model.connection).to receive(:quote_table_name).with(name).and_return("\"#{name}\"") + end + end + + it 'drops all objects for all databases', :aggregate_failures do + expect_objects_to_be_dropped(main_model.connection) + expect_objects_to_be_dropped(ci_model.connection) + + run_rake_task('gitlab:db:drop_tables') + end + + context 'when the single database task is used' do + it 'drops all objects for the given database', :aggregate_failures do + expect_objects_to_be_dropped(main_model.connection) + + expect(ci_model.connection).not_to receive(:execute) + + run_rake_task('gitlab:db:drop_tables:main') + end + end end - it 'drops all views' do + def expect_objects_to_be_dropped(connection) + expect(connection).to receive(:execute).with('DROP TABLE IF EXISTS "one" CASCADE') + expect(connection).to receive(:execute).with('DROP TABLE IF EXISTS "two" CASCADE') + expect(connection).to receive(:execute).with('DROP VIEW IF EXISTS "three" CASCADE') expect(connection).to receive(:execute).with('DROP VIEW IF EXISTS "four" CASCADE') - subject - end - - it 'truncates schema_migrations table' do expect(connection).to receive(:execute).with('TRUNCATE schema_migrations') - subject - end - - it 'drops extra schemas' do Gitlab::Database::EXTRA_SCHEMAS.each do |schema| expect(connection).to receive(:execute).with("DROP SCHEMA IF EXISTS \"#{schema}\" CASCADE") end - - subject end end @@ -422,13 +463,11 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do context 'with multiple databases', :reestablished_active_record_base do before do - allow(ActiveRecord::Tasks::DatabaseTasks).to receive(:setup_initial_database_yaml).and_return([:main, :geo]) + skip_if_multiple_databases_not_setup end describe 'db:structure:dump' do it 'invokes gitlab:db:clean_structure_sql' do - skip unless Gitlab.ee? - expect(Rake::Task['gitlab:db:clean_structure_sql']).to receive(:invoke).twice.and_return(true) expect { run_rake_task('db:structure:dump:main') }.not_to raise_error @@ -437,13 +476,33 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do describe 'db:schema:dump' do it 'invokes gitlab:db:clean_structure_sql' do - skip unless Gitlab.ee? - expect(Rake::Task['gitlab:db:clean_structure_sql']).to receive(:invoke).once.and_return(true) expect { run_rake_task('db:schema:dump:main') }.not_to raise_error end end + + describe 'db:migrate' do + it 'invokes gitlab:db:create_dynamic_partitions' do + expect(Rake::Task['gitlab:db:create_dynamic_partitions']).to receive(:invoke).once.and_return(true) + + expect { run_rake_task('db:migrate:main') }.not_to raise_error + end + end + + describe 'db:migrate:geo' do + it 'does not invoke gitlab:db:create_dynamic_partitions' do + skip 'Skipping because geo database is not setup' unless geo_configured? + + expect(Rake::Task['gitlab:db:create_dynamic_partitions']).not_to receive(:invoke) + + expect { run_rake_task('db:migrate:geo') }.not_to raise_error + end + + def geo_configured? + !!ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: 'geo') + end + end end describe 'gitlab:db:reset_as_non_superuser' do |