3.3 KiB · text History bbb2a48
defmodule Vigil.Repo.Migrations.CreateUptime do
  use Ecto.Migration

  # Slice 8 (ROADMAP.md): uptime monitors, their checks (partitioned by
  # UUIDv7 id, a partition a day), and status pages.
  def up do
    create table(:uptime_monitors, 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 :organization_id, :binary_id, null: false
      add :name, :string, null: false
      # ping (HTTP HEAD) | get | post | tcp | ssl | heartbeat
      add :type, :string, null: false
      add :url, :string, size: 2000
      add :expected_status, :integer
      add :expected_body, :string, size: 2000
      add :interval_seconds, :integer, null: false, default: 60
      add :timeout_seconds, :integer, null: false, default: 20
      # ssl: down when the certificate expires within this many days.
      add :ssl_warn_days, :integer, null: false, default: 14
      add :enabled, :boolean, null: false, default: true
      # heartbeat: the secret in its URL.
      add :heartbeat_token, :uuid

      add :is_up, :boolean
      add :reason, :string
      add :last_change_at, :utc_datetime_usec
      add :last_check_at, :utc_datetime_usec
      add :last_heartbeat_at, :utc_datetime_usec
      add :response_ms, :integer
      add :ssl_expires_at, :utc_datetime

      timestamps(type: :utc_datetime)
    end

    create index(:uptime_monitors, [:organization_id])
    create index(:uptime_monitors, [:project_id])
    create unique_index(:uptime_monitors, [:heartbeat_token])

    execute """
    CREATE TABLE uptime_checks (
      id uuid NOT NULL,
      monitor_id uuid NOT NULL REFERENCES uptime_monitors (id) ON DELETE CASCADE,
      organization_id uuid NOT NULL,
      is_up boolean NOT NULL,
      is_change boolean NOT NULL DEFAULT false,
      reason varchar(32),
      status_code integer,
      response_ms integer,
      checked_at timestamp(6) NOT NULL,
      data jsonb,
      PRIMARY KEY (id)
    ) PARTITION BY RANGE (id)
    """

    execute "CREATE INDEX uptime_checks_monitor_id_index ON uptime_checks (monitor_id, id DESC)"

    execute """
    CREATE INDEX uptime_checks_monitor_changes_index ON uptime_checks (monitor_id, id DESC)
    WHERE is_change
    """

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

      add :organization_id,
          references(:organizations, type: :binary_id, on_delete: :delete_all),
          null: false

      add :name, :string, null: false
      add :slug, :string, null: false
      add :is_public, :boolean, null: false, default: false

      timestamps(type: :utc_datetime)
    end

    create unique_index(:status_pages, [:organization_id, :slug])

    create table(:status_page_monitors, primary_key: false) do
      add :status_page_id,
          references(:status_pages, type: :binary_id, on_delete: :delete_all),
          primary_key: true

      add :uptime_monitor_id,
          references(:uptime_monitors, type: :binary_id, on_delete: :delete_all),
          primary_key: true

      add :position, :integer, null: false, default: 0
    end
  end

  def down do
    drop table(:status_page_monitors)
    drop table(:status_pages)
    execute "DROP TABLE uptime_checks"
    drop table(:uptime_monitors)
  end
end