8.3 KiB · text History bbb2a48
defmodule Vigil.Repo.Migrations.CreateIssuesAndEvents do
  use Ecto.Migration

  # Slice 2 (ROADMAP.md): events grouped into issues.
  #
  # issue_events is range-partitioned by its UUIDv7 id, a partition a day;
  # issue_tags, issue_counts and project_counts by week. Vigil.Partitions
  # makes the partitions (at boot, daily, and in test_helper.exs) and drops
  # expired ones. There are no DEFAULT partitions.
  def up do
    execute "CREATE EXTENSION IF NOT EXISTS pg_trgm"

    # How a project groups: its strategy, and while switching, the old one
    # too until the expiry (events are hashed under both, so issues don't
    # split).
    alter table(:projects) do
      add :grouping_config, :string, null: false, default: "newstyle:2019-10-29"
      add :secondary_grouping_config, :string
      add :secondary_grouping_expiry, :utc_datetime
    end

    # Short ids: PROJ-1, PROJ-2, ... per project.
    create table(:project_counters, primary_key: false) do
      add :project_id, references(:projects, type: :binary_id, on_delete: :delete_all),
        primary_key: true

      add :value, :bigint, null: false, default: 0
    end

    create table(:releases, 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 :version, :string, null: false
      add :ref, :string
      add :url, :string
      add :date_released, :utc_datetime
      add :data, :map, null: false, default: %{}
      add :commit_count, :integer, null: false, default: 0
      add :deploy_count, :integer, null: false, default: 0

      timestamps(type: :utc_datetime)
    end

    create unique_index(:releases, [:organization_id, :version])

    create table(:release_projects, primary_key: false) do
      add :release_id, references(:releases, 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
    end

    create index(:release_projects, [:project_id])

    create table(:environments, 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
      timestamps(type: :utc_datetime, updated_at: false)
    end

    create unique_index(:environments, [:organization_id, :name])

    create table(:environment_projects, primary_key: false) do
      add :environment_id, references(:environments, 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 :hidden, :boolean, null: false, default: false
    end

    create table(:issues, 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 :short_id, :bigint, null: false
      # error | default | csp
      add :type, :string, null: false
      add :title, :string, null: false
      add :culprit, :string
      add :metadata, :map, null: false, default: %{}
      add :level, :string, null: false, default: "error"
      add :platform, :string
      # unresolved | resolved | ignored
      add :status, :string, null: false, default: "unresolved"
      add :resolved_at, :utc_datetime
      add :resolved_in_release_id, references(:releases, type: :binary_id, on_delete: :nilify_all)
      add :first_release_id, references(:releases, type: :binary_id, on_delete: :nilify_all)
      add :last_release_id, references(:releases, type: :binary_id, on_delete: :nilify_all)
      add :first_seen, :utc_datetime_usec, null: false
      add :last_seen, :utc_datetime_usec, null: false
      add :count, :bigint, null: false, default: 0
      add :is_public, :boolean, null: false, default: false
      add :search_vector, :tsvector
      add :deleted_at, :utc_datetime

      timestamps(type: :utc_datetime)
    end

    create unique_index(:issues, [:project_id, :short_id])
    create index(:issues, [:project_id, :status, :last_seen])
    create index(:issues, [:organization_id, :last_seen])
    execute "CREATE INDEX issues_search_vector_index ON issues USING gin (search_vector)"
    execute "CREATE INDEX issues_title_trgm_index ON issues USING gin (title gin_trgm_ops)"

    # What groups events into an issue: one row per (strategy, hash).
    create table(:issue_hashes, 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 :issue_id, references(:issues, type: :binary_id, on_delete: :delete_all), null: false
      add :config, :string, null: false
      add :hash, :string, null: false

      timestamps(type: :utc_datetime, updated_at: false)
    end

    create unique_index(:issue_hashes, [:project_id, :config, :hash])
    create index(:issue_hashes, [:issue_id])

    execute """
    CREATE TABLE issue_events (
      id uuid NOT NULL,
      event_id varchar(32) NOT NULL,
      project_id uuid NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
      organization_id uuid NOT NULL,
      issue_id uuid NOT NULL REFERENCES issues (id) ON DELETE CASCADE,
      type varchar(16) NOT NULL,
      level varchar(16) NOT NULL,
      title varchar(255) NOT NULL,
      culprit varchar(255),
      platform varchar(64),
      release_id uuid,
      environment varchar(255),
      timestamp timestamp NOT NULL,
      received_at timestamp NOT NULL,
      grouping_config varchar(64),
      hashes text[] NOT NULL DEFAULT '{}',
      tags jsonb NOT NULL DEFAULT '{}',
      data jsonb NOT NULL DEFAULT '{}',
      PRIMARY KEY (id)
    ) PARTITION BY RANGE (id)
    """

    execute "CREATE INDEX issue_events_issue_id_id_index ON issue_events (issue_id, id DESC)"

    execute "CREATE INDEX issue_events_project_event_id_index ON issue_events (project_id, event_id)"

    # Tag counts per issue per day.
    execute """
    CREATE TABLE issue_tags (
      issue_id uuid NOT NULL REFERENCES issues (id) ON DELETE CASCADE,
      organization_id uuid NOT NULL,
      date date NOT NULL,
      key varchar(200) NOT NULL,
      value varchar(200) NOT NULL,
      count bigint NOT NULL DEFAULT 0,
      PRIMARY KEY (issue_id, date, key, value)
    ) PARTITION BY RANGE (date)
    """

    # Events per issue, and per project, per hour.
    execute """
    CREATE TABLE issue_counts (
      issue_id uuid NOT NULL REFERENCES issues (id) ON DELETE CASCADE,
      hour timestamp NOT NULL,
      count bigint NOT NULL DEFAULT 0,
      PRIMARY KEY (issue_id, hour)
    ) PARTITION BY RANGE (hour)
    """

    execute """
    CREATE TABLE project_counts (
      project_id uuid NOT NULL REFERENCES projects (id) ON DELETE CASCADE,
      organization_id uuid NOT NULL,
      hour timestamp NOT NULL,
      count bigint NOT NULL DEFAULT 0,
      PRIMARY KEY (project_id, hour)
    ) PARTITION BY RANGE (hour)
    """

    # User feedback (user_report / feedback items), linked to its event's
    # issue when that is known.
    create table(:user_reports, 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 :issue_id, references(:issues, type: :binary_id, on_delete: :nilify_all)
      add :event_id, :string, null: false
      add :name, :string
      add :email, :string
      add :comments, :text, null: false

      timestamps(type: :utc_datetime)
    end

    create unique_index(:user_reports, [:project_id, :event_id])
    create index(:user_reports, [:issue_id])
  end

  def down do
    drop table(:user_reports)
    execute "DROP TABLE project_counts"
    execute "DROP TABLE issue_counts"
    execute "DROP TABLE issue_tags"
    execute "DROP TABLE issue_events"
    drop table(:issue_hashes)
    drop table(:issues)
    drop table(:environment_projects)
    drop table(:environments)
    drop table(:release_projects)
    drop table(:releases)
    drop table(:project_counters)

    alter table(:projects) do
      remove :grouping_config
      remove :secondary_grouping_config
      remove :secondary_grouping_expiry
    end
  end
end