-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create tags and captures_tags tables #16
base: master
Are you sure you want to change the base?
Changes from 3 commits
4a9f880
59f4e65
e2ee3aa
14dc9b4
17a1523
14bff08
f210730
90bd161
e07d853
e4d440b
d9e7c6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
language: elixir | ||
elixir: | ||
- 1.9 | ||
otp_release: | ||
- 22.1.8 | ||
env: | ||
- MIX_ENV=test | ||
cache: | ||
directories: | ||
- _build | ||
- deps | ||
services: | ||
- postgresql | ||
env: | ||
global: | ||
- MIX_ENV=test | ||
before_script: | ||
- mix do ecto.create, ecto.migrate | ||
script: | ||
- mix cover | ||
after_success: | ||
- bash <(curl -s https://codecov.io/bash) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,20 @@ defmodule AppApi.Captures.Capture do | |
use Ecto.Schema | ||
import Ecto.Changeset | ||
alias AppApi.Timers.Timer | ||
alias AppApi.Tags.Tag | ||
import Ecto.Query, warn: false | ||
alias AppApi.Repo | ||
|
||
schema "captures" do | ||
field :completed, :boolean, default: false | ||
field :id_person, :integer | ||
field :text, :string | ||
has_many :timers, Timer | ||
|
||
many_to_many :tags, Tag, | ||
join_through: "captures_tags", | ||
on_replace: :delete | ||
|
||
timestamps() | ||
end | ||
|
||
|
@@ -17,5 +24,24 @@ defmodule AppApi.Captures.Capture do | |
capture | ||
|> cast(attrs, [:id_person, :text, :completed]) | ||
|> validate_required([:id_person, :text, :completed]) | ||
|> put_assoc(:tags, parse_tags(attrs)) | ||
end | ||
|
||
defp parse_tags(params) do | ||
(params["tags"] || "") | ||
|> String.split(",") | ||
|> Enum.map(&String.trim/1) | ||
|> Enum.reject(&(&1 == "")) | ||
|> insert_and_get_all() | ||
end | ||
|
||
defp insert_and_get_all([]) do | ||
[] | ||
end | ||
|
||
defp insert_and_get_all(names) do | ||
maps = Enum.map(names, &%{text: &1}) | ||
Repo.insert_all(Tag, maps, on_conflict: :nothing) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A conflict can appears when inserting tags, for example on a race condition. If mutiple tag are inserted at the same time with the same name the postgres index will report an error. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SimonLab ideally the index should be based on the person_id and tag_id so that different people can have the same tag. But I think we need a dedicated issue for tag ownership. 💭 |
||
Repo.all(from t in Tag, where: t.name in ^names) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule AppApi.Tags.Tag do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
alias AppApi.Captures.Capture | ||
|
||
schema "tags" do | ||
field :text, :string | ||
field :id_person, :integer | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(tag, attrs) do | ||
tag | ||
|> cast(attrs, [:text, :id_person]) | ||
|> validate_required([:text]) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule AppApi.Repo.Migrations.CreateTags do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:tags) do | ||
add :text, :string | ||
add :id_person, :integer | ||
|
||
timestamps() | ||
end | ||
|
||
create unique_index(:tags, [:text]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This index makes sure the database doesn't contain tags with the same name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SimonLab which issue requirement or acceptance criteria is this for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tried to follow the requirements from dwyl/app#245 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SimonLab thanks for clarifying. In which case it could be worth having that issue linked in the PR description and having it "in-progress" on the Kanban board (which makes it easier for the reviewer) 😉 |
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule AppApi.Repo.Migrations.CapturesTags do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:captures_tags, primary_key: false) do | ||
add(:capture_id, references(:captures, on_delete: :delete_all), primary_key: true) | ||
add(:tag_id, references(:tags, on_delete: :delete_all), primary_key: true) | ||
timestamps() | ||
end | ||
|
||
create(index(:captures_tags, [:capture_id])) | ||
create(index(:captures_tags, [:tag_id])) | ||
|
||
create( | ||
unique_index(:captures_tags, [:capture_id, :tag_id], name: :capture_id_tag_id_unique_index) | ||
) | ||
|
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tags are passed as a string separated by a comma