Skip to content

Commit b0c892a

Browse files
committed
fix: Partition check in trigger query + tests
1 parent 5639bcd commit b0c892a

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

lib/ecto_watch/watcher_trigger_validator.ex

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,14 @@ defmodule EctoWatch.WatcherTriggerValidator do
148148
"""
149149
SELECT trigger_name, event_object_schema, event_object_table
150150
FROM information_schema.triggers
151-
WHERE trigger_name LIKE 'ew_%'
152-
AND EXISTS (
153-
SELECT 1
154-
FROM pg_class c
155-
JOIN pg_namespace n ON c.relnamespace = n.oid
156-
WHERE n.nspname = event_object_schema
157-
AND c.relname = event_object_table
158-
AND NOT EXISTS (
159-
SELECT 1
160-
FROM pg_inherits inh
161-
WHERE inh.inhrelid = c.oid
162-
)
163-
)
151+
JOIN pg_class
152+
ON pg_class.relname = event_object_table
153+
JOIN pg_namespace
154+
ON pg_class.relnamespace = pg_namespace.oid AND
155+
pg_namespace.nspname = event_object_schema
156+
LEFT JOIN pg_inherits
157+
ON pg_inherits.inhrelid = pg_class.oid
158+
WHERE trigger_name LIKE 'ew_%' AND pg_inherits.inhrelid IS NULL
164159
"""
165160
)
166161
|> Enum.map(fn [name, table_schema, table_name] ->

test/ecto_watch_test.exs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ defmodule EctoWatchTest do
730730
end
731731
end
732732

733-
describe "trigger cleanup" do
733+
describe "trigger validation" do
734734
setup do
735735
Ecto.Adapters.SQL.query!(
736736
TestRepo,
@@ -772,7 +772,7 @@ defmodule EctoWatchTest do
772772
TestRepo,
773773
"""
774774
CREATE TRIGGER ew_partition_level_trigger
775-
AFTER UPDATE ON \"public\".\"partitioned_part1\" FOR EACH ROW
775+
AFTER UPDATE ON \"public\".\"partitioned_root\" FOR EACH ROW
776776
EXECUTE PROCEDURE \"public\".non_ecto_watch_func();
777777
""",
778778
[]
@@ -819,15 +819,17 @@ defmodule EctoWatchTest do
819819
Process.sleep(2_000)
820820
end)
821821

822+
IO.puts(log)
823+
822824
assert log =~
823-
~r/Found the following extra EctoWatch triggers:\n\n"ew_some_weird_trigger" in the table "public"\."things"\n\n\.\.\.but they were not specified in the watcher options/
825+
~r/Found the following extra EctoWatch triggers:\n\n"ew_partition_level_trigger" in the table "public"\."partitioned_root"\n"ew_some_weird_trigger" in the table "public"\."things"\n\n\.\.\.but they were not specified in the watcher options/
824826

825827
assert log =~
826828
~r/Found the following extra EctoWatch functions:\n\n"ew_some_weird_func" in the schema "public"/
827829

828830
refute log =~ ~r/non_ecto_watch_trigger/
829831
refute log =~ ~r/non_ecto_watch_func/
830-
refute log =~ ~r/ew_partition_level_trigger/
832+
refute log =~ ~r/partitioned_part1/
831833
end
832834

833835
test "actual cleanup" do
@@ -860,7 +862,7 @@ defmodule EctoWatchTest do
860862

861863
assert "ew_some_weird_trigger" not in values
862864
assert "non_ecto_watch_trigger" in values
863-
assert "ew_partition_level_trigger" in values
865+
assert "ew_partition_level_trigger" not in values
864866

865867
%Postgrex.Result{rows: rows} =
866868
Ecto.Adapters.SQL.query!(

0 commit comments

Comments
 (0)