summaryrefslogtreecommitdiff
path: root/db/post_migrate/20220919080303_delete_migrate_shared_vulnerability_scanners.rb
blob: 4aedfcf16994ebf85a9fbb80fa56616f1ac2c97b (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
# frozen_string_literal: true

class DeleteMigrateSharedVulnerabilityScanners < Gitlab::Database::Migration[2.0]
  disable_ddl_transaction!
  restrict_gitlab_migration gitlab_schema: :gitlab_main

  MIGRATION = "MigrateSharedVulnerabilityScanners"
  TABLE_NAME = :vulnerability_occurrences
  BATCH_COLUMN = :id
  BATCH_SIZE = 250

  class BatchedBackgroundMigration < MigrationRecord
    self.table_name = "batched_background_migrations"
  end

  class BatchedBackgroundMigrationJob < MigrationRecord
    include ::EachBatch

    self.table_name = "batched_background_migration_jobs"

    belongs_to :batched_background_migration
  end

  def up
    return unless migration_id = BatchedBackgroundMigration.find_by(job_class_name: MIGRATION)&.id

    # rubocop:disable Style/SymbolProc
    BatchedBackgroundMigrationJob
      .where(batched_background_migration_id: migration_id)
      .each_batch(of: BATCH_SIZE) do |relation|
      relation.delete_all
    end
    # rubocop:enable Style/SymbolProc

    delete_batched_background_migration(MIGRATION,
                                        TABLE_NAME,
                                        BATCH_COLUMN,
                                        [])
  end

  def down
    # no-op
  end
end