diff options
Diffstat (limited to 'spec/lib/gitlab/database/background_migration')
-rw-r--r-- | spec/lib/gitlab/database/background_migration/batched_job_spec.rb | 18 | ||||
-rw-r--r-- | spec/lib/gitlab/database/background_migration/batched_migration_spec.rb | 96 |
2 files changed, 112 insertions, 2 deletions
diff --git a/spec/lib/gitlab/database/background_migration/batched_job_spec.rb b/spec/lib/gitlab/database/background_migration/batched_job_spec.rb index 32746a46308..cc9f3d5b7f1 100644 --- a/spec/lib/gitlab/database/background_migration/batched_job_spec.rb +++ b/spec/lib/gitlab/database/background_migration/batched_job_spec.rb @@ -7,7 +7,15 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedJob, type: :model d it { is_expected.to be_a Gitlab::Database::SharedModel } - it { expect(described_class::TIMEOUT_EXCEPTIONS).to match_array [ActiveRecord::StatementTimeout, ActiveRecord::ConnectionTimeoutError, ActiveRecord::AdapterTimeout, ActiveRecord::LockWaitTimeout] } + specify do + expect(described_class::TIMEOUT_EXCEPTIONS).to contain_exactly( + ActiveRecord::StatementTimeout, + ActiveRecord::ConnectionTimeoutError, + ActiveRecord::AdapterTimeout, + ActiveRecord::LockWaitTimeout, + ActiveRecord::QueryCanceled + ) + end describe 'associations' do it { is_expected.to belong_to(:batched_migration).with_foreign_key(:batched_background_migration_id) } @@ -272,7 +280,13 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedJob, type: :model d context 'when is a timeout exception' do let(:exception) { ActiveRecord::StatementTimeout.new } - it { expect(subject).to be_truthy } + it { expect(subject).to be_truthy } + end + + context 'when is a QueryCanceled exception' do + let(:exception) { ActiveRecord::QueryCanceled.new } + + it { expect(subject).to be_truthy } end context 'when is not a timeout exception' do diff --git a/spec/lib/gitlab/database/background_migration/batched_migration_spec.rb b/spec/lib/gitlab/database/background_migration/batched_migration_spec.rb index 1ac9cbae036..31ae5e9b55d 100644 --- a/spec/lib/gitlab/database/background_migration/batched_migration_spec.rb +++ b/spec/lib/gitlab/database/background_migration/batched_migration_spec.rb @@ -211,6 +211,102 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigration, type: :m expect(active_migration).to eq(migration3) end end + + context 'when there are no active migrations available' do + it 'returns nil' do + expect(active_migration).to eq(nil) + end + end + end + + describe '.find_executable' do + let(:connection) { Gitlab::Database.database_base_models[:main].connection } + let(:migration_id) { migration.id } + + subject(:executable_migration) { described_class.find_executable(migration_id, connection: connection) } + + around do |example| + Gitlab::Database::SharedModel.using_connection(connection) do + example.run + end + end + + context 'when the migration does not exist' do + let(:migration_id) { non_existing_record_id } + + it 'returns nil' do + expect(executable_migration).to be_nil + end + end + + context 'when the migration is not active' do + let!(:migration) { create(:batched_background_migration, :finished) } + + it 'returns nil' do + expect(executable_migration).to be_nil + end + end + + context 'when the migration is on hold' do + let!(:migration) { create(:batched_background_migration, :active, on_hold_until: 10.minutes.from_now) } + + it 'returns nil' do + expect(executable_migration).to be_nil + end + end + + context 'when the migration is not available for the current connection' do + let!(:migration) { create(:batched_background_migration, :active, gitlab_schema: :gitlab_not_existing) } + + it 'returns nil' do + expect(executable_migration).to be_nil + end + end + + context 'when ther migration exists and is executable' do + let!(:migration) { create(:batched_background_migration, :active, gitlab_schema: :gitlab_main) } + + it 'returns the migration' do + expect(executable_migration).to eq(migration) + end + end + end + + describe '.active_migrations_distinct_on_table' do + let(:connection) { Gitlab::Database.database_base_models[:main].connection } + + around do |example| + Gitlab::Database::SharedModel.using_connection(connection) do + example.run + end + end + + it 'returns one pending executable migration per table' do + # non-active migration + create(:batched_background_migration, :finished) + # migration put on hold + create(:batched_background_migration, :active, on_hold_until: 10.minutes.from_now) + # migration not availab for the current connection + create(:batched_background_migration, :active, gitlab_schema: :gitlab_not_existing) + # active migration that is no longer on hold + migration_1 = create(:batched_background_migration, :active, table_name: :users, on_hold_until: 10.minutes.ago) + # another active migration for the same table + create(:batched_background_migration, :active, table_name: :users) + # active migration for different table + migration_2 = create(:batched_background_migration, :active, table_name: :projects) + # active migration for third table + create(:batched_background_migration, :active, table_name: :namespaces) + + actual = described_class.active_migrations_distinct_on_table(connection: connection, limit: 2) + + expect(actual).to eq([migration_1, migration_2]) + end + + it 'returns epmty collection when there are no pending executable migrations' do + actual = described_class.active_migrations_distinct_on_table(connection: connection, limit: 2) + + expect(actual).to be_empty + end end describe '.created_after' do |