Files
chess-endgame-trainer/priv/repo/seeds.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

108 lines
2.8 KiB
Elixir

# Script for populating the database. You can run it as:
#
# mix run priv/repo/seeds.exs
#
# Inside the script, you can read and write to any of your
# repositories directly:
#
# Chesstrainer.Repo.insert!(%Chesstrainer.SomeSchema{})
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
Code.require_file("seeds/endgames/win.ex", __DIR__)
Code.require_file("seeds/endgames/draw.ex", __DIR__)
Code.require_file("seeds/endgames.ex", __DIR__)
Code.require_file("seeds/themes.ex", __DIR__)
alias Chesstrainer.Repo
alias Chesstrainer.Endgames.Endgame
alias Chesstrainer.Themes.Theme
alias Chesstrainer.Repo.Seeds.Endgames
alias Chesstrainer.Repo.Seeds.Themes, as: SeedThemes
now = DateTime.utc_now() |> DateTime.truncate(:second)
# Seed maps may list `themes: ["Lucena", ...]`. Names are matched to seeded
# themes case-insensitively, and names missing from seeds/themes.ex are created.
endgames =
Enum.map(Endgames.all(), fn endgame ->
{theme_names, attrs} = Map.pop(endgame, :themes, [])
fen = Chesstrainer.FEN.normalize(attrs.fen)
{:ok, key} = Chesstrainer.Key.from_fen(fen)
themes =
theme_names
|> Enum.map(&String.trim/1)
|> Enum.reject(&(&1 == ""))
|> Enum.uniq_by(&String.downcase/1)
row =
attrs
|> Map.put(:fen, fen)
|> Map.put(:id, Ecto.UUID.generate())
|> Map.put(:public_code, Endgame.generate_public_code())
|> Map.put(:key, key)
|> Map.merge(%{inserted_at: now, updated_at: now})
%{fen: fen, themes: themes, row: row}
end)
IO.puts("🌱 Seeding endgame themes into DB...")
theme_names =
(SeedThemes.all() ++ Enum.flat_map(endgames, & &1.themes))
|> Enum.map(&String.trim/1)
|> Enum.reject(&(&1 == ""))
|> Enum.uniq_by(&String.downcase/1)
Repo.insert_all(
Theme,
Enum.map(theme_names, fn name ->
%{
name: name,
slug: Theme.slugify(name),
kind: :technique,
category: :endgame,
inserted_at: now,
updated_at: now
}
end),
on_conflict: :nothing
)
theme_id_by_name =
Theme
|> Repo.all()
|> Enum.filter(&(&1.category == :endgame))
|> Map.new(&{String.downcase(&1.name), &1.id})
IO.puts("✅ Done seeding endgame themes.")
IO.puts("🌱 Seeding basic endgames into DB...")
Repo.insert_all(Endgame, Enum.map(endgames, & &1.row), on_conflict: :nothing)
endgame_id_by_fen =
Endgame
|> Repo.all()
|> Map.new(&{&1.fen, &1.id})
links =
Enum.flat_map(endgames, fn %{fen: fen, themes: themes} ->
endgame_id = Map.fetch!(endgame_id_by_fen, fen)
Enum.map(themes, fn name ->
%{
endgame_id: Ecto.UUID.dump!(endgame_id),
theme_id: Map.fetch!(theme_id_by_name, String.downcase(name))
}
end)
end)
if links != [] do
Repo.insert_all("endgames_themes", links, on_conflict: :nothing)
end
IO.puts("✅ Done seeding endgames.")