7.0 KiB · text History bbb2a48
defmodule Vigil.Repo.Migrations.CreateOrganizationsAndProjects do
  use Ecto.Migration

  # Slice 0 (ROADMAP.md): organizations, their members, teams, invites and
  # group rules; projects and their DSN keys; API tokens. GlitchTip's model,
  # with UUIDv7 ids (Vigil.Schema).
  def change do
    create table(:organizations, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :name, :string, null: false
      add :slug, :string, null: false
      add :accepting_events, :boolean, null: false, default: true
      add :scrub_ip, :boolean, null: false, default: true
      add :open_membership, :boolean, null: false, default: false
      # What group sync does to a group-made membership whose group is gone:
      # remove | demote | keep.
      add :group_removal, :string, null: false, default: "remove"
      # Whether group rules may raise (never lower) a manual member's role.
      add :group_rules_promote_manual, :boolean, null: false, default: false
      add :deleted_at, :utc_datetime

      timestamps(type: :utc_datetime)
    end

    create unique_index(:organizations, [:slug])

    create table(:org_members, 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 :user_id, references(:users, type: :binary_id, on_delete: :delete_all), null: false
      add :role, :string, null: false
      # manual: added by a person (or an invite); group: by group sync.
      add :source, :string, null: false, default: "manual"

      timestamps(type: :utc_datetime)
    end

    create unique_index(:org_members, [:organization_id, :user_id])
    create index(:org_members, [:user_id])

    create table(:teams, 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

      timestamps(type: :utc_datetime)
    end

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

    create table(:projects, 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 :platform, :string
      add :scrub_ip, :boolean, null: false, default: true
      add :first_event_at, :utc_datetime
      add :deleted_at, :utc_datetime

      timestamps(type: :utc_datetime)
    end

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

    create table(:team_members, primary_key: false) do
      add :team_id, references(:teams, type: :binary_id, on_delete: :delete_all),
        null: false,
        primary_key: true

      add :member_id, references(:org_members, type: :binary_id, on_delete: :delete_all),
        null: false,
        primary_key: true
    end

    create index(:team_members, [:member_id])

    create table(:team_projects, primary_key: false) do
      add :team_id, references(:teams, type: :binary_id, on_delete: :delete_all),
        null: false,
        primary_key: true

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

    create index(:team_projects, [:project_id])

    create table(:project_keys, 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 :label, :string, null: false, default: "Default"
      # The DSN's key: random (v4), never time-ordered, since it is the
      # ingest credential.
      add :public_key, :uuid, null: false
      add :active, :boolean, null: false, default: true
      # Events per window, enforced at ingest; nil count means unlimited.
      add :rate_limit_count, :integer
      add :rate_limit_window, :integer

      timestamps(type: :utc_datetime)
    end

    create unique_index(:project_keys, [:public_key])
    create index(:project_keys, [:project_id])

    create table(:org_invites, 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 :email, :string, null: false
      add :role, :string, null: false
      add :team_ids, {:array, :binary_id}, null: false, default: []
      add :token_hash, :binary, null: false
      add :invited_by_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      add :accepted_by_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      add :accepted_at, :utc_datetime
      add :revoked_at, :utc_datetime
      add :expires_at, :utc_datetime, null: false

      timestamps(type: :utc_datetime)
    end

    create unique_index(:org_invites, [:token_hash])
    create index(:org_invites, [:organization_id])
    create index(:org_invites, [:email])

    create table(:org_group_rules, 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

      # A neiam_auth provider id, or nil for any provider.
      add :provider, :string
      add :group, :string, null: false
      add :role, :string, null: false
      add :team_ids, {:array, :binary_id}, null: false, default: []
      # Declared in config (applied at boot) rather than made in the UI.
      add :declared, :boolean, null: false, default: false

      timestamps(type: :utc_datetime)
    end

    create unique_index(:org_group_rules, [:organization_id, :provider, :group],
             nulls_distinct: false
           )

    create table(:org_audit_events, 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 :actor_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      add :target_user_id, references(:users, type: :binary_id, on_delete: :nilify_all)
      add :action, :string, null: false
      add :data, :map, null: false, default: %{}

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

    create index(:org_audit_events, [:organization_id, :id])

    create table(:api_tokens, primary_key: false) do
      add :id, :binary_id, primary_key: true
      add :user_id, references(:users, type: :binary_id, on_delete: :delete_all), null: false
      add :label, :string, null: false
      # SHA-256 of the token; the token itself is shown once.
      add :token_hash, :binary, null: false
      add :token_hint, :string, null: false
      add :scopes, {:array, :string}, null: false, default: []
      add :last_used_at, :utc_datetime

      timestamps(type: :utc_datetime)
    end

    create unique_index(:api_tokens, [:token_hash])
    create index(:api_tokens, [:user_id])
  end
end