1.1 KiB · text History aa9e861
defmodule GitGud.Repo.Migrations.AddPackageRetention do
  use Ecto.Migration

  @moduledoc """
  Per-package retention, and the pins that survive it.

  All three retention columns are nullable, and nil means "inherit the
  instance policy" — which is what lets the instance ceiling move
  without rewriting every package row.
  """

  def change do
    alter table(:packages) do
      add :retention_mode, :string
      add :retention_keep, :integer
      add :retention_bytes, :bigint
    end

    alter table(:package_versions) do
      add :pinned_at, :utc_datetime
      add :pinned_by_id, references(:users, on_delete: :nilify_all)
    end

    # Retention counts and pin counts are both per-package walks over
    # versions, newest first.
    create index(:package_versions, [:package_id, :pushed_at])

    # "Which versions are pinned" is asked per package on every plan,
    # and pinned rows are the small minority.
    create index(:package_versions, [:package_id],
             where: "pinned_at IS NOT NULL",
             name: :package_versions_pinned_index
           )
  end
end