diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 69c3c68..4e92539 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -19,35 +19,42 @@ alias Chesstrainer.Repo alias Chesstrainer.Endgames.Endgame alias Chesstrainer.Themes.Theme alias Chesstrainer.Repo.Seeds.Endgames -alias Chesstrainer.Repo.Seeds.Themes +alias Chesstrainer.Repo.Seeds.Themes, as: SeedThemes now = DateTime.utc_now() |> DateTime.truncate(:second) -IO.puts("🌱 Seeding basic endgames into DB...") - -endgames = Endgames.all() - -Repo.insert_all( - Endgame, - Enum.map(endgames, fn endgame -> - fen = Chesstrainer.FEN.normalize(endgame.fen) +# 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) - endgame - |> 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}) - end), - on_conflict: :nothing -) + themes = + theme_names + |> Enum.map(&String.trim/1) + |> Enum.reject(&(&1 == "")) + |> Enum.uniq_by(&String.downcase/1) -IO.puts("✅ Done seeding endgames.") + 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 = Themes.all() +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, @@ -57,4 +64,37 @@ Repo.insert_all( 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.") diff --git a/priv/repo/seeds/endgames/win.ex b/priv/repo/seeds/endgames/win.ex index f630c1c..c18e358 100644 --- a/priv/repo/seeds/endgames/win.ex +++ b/priv/repo/seeds/endgames/win.ex @@ -6,7 +6,8 @@ defmodule Chesstrainer.Repo.Seeds.Endgames.Win do color: :white, message: "Rook vs king endgame", notes: "Basic rook endgame", - result: :win + result: :win, + themes: ["rook endgame"] }, %{ fen: "8/8/8/1P4p1/5k2/5p2/P6K/8 b - - 0 0", diff --git a/priv/repo/seeds/themes.ex b/priv/repo/seeds/themes.ex index e11b12a..d770e99 100644 --- a/priv/repo/seeds/themes.ex +++ b/priv/repo/seeds/themes.ex @@ -1,4 +1,6 @@ defmodule Chesstrainer.Repo.Seeds.Themes do + # Endgame seed maps may also reference themes with `themes: ["Lucena", ...]`; + # names missing from this list are created when the seeds run. def all do [ "mate in 2",