Files
chess-endgame-trainer/priv/repo/migrations/20261117191557_theme_provenance_kinds_and_coverage.exs
T
owenrees 80cb033077 Add theme slugs, kinds and link provenance (#192)
* add function for upgrading a user's plan in interactive Elixir

* Add theme slugs, kinds and link provenance

Themes gain a stable slug, an optional description and a kind so the app can
tell material, motif and technique labels apart. Slugs are generated from the
name on first save and keep their value when the theme is renamed, so links to
a theme stay valid.

Endgame-to-theme links now record their source (derived, curated or community)
and creation time. Existing links are backfilled as curated. A derived pass can
therefore replace only its own links, leaving human work untouched.

The admin index shows each theme's kind and its coverage across the library.
Creating a theme name requires the manage_themes capability, and members get an
unauthorized error instead, ahead of the suggestion flow.

Closes #185
2026-11-17 20:46:43 +01:00

91 lines
2.3 KiB
Elixir

defmodule Chesstrainer.Repo.Migrations.ThemeProvenanceKindsAndCoverage do
use Ecto.Migration
import Ecto.Query
def up do
alter table(:themes) do
add :slug, :string
add :description, :string
add :kind, :string, null: false, default: "technique"
end
alter table(:endgames_themes) do
add :source, :string, null: false, default: "curated"
add :inserted_at, :utc_datetime, null: false, default: fragment("now()")
end
flush()
backfill_slugs()
alter table(:themes) do
modify :slug, :string, null: false
end
create unique_index(:themes, [:slug])
create constraint(:themes, :kind_in_allowed,
check: "kind in ('material', 'motif', 'technique')"
)
create constraint(:endgames_themes, :source_in_allowed,
check: "source in ('derived', 'curated', 'community')"
)
end
def down do
drop constraint(:endgames_themes, :source_in_allowed)
drop constraint(:themes, :kind_in_allowed)
drop unique_index(:themes, [:slug])
alter table(:endgames_themes) do
remove :inserted_at
remove :source
end
alter table(:themes) do
remove :kind
remove :description
remove :slug
end
end
# Themes were created without a slug, so every existing row needs one before
# the column can become mandatory. Names are unique per category but not
# globally, so collisions get a numeric suffix.
defp backfill_slugs do
themes = repo().all(from(t in "themes", select: {t.id, t.name}))
Enum.reduce(themes, MapSet.new(), fn {id, name}, used ->
slug = unique_slug(slugify(name), used)
repo().update_all(from(t in "themes", where: t.id == ^id), set: [slug: slug])
MapSet.put(used, slug)
end)
end
defp unique_slug(base, used) do
if MapSet.member?(used, base) do
Enum.find_value(2..1_000, base, fn suffix ->
candidate = "#{base}-#{suffix}"
if MapSet.member?(used, candidate), do: nil, else: candidate
end)
else
base
end
end
defp slugify(name) do
slug =
name
|> to_string()
|> :unicode.characters_to_nfd_binary()
|> String.replace(~r/[\x{0300}-\x{036F}]/u, "")
|> String.downcase()
|> String.replace(~r/[^a-z0-9]+/u, "-")
|> String.trim("-")
if slug == "", do: "theme", else: slug
end
end