1.4 KiB · text History b868907
defmodule GitGud.Repo.Migrations.CreateJobTokenGrants do
  use Ecto.Migration

  def change do
    create table(:job_token_grants) do
      # The repository granting access.
      add :repository_id, references(:repositories, on_delete: :delete_all), null: false
      # Whose job tokens it grants to: one repository, or every repository
      # in an organization. Exactly one is set.
      add :source_repository_id, references(:repositories, on_delete: :delete_all)
      add :source_organization_id, references(:organizations, on_delete: :delete_all)
      add :read, :boolean, null: false, default: true
      add :dispatch, :boolean, null: false, default: false

      timestamps(type: :utc_datetime)
    end

    create constraint(:job_token_grants, :exactly_one_source,
             check: "(source_repository_id IS NULL) <> (source_organization_id IS NULL)"
           )

    create unique_index(:job_token_grants, [:repository_id, :source_repository_id],
             where: "source_repository_id IS NOT NULL",
             name: :job_token_grants_repo_source_repo_index
           )

    create unique_index(:job_token_grants, [:repository_id, :source_organization_id],
             where: "source_organization_id IS NOT NULL",
             name: :job_token_grants_repo_source_org_index
           )

    create index(:job_token_grants, [:source_repository_id])
    create index(:job_token_grants, [:source_organization_id])
  end
end