# frozen_string_literal: true

class CreateVulnerabilityOccurrences < ActiveRecord::Migration[4.2]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  def change
    create_table :vulnerability_occurrences, id: :bigserial do |t|
      t.timestamps_with_timezone null: false

      t.integer :severity, null: false, limit: 2
      t.integer :confidence, null: false, limit: 2
      t.integer :report_type, null: false, limit: 2

      t.integer :pipeline_id, null: false
      t.foreign_key :ci_pipelines, column: :pipeline_id, on_delete: :cascade
      t.references :project, null: false, foreign_key: { on_delete: :cascade }

      t.bigint :scanner_id, null: false
      t.foreign_key :vulnerability_scanners, column: :scanner_id, on_delete: :cascade

      t.binary :first_seen_in_commit_sha, null: false, limit: 20
      t.binary :project_fingerprint, null: false, limit: 20
      t.binary :location_fingerprint, null: false, limit: 20
      t.binary :primary_identifier_fingerprint, null: false, limit: 20

      t.string :ref, null: false
      t.string :name, null: false
      t.string :metadata_version, null: false
      t.text :raw_metadata, null: false

      t.index :pipeline_id
      t.index :scanner_id
      t.index [:project_id, :ref, :scanner_id, :primary_identifier_fingerprint, :location_fingerprint],
        unique: true,
        name: 'index_vulnerability_occurrences_on_unique_keys',
        length: { location_fingerprint: 20, primary_identifier_fingerprint: 20 }
    end
  end
end
