summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/background_migration/batched_migration_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/gitlab/database/background_migration/batched_migration_spec.rb')
-rw-r--r--spec/lib/gitlab/database/background_migration/batched_migration_spec.rb96
1 files changed, 96 insertions, 0 deletions
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