From eb052dc5770f2c87f913cd6563e9b361491fa183 Mon Sep 17 00:00:00 2001 From: Brian Underwood Date: Fri, 16 Aug 2024 22:00:55 +0200 Subject: [PATCH] feat: Make a public-facing function for details To implement https://github.com/cheerfulstoic/ecto_watch/issues/17 --- lib/ecto_watch.ex | 23 ++++++++++++++ test/ecto_watch_test.exs | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/lib/ecto_watch.ex b/lib/ecto_watch.ex index bae75f5..9ca1832 100644 --- a/lib/ecto_watch.ex +++ b/lib/ecto_watch.ex @@ -156,6 +156,29 @@ defmodule EctoWatch do end end + @doc """ + Returns details about a watcher for reflection purposes + + For example if you need to know what the function/triggers are in the database. + """ + @spec details(watcher_identifier()) :: %{ + repo_mod: module(), + schema_definition: %EctoWatch.Options.WatcherOptions.SchemaDefinition{ + schema_prefix: binary(), + table_name: binary(), + primary_key: binary(), + columns: [atom()], + association_columns: [atom()], + label: term() + }, + function_name: binary(), + trigger_name: binary(), + notify_channel: binary() + } + def details(watcher_identifier) do + WatcherServer.details(watcher_identifier) + end + defp validate_identifier({schema_mod, update_type}) when is_atom(schema_mod) and is_atom(update_type) do cond do diff --git a/test/ecto_watch_test.exs b/test/ecto_watch_test.exs index d81983f..19dde02 100644 --- a/test/ecto_watch_test.exs +++ b/test/ecto_watch_test.exs @@ -1267,4 +1267,70 @@ defmodule EctoWatchTest do refute_receive {{Thing, :deleted}, %{id: ^already_existing_id2}} end end + + describe "details" do + test "for standard and labeled" do + start_supervised!( + {EctoWatch, + repo: TestRepo, + pub_sub: TestPubSub, + watchers: [ + {Thing, :updated, label: :thing_custom_event}, + {Thing, :deleted} + ]} + ) + + details = EctoWatch.details({Thing, :deleted}) + + assert details.repo_mod == TestRepo + + assert details.schema_definition == %EctoWatch.Options.WatcherOptions.SchemaDefinition{ + schema_prefix: "public", + table_name: "things", + primary_key: :id, + columns: [ + :id, + :the_string, + :the_integer, + :the_float, + :parent_thing_id, + :other_parent_thing_id, + :inserted_at, + :updated_at + ], + association_columns: [:parent_thing_id, :other_parent_thing_id], + label: EctoWatchTest.Thing + } + + assert details.function_name == "ew_deleted_for_ectowatchtest_thing_func" + assert details.trigger_name == "ew_deleted_for_ectowatchtest_thing_trigger" + assert details.notify_channel == "ew_deleted_for_ectowatchtest_thing" + + details = EctoWatch.details(:thing_custom_event) + + assert details.repo_mod == TestRepo + + assert details.schema_definition == %EctoWatch.Options.WatcherOptions.SchemaDefinition{ + schema_prefix: "public", + table_name: "things", + primary_key: :id, + columns: [ + :id, + :the_string, + :the_integer, + :the_float, + :parent_thing_id, + :other_parent_thing_id, + :inserted_at, + :updated_at + ], + association_columns: [:parent_thing_id, :other_parent_thing_id], + label: EctoWatchTest.Thing + } + + assert details.function_name == "ew_for_thing_custom_event_func" + assert details.trigger_name == "ew_for_thing_custom_event_trigger" + assert details.notify_channel == "ew_for_thing_custom_event" + end + end end