Skip to content

Commit

Permalink
adds schema for address book, #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Danwhy committed Sep 12, 2018
1 parent ea47491 commit 742803b
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 11 deletions.
95 changes: 93 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
```
12 changes: 9 additions & 3 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions lib/append/address.ex
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
]
Expand All @@ -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.
#
Expand Down Expand Up @@ -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
23 changes: 23 additions & 0 deletions priv/repo/migrations/20180912142549_create_addresses.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 742803b

Please sign in to comment.