From 02348a83e29acc1f6752c48989e114db87a56de0 Mon Sep 17 00:00:00 2001 From: dylanseago Date: Wed, 12 Feb 2025 21:52:09 -0500 Subject: [PATCH] support prefixed tables --- config/test.exs | 1 + example/private_user.ex | 20 ++++++++++++++ lib/repo/schema_type.ex | 12 +++++++-- .../20171018124842_initial_tables.exs | 9 +++++++ test/schema_type_test.exs | 26 +++++++++++++++++++ 5 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 example/private_user.ex create mode 100644 test/schema_type_test.exs diff --git a/config/test.exs b/config/test.exs index abd037f..398592d 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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 diff --git a/example/private_user.ex b/example/private_user.ex new file mode 100644 index 0000000..d698919 --- /dev/null +++ b/example/private_user.ex @@ -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 diff --git a/lib/repo/schema_type.ex b/lib/repo/schema_type.ex index 1269c81..94ba761 100644 --- a/lib/repo/schema_type.ex +++ b/lib/repo/schema_type.ex @@ -25,7 +25,7 @@ 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 @@ -33,10 +33,18 @@ defmodule ExAudit.Type.Schema do 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 diff --git a/priv/repo/migrations/20171018124842_initial_tables.exs b/priv/repo/migrations/20171018124842_initial_tables.exs index 0f122ef..5e4f437 100644 --- a/priv/repo/migrations/20171018124842_initial_tables.exs +++ b/priv/repo/migrations/20171018124842_initial_tables.exs @@ -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) diff --git a/test/schema_type_test.exs b/test/schema_type_test.exs new file mode 100644 index 0000000..e03083c --- /dev/null +++ b/test/schema_type_test.exs @@ -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