Skip to content

Commit dc9b009

Browse files
committed
support prefixed tables
1 parent e3822f0 commit dc9b009

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
elixir 1.15.2-otp-26
2-
erlang 26.0.2
2+
erlang 27.0.1
33
nodejs 15.10.0

config/test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ config :ex_audit,
1717
ExAudit.Test.Repo => %{
1818
version_schema: ExAudit.Test.Version,
1919
tracked_schemas: [
20+
ExAudit.Test.PrivateUser,
2021
ExAudit.Test.User,
2122
ExAudit.Test.BlogPost,
2223
ExAudit.Test.BlogPost.Section,

example/private_user.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defmodule ExAudit.Test.PrivateUser do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
@derive {ExAudit.Tracker, except: [:transient_field]}
6+
7+
@schema_prefix "private"
8+
schema "users" do
9+
field :email, :string
10+
field :name, :string
11+
field :birthday, :date
12+
13+
timestamps()
14+
end
15+
16+
def changeset(struct, params \\ %{}) do
17+
struct
18+
|> cast(params, [:email, :name, :birthday])
19+
end
20+
end

lib/repo/schema_type.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,26 @@ defmodule ExAudit.Type.Schema do
2525

2626
def dump(schema) do
2727
case Enum.member?(schemas(), schema) do
28-
true -> {:ok, schema.__schema__(:source)}
28+
true -> {:ok, schema_name(schema)}
2929
_ -> :error
3030
end
3131
end
3232

3333
defp get_schema_by_table(table) do
3434
schemas()
3535
|> Enum.find(fn schema ->
36-
schema.__schema__(:source) == table
36+
schema_name(schema) == table
3737
end)
3838
end
3939

40+
defp schema_name(schema) do
41+
if prefix = schema.__schema__(:prefix) do
42+
prefix <> "." <> schema.__schema__(:source)
43+
else
44+
schema.__schema__(:source)
45+
end
46+
end
47+
4048
def type, do: :string
4149

4250
defp schemas() do

priv/repo/migrations/20171018124842_initial_tables.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ defmodule ExAudit.Test.Repo.Migrations.InitialTables do
99
timestamps(type: :utc_datetime)
1010
end
1111

12+
execute("CREATE SCHEMA private")
13+
14+
create table(:users, prefix: "private") do
15+
add :name, :string
16+
add :email, :string
17+
18+
timestamps(type: :utc_datetime)
19+
end
20+
1221
create table(:blog_post) do
1322
add :title, :string
1423
add :author_id, references(:users, on_update: :update_all, on_delete: :delete_all)

test/schema_type_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule SchemaTypeTest do
2+
use ExUnit.Case
3+
4+
alias ExAudit.Type.Schema, as: SchemaType
5+
alias ExAudit.Test.{User, PrivateUser}
6+
7+
describe "load/1" do
8+
test "should load unprefixed schema" do
9+
assert SchemaType.load("users") == {:ok, User}
10+
end
11+
12+
test "should load prefixed schema" do
13+
assert SchemaType.load("private.users") == {:ok, PrivateUser}
14+
end
15+
end
16+
17+
describe "dump/1" do
18+
test "should dump unprefixed schema" do
19+
assert SchemaType.dump(User) == {:ok, "users"}
20+
end
21+
22+
test "should dump prefixed schema" do
23+
assert SchemaType.dump(PrivateUser) == {:ok, "private.users"}
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)