4.2 KiB · text History 0ac4ba6
defmodule Vigil.Repo.Migrations.CreateAlerts do
  use Ecto.Migration

  # Slice 4 (ROADMAP.md): alert rules, their recipients, and what was sent.
  def change do
    create table(:alert_rules, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :project_id, references(:projects, type: :binary_id, on_delete: :delete_all),
        null: false

      add :name, :string, null: false
      # frequency (quantity events in timespan_minutes) | new_issue | regression
      add :kind, :string, null: false
      add :quantity, :integer
      add :timespan_minutes, :integer
      # Tell again about the same issue after this long; nil: once, until it
      # regresses (GlitchTip's only behaviour).
      add :renotify_minutes, :integer
      add :enabled, :boolean, null: false, default: true

      timestamps(type: :utc_datetime)
    end

    create index(:alert_rules, [:project_id])

    create table(:alert_recipients, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :alert_rule_id, references(:alert_rules, type: :binary_id, on_delete: :delete_all),
        null: false

      # email | webhook | discord | googlechat | ntfy | teams | zulip
      add :type, :string, null: false
      add :url, :string
      add :config, :map, null: false, default: %{}
      add :tags_to_add, {:array, :string}, null: false, default: []

      timestamps(type: :utc_datetime)
    end

    create index(:alert_recipients, [:alert_rule_id])

    # Issues an event touched since the last evaluation: at most one row
    # per issue and kind (event | new | regression).
    create table(:alert_candidates, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :project_id, :binary_id, null: false
      add :issue_id, references(:issues, type: :binary_id, on_delete: :delete_all), null: false
      add :kind, :string, null: false
      timestamps(type: :utc_datetime, updated_at: false)
    end

    create unique_index(:alert_candidates, [:issue_id, :kind])

    # When a rule last told about an issue (its dedupe); cleared when the
    # issue regresses.
    create table(:alert_issue_states, primary_key: false) do
      add :alert_rule_id, references(:alert_rules, type: :binary_id, on_delete: :delete_all),
        primary_key: true

      add :issue_id, references(:issues, type: :binary_id, on_delete: :delete_all),
        primary_key: true

      add :last_notified_at, :utc_datetime, null: false
    end

    create index(:alert_issue_states, [:issue_id])

    create table(:alert_notifications, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :alert_rule_id, references(:alert_rules, type: :binary_id, on_delete: :delete_all),
        null: false

      add :project_id, :binary_id, null: false
      add :issue_ids, {:array, :binary_id}, null: false, default: []
      timestamps(type: :utc_datetime, updated_at: false)
    end

    create index(:alert_notifications, [:project_id, :id])

    # One per recipient per notification: its fate, for the alerts page.
    create table(:alert_deliveries, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :notification_id,
          references(:alert_notifications, type: :binary_id, on_delete: :delete_all),
          null: false

      add :recipient_id, references(:alert_recipients, type: :binary_id, on_delete: :nilify_all)
      add :project_id, :binary_id, null: false
      add :type, :string, null: false
      add :target, :string
      # pending | sent | failed
      add :state, :string, null: false, default: "pending"
      add :attempts, :integer, null: false, default: 0
      add :error, :text
      add :delivered_at, :utc_datetime

      timestamps(type: :utc_datetime)
    end

    create index(:alert_deliveries, [:project_id, :id])

    # A user's email alerts for a project: on unless turned off here.
    create table(:user_project_alerts, primary_key: false) do
      add :user_id, references(:users, type: :binary_id, on_delete: :delete_all),
        primary_key: true

      add :project_id, references(:projects, type: :binary_id, on_delete: :delete_all),
        primary_key: true

      add :enabled, :boolean, null: false
    end
  end
end