defmodule Tarakan.Repo.Migrations.AddReproductionCountersToCanonicalFindings do
  use Ecto.Migration

  def change do
    alter table(:canonical_findings) do
      # Denormalised the same way confirmations_count is, so findings backed by a
      # working reproduction can be ranked and filtered without joining checks.
      add :reproductions_count, :integer, null: false, default: 0
      add :reproduced_at, :utc_datetime_usec
    end

    create constraint(:canonical_findings, :canonical_findings_reproductions_non_negative,
             check: "reproductions_count >= 0"
           )

    create index(:canonical_findings, [:reproduced_at])
  end
end
