summaryrefslogtreecommitdiff
path: root/db/migrate/20221209174157_truncate_sbom_occurrences.rb
blob: e9db6526e2ddbba6528e72c58400fd0810234400 (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
# frozen_string_literal: true

class TruncateSbomOccurrences < Gitlab::Database::Migration[2.1]
  def up
    # Because existing data in the table violates the new
    # uniqueness constraints, we need to remove the non-distinct rows.
    # Rather than do an expensive and error-prone batch migration
    # to find and remove the duplicates, we'll just remove all records
    # from the table.
    #
    # The `cyclonedx_sbom_ingestion` feature flag should
    # be OFF in all environments to avoid having more duplicate records
    # added between this migration and the one where the new unqiue index
    # is added.

    # TRUNCATE is a DDL statement (it drops the table and re-creates it), so we want to run the
    # migration in DDL mode, but we also don't want to execute it against all schemas because
    # it's considered a write operation. So, we'll manually check and skip the migration if
    # it's on not `:gitlab_main`.
    return unless Gitlab::Database.gitlab_schemas_for_connection(connection).include?(:gitlab_main)

    execute('TRUNCATE sbom_occurrences')
  end

  def down
    # no-op
  end
end