-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
191 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule Sanbase.Version do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "versions" do | ||
# The patch in Erlang External Term Format | ||
field(:patch, ExAudit.Type.Patch) | ||
|
||
# supports UUID and other types as well | ||
field(:entity_id, :integer) | ||
|
||
# name of the table the entity is in | ||
field(:entity_schema, ExAudit.Type.Schema) | ||
|
||
# type of the action that has happened to the entity (created, updated, deleted) | ||
field(:action, ExAudit.Type.Action) | ||
|
||
# when has this happened | ||
field(:recorded_at, :utc_datetime) | ||
|
||
# was this change part of a rollback? | ||
field(:rollback, :boolean, default: false) | ||
|
||
# custom fields | ||
belongs_to(:user, Sanbase.Accounts.User) | ||
end | ||
|
||
def changeset(struct, params \\ %{}) do | ||
struct | ||
|> cast(params, [:patch, :entity_id, :entity_schema, :action, :recorded_at, :rollback]) | ||
# custom fields | ||
|> cast(params, [:user_id]) | ||
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,47 @@ | ||
defmodule SanbaseWeb.GenericAdmin.Version do | ||
import Ecto.Query | ||
def schema_module, do: Sanbase.Version | ||
|
||
def resource() do | ||
%{ | ||
actions: [:show], | ||
preloads: [:user], | ||
index_fields: [ | ||
:id, | ||
:entity_id, | ||
:entity_schema, | ||
:action, | ||
:recorded_at, | ||
:rollback | ||
], | ||
fields_override: %{ | ||
patch: %{ | ||
value_modifier: &format_patch/1 | ||
} | ||
} | ||
} | ||
end | ||
|
||
defp format_patch(%{patch: patch}) when is_map(patch) do | ||
patch | ||
|> Enum.map_join("\n", fn {field, change} -> | ||
format_change(field, change) | ||
end) | ||
end | ||
|
||
defp format_patch(_), do: "" | ||
|
||
defp format_change(field, {:changed, {:primitive_change, old_val, new_val}}) do | ||
"#{field}: #{inspect(old_val)} → #{inspect(new_val)}" | ||
end | ||
|
||
defp format_change(field, {:changed, nested}) when is_map(nested) do | ||
nested_changes = | ||
nested | ||
|> Enum.map_join(", ", fn {k, v} -> format_change(k, v) end) | ||
|
||
"#{field}: {#{nested_changes}}" | ||
end | ||
|
||
defp format_change(field, other), do: "#{field}: #{inspect(other)}" | ||
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
32 changes: 32 additions & 0 deletions
32
priv/repo/migrations/20241030141825_add_ex_audit_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,32 @@ | ||
defmodule Sanbase.Repo.Migrations.AddExAuditVersions do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:versions) do | ||
# The patch in Erlang External Term Format | ||
add(:patch, :binary) | ||
|
||
# supports UUID and other types as well | ||
add(:entity_id, :integer) | ||
|
||
# name of the table the entity is in | ||
add(:entity_schema, :string) | ||
|
||
# type of the action that has happened to the entity (created, updated, deleted) | ||
add(:action, :string) | ||
|
||
# when has this happened | ||
add(:recorded_at, :utc_datetime) | ||
|
||
# was this change part of a rollback? | ||
add(:rollback, :boolean, default: false) | ||
|
||
# optional fields that you can define yourself | ||
# for example, it's a good idea to track who did the change | ||
add(:user_id, references(:users, on_update: :update_all, on_delete: :nilify_all)) | ||
end | ||
|
||
# create this if you are going to have more than a hundred of thousands of versions | ||
create(index(:versions, [:entity_schema, :entity_id])) | ||
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