forked from ZennerIoT/ex_audit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
can track a lifecycle of an entity without associations
repo functions that can track now: * update/2 * create/2 * delete/2 * !update/2 * !create/2 * !delete/2
- Loading branch information
Showing
11 changed files
with
203 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
use Mix.Config | ||
|
||
config :ex_audit, ecto_repos: [ExAudit.Test.Repo] | ||
config :ex_audit, | ||
ecto_repos: [ExAudit.Test.Repo], | ||
version_schema: ExAudit.Test.Version, | ||
tracked_schemas: [ | ||
ExAudit.Test.User, | ||
ExAudit.Test.BlogPost, | ||
ExAudit.Test.BlogPost.Section, | ||
ExAudit.Test.Comment | ||
] | ||
|
||
config :ex_audit, ExAudit.Test.Repo, | ||
adapter: Ecto.Adapters.Postgres, | ||
username: "postgres", | ||
password: "postgres", | ||
database: "ex_audit_test", | ||
hostname: "localhost", | ||
pool_size: 10 | ||
pool_size: 10 | ||
|
||
import_config "#{Mix.env}.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,10 @@ | ||
use Mix.Config | ||
|
||
config :ex_audit, ExAudit.Test.Repo, | ||
adapter: Ecto.Adapters.Postgres, | ||
pool: Ecto.Adapters.SQL.Sandbox, | ||
username: "postgres", | ||
password: "postgres", | ||
database: "ex_audit_test", | ||
hostname: "localhost", | ||
pool_size: 10 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,73 @@ | ||
defmodule ExAudit.Schema do | ||
def insert_all(module, adapter, schema_or_source, entries, opts) do | ||
# TODO! | ||
Ecto.Repo.Schema.insert_all(module, adapter, schema_or_source, entries, opts) | ||
end | ||
|
||
def insert(module, adapter, struct, opts) do | ||
Ecto.Repo.Schema.insert(module, adapter, struct, opts) | ||
result = Ecto.Repo.Schema.insert(module, adapter, struct, opts) | ||
|
||
case result do | ||
{:ok, resulting_struct} -> | ||
ExAudit.Tracking.track_change(module, adapter, :created, struct, resulting_struct, opts) | ||
_ -> | ||
:ok | ||
end | ||
|
||
result | ||
end | ||
|
||
def update(module, adapter, struct, opts) do | ||
Ecto.Repo.Schema.update(module, adapter, struct, opts) | ||
result = Ecto.Repo.Schema.update(module, adapter, struct, opts) | ||
|
||
case result do | ||
{:ok, resulting_struct} -> | ||
ExAudit.Tracking.track_change(module, adapter, :updated, struct, resulting_struct, opts) | ||
_ -> | ||
:ok | ||
end | ||
|
||
result | ||
end | ||
|
||
def insert_or_update(module, adapter, changeset, opts) do | ||
# TODO! | ||
Ecto.Repo.Schema.insert_or_update(module, adapter, changeset, opts) | ||
end | ||
|
||
def delete(module, adapter, struct, opts) do | ||
Ecto.Repo.Schema.delete(module, adapter, struct, opts) | ||
result = Ecto.Repo.Schema.delete(module, adapter, struct, opts) | ||
|
||
case result do | ||
{:ok, resulting_struct} -> | ||
ExAudit.Tracking.track_change(module, adapter, :deleted, struct, resulting_struct, opts) | ||
_ -> | ||
:ok | ||
end | ||
|
||
result | ||
end | ||
|
||
def insert!(module, adapter, struct, opts) do | ||
Ecto.Repo.Schema.insert!(module, adapter, struct, opts) | ||
result = Ecto.Repo.Schema.insert!(module, adapter, struct, opts) | ||
ExAudit.Tracking.track_change(module, adapter, :created, struct, result, opts) | ||
result | ||
end | ||
|
||
def update!(module, adapter, struct, opts) do | ||
Ecto.Repo.Schema.update!(module, adapter, struct, opts) | ||
result = Ecto.Repo.Schema.update!(module, adapter, struct, opts) | ||
ExAudit.Tracking.track_change(module, adapter, :updated, struct, result, opts) | ||
result | ||
end | ||
|
||
def insert_or_update!(module, adapter, changeset, opts) do | ||
# TODO | ||
Ecto.Repo.Schema.insert_or_update!(module, adapter, changeset, opts) | ||
end | ||
|
||
def delete!(module, adapter, struct, opts) do | ||
Ecto.Repo.Schema.delete!(module, adapter, struct, opts) | ||
result = Ecto.Repo.Schema.delete!(module, adapter, struct, opts) | ||
ExAudit.Tracking.track_change(module, adapter, :deleted, struct, result, opts) | ||
result | ||
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,20 @@ | ||
defmodule ExAudit.Type.Schema do | ||
@behaviour Ecto.Type | ||
|
||
@schemas Application.get_env(:ex_audit, :tracked_schemas) | ||
|
||
for schema <- @schemas do | ||
def cast(unquote(schema)), do: {:ok, unquote(schema)} | ||
def cast(unquote(schema.__schema__(:source))), do: {:ok, unquote(schema)} | ||
|
||
def load(unquote(schema.__schema__(:source))), do: {:ok, unquote(schema)} | ||
|
||
def dump(unquote(schema)), do: {:ok, unquote(schema.__schema__(:source))} | ||
end | ||
|
||
def cast(_), do: :error | ||
def load(_), do: :error | ||
def dump(_), do: :error | ||
|
||
def type, do: :string | ||
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,64 @@ | ||
defmodule ExAudit.Tracking do | ||
@version_schema Application.get_env(:ex_audit, :version_schema) | ||
@ignored_fields [:__meta__, :__struct__] | ||
|
||
def find_changes(action, struct_or_changeset, resulting_struct) do | ||
old = case {action, struct_or_changeset} do | ||
{:created, _} -> %{} | ||
{_, %Ecto.Changeset{data: struct}} -> struct | ||
{_, %{} = struct} -> struct | ||
{_, nil} -> %{} | ||
end | ||
|
||
new = case action do | ||
x when x in [:updated, :created] -> | ||
resulting_struct | ||
:deleted -> %{} | ||
end | ||
|
||
compare_versions(action, old, new) | ||
end | ||
|
||
def compare_versions(guessed_action, old, new) do | ||
schema = Map.get(old, :__struct__, Map.get(new, :__struct__)) | ||
|
||
assocs = schema.__schema__(:associations) | ||
|
||
ignored_fields = @ignored_fields ++ assocs | ||
|
||
patch = ExAudit.Diff.diff(Map.drop(old, ignored_fields), Map.drop(new, ignored_fields)) | ||
|
||
guessed_action = guessed_action || guess_action(old, new) | ||
|
||
params = %{ | ||
entity_id: Map.get(old, :id) || Map.get(new, :id), | ||
entity_schema: schema, | ||
patch: patch, | ||
action: guessed_action | ||
} | ||
|
||
[params] | ||
end | ||
|
||
def guess_action(%{id: id}, %{id: id}) when not is_nil(id), do: :updated | ||
def guess_action(%{}, %{id: id}) when not is_nil(id), do: :created | ||
def guess_action(%{id: id}, nil) when not is_nil(id), do: :deleted | ||
|
||
def track_change(module, adapter, action, changeset, resulting_struct, opts) do | ||
changes = find_changes(action, changeset, resulting_struct) | ||
|
||
now = DateTime.utc_now | ||
custom_fields = Keyword.get(opts, :ex_audit_custom, []) |> Enum.into(%{}) | ||
|
||
changes = Enum.map(changes, fn change -> | ||
change = Map.put(change, :recorded_at, now) | ||
Map.merge(change, custom_fields) | ||
end) | ||
|
||
case changes do | ||
[] -> :ok | ||
_ -> | ||
Ecto.Repo.Schema.insert_all(module, adapter, @version_schema, changes, opts) | ||
end | ||
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
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
ExAudit.Test.Repo.start_link() | ||
Ecto.Adapters.SQL.Sandbox.mode(ExAudit.Test.Repo, :auto) | ||
|
||
ExUnit.start() |