Skip to content

Commit

Permalink
feat: Make a public-facing function for details
Browse files Browse the repository at this point in the history
To implement #17
  • Loading branch information
cheerfulstoic committed Aug 16, 2024
1 parent eff9e32 commit eb052dc
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/ecto_watch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions test/ecto_watch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit eb052dc

Please sign in to comment.