4.1 KiB · text History bbb2a48
defmodule Vigil.Repo.Migrations.CreateReleasesArtifacts do
  use Ecto.Migration

  # Slice 5 (ROADMAP.md): deploys, chunk-upload blobs, assemble status, and
  # artifact bundles with the files extracted from them.
  def change do
    create table(:deploys, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :release_id, references(:releases, type: :binary_id, on_delete: :delete_all),
        null: false

      add :environment, :string, null: false
      add :name, :string
      add :url, :string
      add :date_started, :utc_datetime
      add :date_finished, :utc_datetime, null: false

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

    create index(:deploys, [:release_id])

    # One chunk of an upload, by its SHA-1, per organization (GlitchTip's
    # are global). The bytes live in Vigil.Storage under `storage_key`.
    create table(:file_blobs, 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 :checksum, :string, null: false
      add :size, :bigint, null: false
      add :storage_key, :string, null: false

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

    create unique_index(:file_blobs, [:organization_id, :checksum])

    # Where an assemble request stands, per (organization, checksum):
    # created, assembling, ok, error.
    create table(:assemble_statuses, 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 :checksum, :string, null: false
      add :state, :string, null: false
      add :detail, :text

      timestamps(type: :utc_datetime)
    end

    create unique_index(:assemble_statuses, [:organization_id, :checksum])

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

      # SHA-1 of the whole bundle zip, as sentry-cli names it.
      add :checksum, :string, null: false
      # The manifest's bundle_id, when it has one.
      add :bundle_id, :string
      add :release, :string
      add :dist, :string
      add :artifact_count, :integer, null: false, default: 0
      add :size, :bigint, null: false, default: 0

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

    create unique_index(:artifact_bundles, [:organization_id, :checksum])
    create index(:artifact_bundles, [:organization_id, :release, :dist])

    create table(:artifact_bundle_projects, primary_key: false) do
      add :artifact_bundle_id,
          references(:artifact_bundles, 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(:artifact_bundle_projects, [:project_id])

    # One file of a bundle: a minified source, its source map, or other.
    create table(:artifact_files, primary_key: false) do
      add :id, :binary_id, primary_key: true

      add :artifact_bundle_id,
          references(:artifact_bundles, type: :binary_id, on_delete: :delete_all),
          null: false

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

      # minified_source | source_map | source | indexed_ram_bundle | other
      add :kind, :string, null: false
      # The manifest's url, e.g. "~/static/js/app.js".
      add :url, :text, null: false
      add :debug_id, :uuid
      # A minified file's `Sourcemap` header: the url of its map.
      add :sourcemap, :text
      add :headers, :map, null: false, default: %{}
      add :size, :bigint, null: false
      add :storage_key, :string, null: false

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

    create index(:artifact_files, [:artifact_bundle_id])
    create index(:artifact_files, [:organization_id, :debug_id], where: "debug_id IS NOT NULL")
  end
end