Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ config :ex_audit,
version_schema: ExAudit.Test.Version,
tracked_schemas: [
ExAudit.Test.User,
ExAudit.Test.PrivateUser,
ExAudit.Test.BlogPost,
ExAudit.Test.BlogPost.Section,
ExAudit.Test.Comment
Expand Down
20 changes: 20 additions & 0 deletions example/private_user.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule ExAudit.Test.PrivateUser do
use Ecto.Schema
import Ecto.Changeset

@derive {ExAudit.Tracker, except: [:transient_field]}

@schema_prefix "private"
schema "users" do
field :email, :string
field :name, :string
field :birthday, :date

timestamps()
end

def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:email, :name, :birthday])
end
end
12 changes: 10 additions & 2 deletions lib/repo/schema_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,26 @@ defmodule ExAudit.Type.Schema do

def dump(schema) do
case Enum.member?(schemas(), schema) do
true -> {:ok, schema.__schema__(:source)}
true -> {:ok, schema_name(schema)}
_ -> :error
end
end

defp get_schema_by_table(table) do
schemas()
|> Enum.find(fn schema ->
schema.__schema__(:source) == table
schema_name(schema) == table
end)
end

defp schema_name(schema) do
if prefix = schema.__schema__(:prefix) do
prefix <> "." <> schema.__schema__(:source)
else
schema.__schema__(:source)
end
end

def type, do: :string

defp schemas do
Expand Down
9 changes: 9 additions & 0 deletions priv/repo/migrations/20171018124842_initial_tables.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ defmodule ExAudit.Test.Repo.Migrations.InitialTables do
timestamps(type: :utc_datetime)
end

execute("CREATE SCHEMA private")

create table(:users, prefix: "private") do
add :name, :string
add :email, :string

timestamps(type: :utc_datetime)
end

create table(:blog_post) do
add :title, :string
add :author_id, references(:users, on_update: :update_all, on_delete: :delete_all)
Expand Down
26 changes: 26 additions & 0 deletions test/schema_type_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule SchemaTypeTest do
use ExUnit.Case

alias ExAudit.Type.Schema, as: SchemaType
alias ExAudit.Test.{User, PrivateUser}

describe "load/1" do
test "should load unprefixed schema" do
assert SchemaType.load("users") == {:ok, User}
end

test "should load prefixed schema" do
assert SchemaType.load("private.users") == {:ok, PrivateUser}
end
end

describe "dump/1" do
test "should dump unprefixed schema" do
assert SchemaType.dump(User) == {:ok, "users"}
end

test "should dump prefixed schema" do
assert SchemaType.dump(PrivateUser) == {:ok, "private.users"}
end
end
end