Skip to content

Commit

Permalink
inline the list and list_items migrations for dwyl/app#271
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 12, 2023
1 parent dab0425 commit f32382c
Showing 1 changed file with 65 additions and 8 deletions.
73 changes: 65 additions & 8 deletions src/mvp/16-lists.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Lists

Our **MVP `App`** already has
Our **MVP App** already has
much of the basic functionality we need/want
but it's sorely lacking
but is sorely lacking
a way to **_organize_ `items`**
into distinct projects/areas.
Let's fix that by introducing `lists`!
Expand Down Expand Up @@ -44,14 +44,60 @@ that will make the `App` more _useful_ both to individuals and teams.

Create the `lists` and `list_items` tables
with the following
[**`mix phx.gen.schema`**](https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.Schema.html)
[`mix phx.gen.schema`](https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.Schema.html)
commands:

```sh
mix phx.gen.schema List lists person_id:integer status:integer text:string
mix phx.gen.schema ListItems list_items item_id:references:items list_id:references:lists person_id:integer position:float
```

Those two commands will create the following migrations:

[`20230416001029_create_lists.exs`](https://github.com/dwyl/mvp/blob/fa0b79eebbe582e3b24213a2c657d0fc343782c9/priv/repo/migrations/20230416001029_create_lists.exs)

```elixir
defmodule App.Repo.Migrations.CreateLists do
use Ecto.Migration

def change do
create table(:lists) do
add :text, :string
add :person_id, :integer
add :status, :integer

timestamps()
end
end
end
```

[`20230416001045_create_list_items.exs`](https://github.com/dwyl/mvp/blob/54aff67d172ad372c5ee1ffd9c41f24b093b78d4/priv/repo/migrations/20230416001045_create_list_items.exs)

```elixir
defmodule App.Repo.Migrations.CreateListItems do
use Ecto.Migration

def change do
create table(:list_items) do
add :person_id, :integer
add :position, :float
add :item_id, references(:items, on_delete: :nothing)
add :list_id, references(:lists, on_delete: :nothing)

timestamps()
end

create index(:list_items, [:item_id])
create index(:list_items, [:list_id])
end
end
```

The Database tables these migrations create
are very simple.


Once we run `mix ecto.migrate`,
we have the following database
ERD:
Expand All @@ -65,7 +111,7 @@ to build the `lists` features. 🎉
## Create Tests (TDD)

The `gen.schema` command only creates the migration files
and the corresponding schema in the
and the corresponding schema files in the
[`lib/app`](https://github.com/dwyl/mvp/tree/main/lib/app)
directory of our `Phoenix App`.
It does not create any `CRUD` functions or tests.
Expand All @@ -76,6 +122,14 @@ We _manually_ created the following test files:
+ test/app/list_test.exs
+ test/app/list_items_test.exs

Inside the test files we have all the tests necessary
to build the _baseline_ `lists` features.
e.g:

```elixir

```

> **TODO**: hyperlink these two files, once they are merged into `main`.
Rather than _duplicating_ large blocks of test code here,
Expand Down Expand Up @@ -110,16 +164,20 @@ def create_list(attrs) do
end
```

The `attrs` are just
`person_id` the `id` of the `person` creating the list,
`text` the name/description of the list
and `status` (optional).

If you are new to the `PaperTrail` package and the benefits it offers,
we wrote a quick intro:
[dwyl/phoenix-papertrail-demo](https://github.com/dwyl/phoenix-papertrail-demo)

The gist is this: it gives us version history for records in our database
without significant query overhead.
without any query overhead.

## `add_list_item/4`


`add_list_item/4` as you would expect,
adds an `item` to a `list` for the given `person_id`

Expand All @@ -140,7 +198,7 @@ The first 3 arguments are self-explanatory;
the final one, `position` is there to help us _order_ the `list`!
As you may have noticed in the `mix gen.schema` command above
for the `list_items` table, the `position` field is a `Float`.
e.g: `1.0`
e.g: `1.0`.
We have very deliberately chosen a `Float` not an `Integer`
so that we can use this for easy append-only positional sorting.
More on this later in the "Reordering" chapter.
Expand All @@ -152,4 +210,3 @@ see:




0 comments on commit f32382c

Please sign in to comment.