-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to configure Originator Field Options (#115)
also adds better support for :binary_id UUIDs
- Loading branch information
Showing
12 changed files
with
175 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
priv/uuid_with_custom_name_repo/migrations/20201130190530_create_projects.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule PaperTrail.UUIDWithCustomNameRepo.Migrations.CreateProjects do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:projects, primary_key: false) do | ||
add :uuid, :binary_id, primary_key: true | ||
add :name, :string, null: false | ||
|
||
timestamps() | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
priv/uuid_with_custom_name_repo/migrations/20201130190545_create_people.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule PaperTrail.UUIDWithCustomNameRepo.Migrations.CreatePeople do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:people, primary_key: false) do | ||
add :uuid, :binary_id, primary_key: true | ||
add :email, :string, null: false | ||
|
||
timestamps() | ||
end | ||
end | ||
end |
22 changes: 22 additions & 0 deletions
22
priv/uuid_with_custom_name_repo/migrations/20201130190555_create_versions.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule PaperTrail.UUIDWithCustomNameRepo.Migrations.CreateVersions do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:versions) do | ||
add :event, :string, null: false, size: 10 | ||
add :item_type, :string, null: false | ||
add :item_id, (if System.get_env("STRING_TEST") == nil, do: :binary_id, else: :string) | ||
add :item_changes, :map, null: false | ||
add :originator_id, references(:people, type: :binary_id, column: :uuid) | ||
add :origin, :string, size: 50 | ||
add :meta, :map | ||
|
||
add :inserted_at, :utc_datetime, null: false | ||
end | ||
|
||
create index(:versions, [:originator_id]) | ||
create index(:versions, [:item_id, :item_type]) | ||
create index(:versions, [:event, :item_type]) | ||
create index(:versions, [:item_type, :inserted_at]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
defmodule Project do | ||
use Ecto.Schema | ||
|
||
import Ecto.Changeset | ||
|
||
@primary_key {:uuid, :binary_id, autogenerate: true} | ||
schema "projects" do | ||
field(:name, :string) | ||
|
||
timestamps() | ||
end | ||
|
||
def changeset(model, params \\ %{}) do | ||
model | ||
|> cast(params, [:name]) | ||
|> validate_required([:name]) | ||
end | ||
end | ||
|
||
defmodule Person do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
@primary_key {:uuid, :binary_id, autogenerate: true} | ||
schema "people" do | ||
field(:email, :string) | ||
|
||
timestamps() | ||
end | ||
|
||
def changeset(model, params \\ %{}) do | ||
model | ||
|> cast(params, [:email]) | ||
|> validate_required([:email]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
defmodule PaperTrailTest.UUIDWithCustomNameTest do | ||
use ExUnit.Case | ||
import PaperTrail.RepoClient, only: [repo: 0] | ||
alias PaperTrail.Version | ||
import Ecto.Query | ||
|
||
setup_all do | ||
Application.put_env(:paper_trail, :repo, PaperTrail.UUIDWithCustomNameRepo) | ||
Application.put_env(:paper_trail, :originator, name: :originator, model: Person) | ||
Application.put_env(:paper_trail, :originator_type, Ecto.UUID) | ||
Application.put_env(:paper_trail, :originator_relationship_options, references: :uuid) | ||
|
||
Application.put_env( | ||
:paper_trail, | ||
:item_type, | ||
if(System.get_env("STRING_TEST") == nil, do: Ecto.UUID, else: :string) | ||
) | ||
|
||
Code.eval_file("lib/paper_trail.ex") | ||
Code.eval_file("lib/version.ex") | ||
|
||
repo().delete_all(Version) | ||
repo().delete_all(Person) | ||
repo().delete_all(Project) | ||
:ok | ||
end | ||
|
||
describe "PaperTrailTest.UUIDWithCustomNameTest" do | ||
test "handles originators with a UUID primary key" do | ||
person = | ||
%Person{} | ||
|> Person.changeset(%{email: "admin@example.com"}) | ||
|> repo().insert! | ||
|
||
%Project{} | ||
|> Project.changeset(%{name: "Interesting Stuff"}) | ||
|> PaperTrail.insert!(originator: person) | ||
|
||
version = | ||
Version | ||
|> last | ||
|> repo().one | ||
|> repo().preload(:originator) | ||
|
||
assert version.originator == person | ||
end | ||
end | ||
end |