Have Ecto be responsible for creating coach_scraper.export table.

pull/3/head
Joshua Potter 2023-12-04 12:30:38 -07:00
parent 8bf4167e61
commit bb650f6d35
2 changed files with 23 additions and 13 deletions

View File

@ -2,12 +2,13 @@ defmodule BoardWise.Coaches.Coach do
@moduledoc """ @moduledoc """
Table `coach_scraper.export` containing all scraped coach information. 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). The `coach-scraper` app is responsible for periodically rewriting the entries
Said utility is responsible for periodically rewriting the entries in the in the table with fresh data. For the time-being, avoid using any of the
table with fresh data. For the time-being, avoid using any of the mutative mutative functions found in the `Coach` context.
functions found in the `Coach` context.
""" """
use Ecto.Schema use Ecto.Schema
@ -17,6 +18,7 @@ defmodule BoardWise.Coaches.Coach do
field :blitz, :integer field :blitz, :integer
field :bullet, :integer field :bullet, :integer
field :rapid, :integer field :rapid, :integer
field :site, :string field :site, :string
field :username, :string field :username, :string
end end
@ -25,6 +27,7 @@ defmodule BoardWise.Coaches.Coach do
def changeset(coach, attrs) do def changeset(coach, attrs) do
coach coach
|> cast(attrs, [:rapid, :blitz, :bullet, :site, :username]) |> 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
end end

View File

@ -1,17 +1,24 @@
defmodule BoardWise.Repo.Migrations.CreateCoaches do defmodule BoardWise.Repo.Migrations.CreateCoaches do
use Ecto.Migration use Ecto.Migration
# We are not responsible for generating this table in production. This @prefix "coach_scraper"
# migration exists for testing purposes
def change do
execute "CREATE SCHEMA IF NOT EXISTS coach_scraper"
create_if_not_exists table(:export, prefix: "coach_scraper") do def change do
add :site, :string execute "CREATE SCHEMA coach_scraper"
add :username, :string
create table(:export, prefix: @prefix) do
add :site, :string, null: false
add :username, :string, null: false
add :rapid, :integer add :rapid, :integer
add :blitz, :integer add :blitz, :integer
add :bullet, :integer add :bullet, :integer
end end
create unique_index(
:export,
[:site, :username],
prefix: @prefix,
name: "site_username_unique"
)
end end
end end