summaryrefslogtreecommitdiff
path: root/spec/migrations/delete_migrate_shared_vulnerability_scanners_spec.rb
blob: 259b175cd19099f97bdd0b8128a4260133214dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true

require "spec_helper"

require_migration!

RSpec.describe DeleteMigrateSharedVulnerabilityScanners, :migration do
  let(:batched_background_migrations) { table(:batched_background_migrations) }
  let(:batched_background_migration_jobs) { table(:batched_background_migration_jobs) }

  let(:migration) do
    batched_background_migrations.create!(created_at: Time.zone.now,
                                          updated_at: Time.zone.now,
                                          min_value: 1,
                                          max_value: 1,
                                          batch_size: described_class::BATCH_SIZE,
                                          sub_batch_size: 100,
                                          interval: 300,
                                          status: 3,
                                          job_class_name: described_class::MIGRATION,
                                          batch_class_name: "PrimaryKeyBatchingStrategy",
                                          table_name: described_class::TABLE_NAME,
                                          column_name: described_class::BATCH_COLUMN,
                                          job_arguments: [],
                                          pause_ms: 100,
                                          max_batch_size: 1000,
                                          gitlab_schema: "gitlab_main")
  end

  let(:jobs) do
    Array.new(10) do
      batched_background_migration_jobs.create!(batched_background_migration_id: migration.id,
                                                created_at: Time.zone.now,
                                                updated_at: Time.zone.now,
                                                min_value: 1,
                                                max_value: 1,
                                                batch_size: 1,
                                                sub_batch_size: 1,
                                                status: 0,
                                                attempts: 0,
                                                metrics: {},
                                                pause_ms: 100)
    end
  end

  describe "#up" do
    it "deletes jobs" do
      expect { migrate! }.to change(batched_background_migration_jobs, :count).from(jobs.count).to(0)
    end

    it "deletes the migration" do
      expect { migrate! }.to change { batched_background_migrations.find_by(id: migration.id) }.from(migration).to(nil)
    end

    context "when background migration does not exist" do
      before do
        migration.destroy!
      end

      it "does not delete jobs" do
        expect { migrate! }.not_to change(batched_background_migration_jobs, :count)
      end

      it "does not delete the migration" do
        expect { migrate! }.not_to change { batched_background_migrations.find_by(id: migration.id) }
      end
    end
  end
end