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

  def change do
    alter table(:review_tasks) do
      # For diff_review: the range is base_commit_sha..commit_sha. Null for every
      # other kind, which reviews the snapshot at commit_sha.
      add :base_commit_sha, :string
    end

    create constraint(:review_tasks, :review_tasks_base_commit_sha_format,
             check: "base_commit_sha IS NULL OR base_commit_sha ~ '^[0-9a-f]{40}$'"
           )

    create constraint(:review_tasks, :review_tasks_diff_review_needs_base,
             check: "kind <> 'diff_review' OR base_commit_sha IS NOT NULL"
           )
  end
end
