From bb650f6d35f17761e7d06e568949067290dc7575 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Mon, 4 Dec 2023 12:30:38 -0700 Subject: [PATCH] Have Ecto be responsible for creating coach_scraper.export table. --- lib/boardwise/coaches/coach.ex | 15 +++++++------ .../20231204141656_create_coaches.exs | 21 ++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/boardwise/coaches/coach.ex b/lib/boardwise/coaches/coach.ex index 146fc23..87c4e95 100644 --- a/lib/boardwise/coaches/coach.ex +++ b/lib/boardwise/coaches/coach.ex @@ -2,12 +2,13 @@ defmodule BoardWise.Coaches.Coach do @moduledoc """ Table `coach_scraper.export` containing all scraped coach information. - Notice this table does not exist in the `public` schema. + Though initially created by our Phoenix application, the data found within + this table is managed by an external utility: + [coach-scraper](https://github.com/boardwise-gg/coach-scraper). - This table is managed by an external utility [coach-scraper](https://github.com/boardwise-gg/coach-scraper). - Said utility is responsible for periodically rewriting the entries in the - table with fresh data. For the time-being, avoid using any of the mutative - functions found in the `Coach` context. + The `coach-scraper` app is responsible for periodically rewriting the entries + in the table with fresh data. For the time-being, avoid using any of the + mutative functions found in the `Coach` context. """ use Ecto.Schema @@ -17,6 +18,7 @@ defmodule BoardWise.Coaches.Coach do field :blitz, :integer field :bullet, :integer field :rapid, :integer + field :site, :string field :username, :string end @@ -25,6 +27,7 @@ defmodule BoardWise.Coaches.Coach do def changeset(coach, attrs) do coach |> cast(attrs, [:rapid, :blitz, :bullet, :site, :username]) - |> validate_required([:rapid, :blitz, :bullet, :site, :username]) + |> validate_required([:site, :username]) + |> unique_constraint(:site_username_unique, name: :site_username_unique) end end diff --git a/priv/repo/migrations/20231204141656_create_coaches.exs b/priv/repo/migrations/20231204141656_create_coaches.exs index 374e478..7175311 100644 --- a/priv/repo/migrations/20231204141656_create_coaches.exs +++ b/priv/repo/migrations/20231204141656_create_coaches.exs @@ -1,17 +1,24 @@ defmodule BoardWise.Repo.Migrations.CreateCoaches do use Ecto.Migration - # We are not responsible for generating this table in production. This - # migration exists for testing purposes - def change do - execute "CREATE SCHEMA IF NOT EXISTS coach_scraper" + @prefix "coach_scraper" - create_if_not_exists table(:export, prefix: "coach_scraper") do - add :site, :string - add :username, :string + def change do + execute "CREATE SCHEMA coach_scraper" + + create table(:export, prefix: @prefix) do + add :site, :string, null: false + add :username, :string, null: false add :rapid, :integer add :blitz, :integer add :bullet, :integer end + + create unique_index( + :export, + [:site, :username], + prefix: @prefix, + name: "site_username_unique" + ) end end