From 742803b92704695486566a966679faa594ba35bc Mon Sep 17 00:00:00 2001 From: Danielwhyte Date: Wed, 12 Sep 2018 16:12:03 +0100 Subject: [PATCH] adds schema for address book, #1 --- README.md | 95 ++++++++++++++++++- config/dev.exs | 12 ++- config/test.exs | 2 +- lib/append/address.ex | 23 +++++ mix.exs | 10 +- .../20180912142549_create_addresses.exs | 23 +++++ 6 files changed, 154 insertions(+), 11 deletions(-) create mode 100644 lib/append/address.ex create mode 100644 priv/repo/migrations/20180912142549_create_addresses.exs diff --git a/README.md b/README.md index 3f1965c..522279d 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,14 @@ Make sure you have installed on your machine: + Phoenix: https://hexdocs.pm/phoenix/installation.html + PostgreSQL: https://www.postgresql.org/download/ -### Getting started +Make sure you have a non-default PostgresQL user, with no more than `CREATEDB` privileges. If not, follow the steps below: + ++ open psql by typing `psql` into your terminal ++ In psql, type: + + `CREATE USER append_only;` + + `ALTER USER append_only CREATEDB;` + +### 1. Getting started Make a new Phoenix app: @@ -31,8 +38,92 @@ Type `y` when asked if you want to install the dependencies, then follow the ins cd append ``` -then `create the database` for your app: +then, go into your generated config file. In `config/dev.exs` and `config/test.exs` you should see a section that looks like this: +``` elixir +# Configure your database +config :append, Append.Repo, + adapter: Ecto.Adapters.Postgres, + username: "postgres", + password: "postgres", + ... +``` + +Change the username to your non-default PostgreSQL user: + +``` elixir + ... + username: "append_only", + ... +``` + +Once you've done this, `create the database` for your app: ``` mix ecto.create ``` + +### 2. Create the Schema + +We're going to use an address book as an example. run the following generator command to create our schema: + +``` +mix phx.gen.schema Address addresses name:string address_line_1:string address_line_2:string city:string postcode:string tel:string +``` + +This will create two new files: ++ `lib/append/address.ex` ++ `priv/repo/migrations/{timestamp}_create_addresses.exs` + +Before you follow the instructions in your terminal, we'll need to edit the generated migration file. + +The generated file should look like this: + +``` elixir +defmodule Append.Repo.Migrations.CreateAddresses do + use Ecto.Migration + + def change do + create table(:addresses) do + add(:name, :string) + add(:address_line_1, :string) + add(:address_line_2, :string) + add(:city, :string) + add(:postcode, :string) + add(:tel, :string) + + timestamps() + end + + end +end +``` + +We need to edit it to remove update and delete privileges for our user: + +``` elixir +defmodule Append.Repo.Migrations.CreateAddresses do + use Ecto.Migration + + # Get name of our Ecto Repo module from our config + @repo :append |> Application.get_env(:ecto_repos) |> List.first() + # Get username of Ecto Repo from our config + @db_user Application.get_env(:append, @repo)[:username] + + def change do + ... + execute("REVOKE UPDATE, DELETE ON TABLE addresses FROM #{@db_user}") + end +end +``` + +Once this is done, run: +``` +mix ecto.migrate +``` +and you should see the following output: +``` +[info] == Running Append.Repo.Migrations.CreateAddresses.change/0 forward +[info] create table addresses +[info] execute "REVOKE UPDATE, DELETE ON TABLE addresses FROM append_only" +[info] == Migrated in 0.0s +``` diff --git a/config/dev.exs b/config/dev.exs index 75c8352..0a1372c 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -11,8 +11,14 @@ config :append, AppendWeb.Endpoint, debug_errors: true, code_reloader: true, check_origin: false, - watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin", - cd: Path.expand("../assets", __DIR__)]] + watchers: [ + node: [ + "node_modules/brunch/bin/brunch", + "watch", + "--stdin", + cd: Path.expand("../assets", __DIR__) + ] + ] # ## SSL Support # @@ -51,7 +57,7 @@ config :phoenix, :stacktrace_depth, 20 # Configure your database config :append, Append.Repo, adapter: Ecto.Adapters.Postgres, - username: "postgres", + username: "append_only", password: "postgres", database: "append_dev", hostname: "localhost", diff --git a/config/test.exs b/config/test.exs index 6dc55d4..9ad4192 100644 --- a/config/test.exs +++ b/config/test.exs @@ -12,7 +12,7 @@ config :logger, level: :warn # Configure your database config :append, Append.Repo, adapter: Ecto.Adapters.Postgres, - username: "postgres", + username: "append_only", password: "postgres", database: "append_test", hostname: "localhost", diff --git a/lib/append/address.ex b/lib/append/address.ex new file mode 100644 index 0000000..f6eae99 --- /dev/null +++ b/lib/append/address.ex @@ -0,0 +1,23 @@ +defmodule Append.Address do + use Ecto.Schema + import Ecto.Changeset + + + schema "addresses" do + field :address_line_1, :string + field :address_line_2, :string + field :city, :string + field :name, :string + field :postcode, :string + field :tel, :string + + timestamps() + end + + @doc false + def changeset(address, attrs) do + address + |> cast(attrs, [:name, :address_line_1, :address_line_2, :city, :postcode, :tel]) + |> validate_required([:name, :address_line_1, :address_line_2, :city, :postcode, :tel]) + end +end diff --git a/mix.exs b/mix.exs index 2721228..2d7653a 100644 --- a/mix.exs +++ b/mix.exs @@ -6,9 +6,9 @@ defmodule Append.Mixfile do app: :append, version: "0.0.1", elixir: "~> 1.4", - elixirc_paths: elixirc_paths(Mix.env), - compilers: [:phoenix, :gettext] ++ Mix.compilers, - start_permanent: Mix.env == :prod, + elixirc_paths: elixirc_paths(Mix.env()), + compilers: [:phoenix, :gettext] ++ Mix.compilers(), + start_permanent: Mix.env() == :prod, aliases: aliases(), deps: deps() ] @@ -26,7 +26,7 @@ defmodule Append.Mixfile do # Specifies which paths to compile per environment. defp elixirc_paths(:test), do: ["lib", "test/support"] - defp elixirc_paths(_), do: ["lib"] + defp elixirc_paths(_), do: ["lib"] # Specifies your project dependencies. # @@ -54,7 +54,7 @@ defmodule Append.Mixfile do [ "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], "ecto.reset": ["ecto.drop", "ecto.setup"], - "test": ["ecto.create --quiet", "ecto.migrate", "test"] + test: ["ecto.create --quiet", "ecto.migrate", "test"] ] end end diff --git a/priv/repo/migrations/20180912142549_create_addresses.exs b/priv/repo/migrations/20180912142549_create_addresses.exs new file mode 100644 index 0000000..e73a011 --- /dev/null +++ b/priv/repo/migrations/20180912142549_create_addresses.exs @@ -0,0 +1,23 @@ +defmodule Append.Repo.Migrations.CreateAddresses do + use Ecto.Migration + + # Get name of our Ecto Repo module from our config + @repo :append |> Application.get_env(:ecto_repos) |> List.first() + # Get username of Ecto Repo from our config + @db_user Application.get_env(:append, @repo)[:username] + + def change do + create table(:addresses) do + add(:name, :string) + add(:address_line_1, :string) + add(:address_line_2, :string) + add(:city, :string) + add(:postcode, :string) + add(:tel, :string) + + timestamps() + end + + execute("REVOKE UPDATE, DELETE ON TABLE addresses FROM #{@db_user}") + end +end