diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-12-13 18:08:31 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-12-13 18:08:31 +0300 |
commit | 57ed4c594d37326f1d27752df575b581c522ab05 (patch) | |
tree | 455227f40aaf31c6977fb51839770753df824e72 /spec/tasks/gitlab | |
parent | 8ebd99a81f8fe458153de9815f73f9067d010293 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/tasks/gitlab')
-rw-r--r-- | spec/tasks/gitlab/click_house/migration_rake_spec.rb | 115 | ||||
-rw-r--r-- | spec/tasks/gitlab/db_rake_spec.rb | 38 |
2 files changed, 102 insertions, 51 deletions
diff --git a/spec/tasks/gitlab/click_house/migration_rake_spec.rb b/spec/tasks/gitlab/click_house/migration_rake_spec.rb index 8ef1fe508f2..37ae9d7f090 100644 --- a/spec/tasks/gitlab/click_house/migration_rake_spec.rb +++ b/spec/tasks/gitlab/click_house/migration_rake_spec.rb @@ -8,53 +8,73 @@ RSpec.describe 'gitlab:clickhouse', click_house: :without_migrations, feature_ca # We don't need to delete data since we don't modify Postgres data self.use_transactional_tests = false - let(:verbose) { nil } - let(:target_version) { nil } - let(:step) { nil } - before(:all) do Rake.application.rake_require 'tasks/gitlab/click_house/migration' end - before do - stub_env('VERBOSE', verbose) if verbose - stub_env('VERSION', target_version) if target_version - stub_env('STEP', step.to_s) if step - end + it 'migrates and rolls back the database' do + expect { run_rake_task('gitlab:clickhouse:migrate:main') }.to change { active_schema_migrations_count }.from(0) + .and output.to_stdout - context 'with real migrations' do - let(:migrations_dir) { File.expand_path(rails_root_join('db', 'click_house', 'migrate')) } + expect { run_rake_task('gitlab:clickhouse:rollback:main') }.to change { active_schema_migrations_count }.by(-1) + .and output.to_stdout + stub_env('VERSION', 0) + expect { run_rake_task('gitlab:clickhouse:rollback:main') }.to change { active_schema_migrations_count }.to(0) + .and output.to_stdout + end + + context 'when clickhouse database is not configured' do before do - ClickHouse::MigrationSupport::Migrator.migrations_paths = [migrations_dir] + allow(::ClickHouse::Client).to receive(:configuration).and_return(::ClickHouse::Client::Configuration.new) end - it 'runs migrations and rollbacks' do - expect { run_rake_task('gitlab:clickhouse:migrate') }.to change { active_schema_migrations_count }.from(0) - .and output.to_stdout + it 'raises an error' do + expect { run_rake_task('gitlab:clickhouse:migrate:main') }.to raise_error(ClickHouse::Client::ConfigurationError) + end - expect { run_rake_task('gitlab:clickhouse:rollback') }.to change { active_schema_migrations_count }.by(-1) - .and output.to_stdout + it 'prints the error message and exits successfully if skip_unless_configured is passed' do + expect do + run_rake_task('gitlab:clickhouse:migrate:main', true) + end.to output(/The 'main' ClickHouse database is not configured, skipping migrations/).to_stdout + end + end - stub_env('VERSION', 0) - expect { run_rake_task('gitlab:clickhouse:rollback') }.to change { active_schema_migrations_count }.to(0) + describe 'gitlab:clickhouse:migrate' do + it 'delegates to gitlab:clickhouse:migrate:main' do + task = Rake::Task['gitlab:clickhouse:migrate:main'] + task.reenable # re-enabling task in case other tests already run it + expect(task).to receive(:invoke).with("true").and_call_original + + expect do + run_rake_task('gitlab:clickhouse:migrate', true) + end.to change { active_schema_migrations_count }.from(0).and output.to_stdout end end - context 'with migration fixtures' do + context 'with migration fixtures', :silence_stdout do let(:migrations_base_dir) { 'click_house/migrations' } let(:migrations_dirname) { 'undefined' } let(:migrations_dir) { expand_fixture_path("#{migrations_base_dir}/#{migrations_dirname}") } - describe 'migrate' do - subject(:migration) { run_rake_task('gitlab:clickhouse:migrate') } + describe 'migrate:main' do + subject(:migration) { run_rake_task('gitlab:clickhouse:migrate:main') } + + let(:verbose) { nil } + let(:target_version) { nil } + let(:step) { nil } - around do |example| - ClickHouse::MigrationSupport::Migrator.migrations_paths = [migrations_dir] + before do + allow(ClickHouse::MigrationSupport::Migrator).to receive(:migrations_paths).with(:main) + .and_return(migrations_dir) - example.run + stub_env('VERBOSE', verbose) if verbose + stub_env('VERSION', target_version) if target_version + stub_env('STEP', step.to_s) if step + end - clear_consts(expand_fixture_path(migrations_base_dir)) + after do + unload_click_house_migration_classes(expand_fixture_path(migrations_dir)) end describe 'when creating a table' do @@ -92,7 +112,7 @@ RSpec.describe 'gitlab:clickhouse', click_house: :without_migrations, feature_ca it 'drops table' do stub_env('VERSION', 1) - run_rake_task('gitlab:clickhouse:migrate') + run_rake_task('gitlab:clickhouse:migrate:main') expect(table_names).to include('some') @@ -155,20 +175,25 @@ RSpec.describe 'gitlab:clickhouse', click_house: :without_migrations, feature_ca end end - describe 'rollback' do - subject(:migration) { run_rake_task('gitlab:clickhouse:rollback') } + describe 'rollback:main' do + subject(:migration) { run_rake_task('gitlab:clickhouse:rollback:main') } + let(:target_version) { nil } + let(:rollback_step) { nil } let(:migrations_dirname) { 'table_creation_with_down_method' } - around do |example| - ClickHouse::MigrationSupport::Migrator.migrations_paths = [migrations_dir] - # Ensure we start with all migrations up - schema_migration = ClickHouse::MigrationSupport::SchemaMigration - migrate(ClickHouse::MigrationSupport::MigrationContext.new(migrations_dir, schema_migration), nil) + before do + allow(ClickHouse::MigrationSupport::Migrator).to receive(:migrations_paths).with(:main) + .and_return(migrations_dir) + + run_rake_task('gitlab:clickhouse:migrate:main') - example.run + stub_env('VERSION', target_version) if target_version + stub_env('STEP', rollback_step.to_s) if rollback_step + end - clear_consts(expand_fixture_path(migrations_base_dir)) + after do + unload_click_house_migration_classes(expand_fixture_path(migrations_dir)) end context 'with VERSION set' do @@ -183,7 +208,7 @@ RSpec.describe 'gitlab:clickhouse', click_house: :without_migrations, feature_ca end context 'with STEP also set' do - let(:step) { 1 } + let(:rollback_step) { 1 } it 'ignores STEP and rolls back all migrations' do expect(table_names).to include('some', 'another') @@ -196,10 +221,10 @@ RSpec.describe 'gitlab:clickhouse', click_house: :without_migrations, feature_ca end context 'with STEP set to 1' do - let(:step) { 1 } + let(:rollback_step) { 1 } it 'executes only first step and drops "another" table' do - run_rake_task('gitlab:clickhouse:rollback') + run_rake_task('gitlab:clickhouse:rollback:main') expect(table_names).to include('some') expect(table_names).not_to include('another') @@ -207,16 +232,4 @@ RSpec.describe 'gitlab:clickhouse', click_house: :without_migrations, feature_ca end end end - - %w[gitlab:clickhouse:migrate].each do |task| - context "when running #{task}" do - it "does run gitlab:clickhouse:prepare_schema_migration_table before" do - expect(Rake::Task['gitlab:clickhouse:prepare_schema_migration_table']).to receive(:execute).and_return(true) - expect(Rake::Task[task]).to receive(:execute).and_return(true) - - Rake::Task['gitlab:clickhouse:prepare_schema_migration_table'].reenable - run_rake_task(task) - end - end - end end diff --git a/spec/tasks/gitlab/db_rake_spec.rb b/spec/tasks/gitlab/db_rake_spec.rb index 39aca404480..d7bcb501425 100644 --- a/spec/tasks/gitlab/db_rake_spec.rb +++ b/spec/tasks/gitlab/db_rake_spec.rb @@ -6,6 +6,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout, feature_categor before(:all) do Rake.application.rake_require 'active_record/railties/databases' Rake.application.rake_require 'tasks/seed_fu' + Rake.application.rake_require 'tasks/gitlab/click_house/migration' Rake.application.rake_require 'tasks/gitlab/db' Rake.application.rake_require 'tasks/gitlab/db/lock_writes' end @@ -357,6 +358,43 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout, feature_categor end end end + + describe 'clickhouse migrations' do + let(:connection) { Gitlab::Database.database_base_models[:main].connection } + let(:main_config) { double(:config, name: 'main') } + + before do + # stub normal migrations + allow(ActiveRecord::Base).to receive_message_chain('configurations.configs_for').and_return([main_config]) + allow(connection).to receive(:tables).and_return(%w[table1 table2]) + allow(Rake::Task['db:migrate']).to receive(:invoke) + end + + it 'migrates clickhouse database' do + expect(Rake::Task['gitlab:clickhouse:migrate']).to receive(:invoke).with(true) + + run_rake_task('gitlab:db:configure') + end + + it 'does not run clickhouse migrations if feature flag is disabled' do + stub_feature_flags(run_clickhouse_migrations_automatically: false) + + expect(Rake::Task['gitlab:clickhouse:migrate']).not_to receive(:invoke) + + run_rake_task('gitlab:db:configure') + end + + it 'does not fail if clickhouse is not configured' do + allow(::ClickHouse::Client).to receive(:configuration).and_return(::ClickHouse::Client::Configuration.new) + + Rake::Task['gitlab:clickhouse:migrate'].reenable + Rake::Task['gitlab:clickhouse:migrate:main'].reenable + + expect do + run_rake_task('gitlab:db:configure') + end.to output(/The 'main' ClickHouse database is not configured, skipping migrations/).to_stdout + end + end end describe 'schema inconsistencies' do |