-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test papertail versions using UUID columns
- Loading branch information
1 parent
e22cba9
commit ae834f9
Showing
9 changed files
with
151 additions
and
4 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
12 changes: 12 additions & 0 deletions
12
priv/uuid_repo/migrations/20170525133833_create_uuid_products.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.UUIDRepo.Migrations.CreateUuidProducts do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:products, primary_key: false) do | ||
add :id, :binary_id, primary_key: true | ||
add :name, :string, null: false | ||
|
||
timestamps() | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
priv/uuid_repo/migrations/20170525142546_create_admins.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.UUIDRepo.Migrations.CreateAdmins do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:admins, primary_key: false) do | ||
add :id, :binary_id, primary_key: true | ||
add :email, :string, null: false | ||
|
||
timestamps() | ||
end | ||
end | ||
end |
22 changes: 22 additions & 0 deletions
22
priv/uuid_repo/migrations/20170525142612_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.UUIDRepo.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, :binary_id | ||
add :item_changes, :map, null: false | ||
add :originator_id, references(:admins, type: :binary_id) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule PaperTrailTest.UUIDTest do | ||
use ExUnit.Case | ||
import PaperTrail.RepoClient, only: [repo: 0] | ||
alias PaperTrail.Version | ||
import Ecto.Query | ||
|
||
|
||
setup do | ||
Application.put_env(:paper_trail, :repo, PaperTrail.UUIDRepo) | ||
Application.put_env(:paper_trail, :originator, [name: :admin, model: Admin]) | ||
Application.put_env(:paper_trail, :originator_type, Ecto.UUID) | ||
Application.put_env(:paper_trail, :item_type, Ecto.UUID) | ||
Code.eval_file("lib/paper_trail.ex") | ||
Code.eval_file("lib/version.ex") | ||
repo().delete_all(Version) | ||
repo().delete_all(Admin) | ||
repo().delete_all(Product) | ||
:ok | ||
end | ||
|
||
test "creates versions with models that have a UUID primary key" do | ||
product = | ||
%Product{} | ||
|> Product.changeset(%{name: "Hair Cream"}) | ||
|> PaperTrail.insert! | ||
|
||
version = Version |> last |> repo.one | ||
|
||
assert version.item_id == product.id | ||
assert version.item_type == "Product" | ||
end | ||
|
||
test "handles originators with a UUID primary key" do | ||
admin = | ||
%Admin{} | ||
|> Admin.changeset(%{email: "admin@example.com"}) | ||
|> repo.insert! | ||
|
||
product = | ||
%Product{} | ||
|> Product.changeset(%{name: "Hair Cream"}) | ||
|> PaperTrail.insert!(originator: admin) | ||
|
||
version = | ||
Version | ||
|> last | ||
|> repo.one | ||
|> repo.preload(:admin) | ||
|
||
assert version.admin == admin | ||
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 Product do | ||
use Ecto.Schema | ||
|
||
import Ecto.Changeset | ||
|
||
@primary_key {:id, :binary_id, autogenerate: true} | ||
schema "products" do | ||
field :name, :string | ||
|
||
timestamps() | ||
end | ||
|
||
def changeset(model, params \\ %{}) do | ||
model | ||
|> cast(params, [:name]) | ||
|> validate_required([:name]) | ||
end | ||
end | ||
|
||
defmodule Admin do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
@primary_key {:id, :binary_id, autogenerate: true} | ||
schema "admins" 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