1.7 KiB · text 5af55d9
defmodule GitGud.Repo.Migrations.OrgLabelsAndOptouts do
use Ecto.Migration
def change do
# Labels can now be owned by an organization instead of a single
# repository. Org labels are visible across every repo the org
# owns unless that repo opts out (see `repository_label_opt_outs`).
alter table(:labels) do
add :organization_id, references(:organizations, on_delete: :delete_all)
end
# Repository_id was NOT NULL — relax it now that org-scoped rows
# exist. Enforce one-or-the-other via a CHECK so we don't end up
# with orphan rows.
execute(
"ALTER TABLE labels ALTER COLUMN repository_id DROP NOT NULL",
"ALTER TABLE labels ALTER COLUMN repository_id SET NOT NULL"
)
create constraint(:labels, :labels_scope_xor,
check:
"(repository_id IS NOT NULL AND organization_id IS NULL) OR " <>
"(repository_id IS NULL AND organization_id IS NOT NULL)"
)
create index(:labels, [:organization_id])
create unique_index(:labels, [:organization_id, :name],
where: "organization_id IS NOT NULL",
name: :labels_organization_id_name_index
)
# Per-repo opt-outs from org labels. Presence of a row means this
# repo hides that org label from its inherited set.
create table(:repository_label_opt_outs) do
add :repository_id, references(:repositories, on_delete: :delete_all), null: false
add :label_id, references(:labels, on_delete: :delete_all), null: false
timestamps(type: :utc_datetime, updated_at: false)
end
create unique_index(:repository_label_opt_outs, [:repository_id, :label_id])
create index(:repository_label_opt_outs, [:label_id])
end
end