mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
Add public codes for player URLs and admin endgame search
Player-facing endgame links now use a 10-character Crockford base32 public code instead of the database UUID; admin routes keep the UUID. Existing rows are backfilled by migration, and the admin Show page displays and copies the code. The admin endgame index gains its own search box: exact database id, exact public code, or a FEN / FEN fragment. A single match opens the endgame; multiple matches list below; nothing found says so. Closes #44 Closes #45
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
defmodule Chesstrainer.Repo.Migrations.AddPublicCodeToEndgames do
|
||||
use Ecto.Migration
|
||||
|
||||
@alphabet ~c"0123456789abcdefghjkmnpqrstvwxyz"
|
||||
@length 10
|
||||
|
||||
def up do
|
||||
alter table(:endgames) do
|
||||
add :public_code, :string
|
||||
end
|
||||
|
||||
flush()
|
||||
|
||||
repo().query!("SELECT id FROM endgames").rows
|
||||
|> Enum.reduce(MapSet.new(), fn [id], used ->
|
||||
code = Stream.repeatedly(&generate_code/0) |> Enum.find(&(not MapSet.member?(used, &1)))
|
||||
|
||||
repo().query!("UPDATE endgames SET public_code = $1 WHERE id = $2", [code, id])
|
||||
|
||||
MapSet.put(used, code)
|
||||
end)
|
||||
|
||||
alter table(:endgames) do
|
||||
modify :public_code, :string, null: false
|
||||
end
|
||||
|
||||
create unique_index(:endgames, [:public_code])
|
||||
end
|
||||
|
||||
def down do
|
||||
drop unique_index(:endgames, [:public_code])
|
||||
|
||||
alter table(:endgames) do
|
||||
remove :public_code
|
||||
end
|
||||
end
|
||||
|
||||
# Inlined rather than calling the schema, so the migration stays frozen even
|
||||
# if the app's generator changes. `rem(byte, 32)` is unbiased: 256 = 8 * 32.
|
||||
defp generate_code do
|
||||
@length
|
||||
|> :crypto.strong_rand_bytes()
|
||||
|> :binary.bin_to_list()
|
||||
|> Enum.map(fn byte -> Enum.at(@alphabet, rem(byte, length(@alphabet))) end)
|
||||
|> List.to_string()
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user