diff options
Diffstat (limited to 'spec/lib/gitlab/background_migration/nullify_orphan_runner_id_on_ci_builds_spec.rb')
-rw-r--r-- | spec/lib/gitlab/background_migration/nullify_orphan_runner_id_on_ci_builds_spec.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/spec/lib/gitlab/background_migration/nullify_orphan_runner_id_on_ci_builds_spec.rb b/spec/lib/gitlab/background_migration/nullify_orphan_runner_id_on_ci_builds_spec.rb new file mode 100644 index 00000000000..90dd3e14606 --- /dev/null +++ b/spec/lib/gitlab/background_migration/nullify_orphan_runner_id_on_ci_builds_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::NullifyOrphanRunnerIdOnCiBuilds, :migration, schema: 20220223112304 do + let(:namespaces) { table(:namespaces) } + let(:projects) { table(:projects) } + let(:ci_runners) { table(:ci_runners) } + let(:ci_pipelines) { table(:ci_pipelines) } + let(:ci_builds) { table(:ci_builds) } + + subject { described_class.new } + + let(:helpers) do + ActiveRecord::Migration.new.extend(Gitlab::Database::MigrationHelpers) + end + + before do + helpers.remove_foreign_key_if_exists(:ci_builds, column: :runner_id) + end + + after do + helpers.add_concurrent_foreign_key(:ci_builds, :ci_runners, column: :runner_id, on_delete: :nullify, validate: false) + end + + describe '#perform' do + let(:namespace) { namespaces.create!(name: 'test', path: 'test', type: 'Group') } + let(:project) { projects.create!(namespace_id: namespace.id, name: 'test') } + let(:pipeline) { ci_pipelines.create!(project_id: project.id, ref: 'master', sha: 'adf43c3a', status: 'success') } + + it 'nullifies runner_id for orphan ci_builds in range' do + ci_runners.create!(id: 2, runner_type: 'project_type') + + ci_builds.create!(id: 5, type: 'Ci::Build', commit_id: pipeline.id, runner_id: 2) + ci_builds.create!(id: 7, type: 'Ci::Build', commit_id: pipeline.id, runner_id: 4) + ci_builds.create!(id: 8, type: 'Ci::Build', commit_id: pipeline.id, runner_id: 5) + ci_builds.create!(id: 9, type: 'Ci::Build', commit_id: pipeline.id, runner_id: 6) + + subject.perform(4, 8, :ci_builds, :id, 10, 0) + + expect(ci_builds.all).to contain_exactly( + an_object_having_attributes(id: 5, runner_id: 2), + an_object_having_attributes(id: 7, runner_id: nil), + an_object_having_attributes(id: 8, runner_id: nil), + an_object_having_attributes(id: 9, runner_id: 6) + ) + end + end +end |