Skip to content

Commit

Permalink
Notify Router only on specific column updates
Browse files Browse the repository at this point in the history
Currently, we send a notification to the Router to update route tables
whenever there is any change to the content_items (or publish_intents)
tables. However, the Router only needs to update the route table when
changes affect specific columns that determine the routes.

This change should reduce unnecessary reloading.
  • Loading branch information
theseanything committed Dec 9, 2024
1 parent ef5074d commit 36b17d5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class UpdateNotifyTriggerForRouteChanges < ActiveRecord::Migration[7.2]
def up
execute <<-SQL
CREATE OR REPLACE FUNCTION notify_route_change() RETURNS trigger AS $$
BEGIN
-- Trigger on INSERT or DELETE
IF (TG_OP = 'INSERT' OR TG_OP = 'DELETE') THEN
PERFORM pg_notify('route_changes', '');
RETURN COALESCE(NEW, OLD);
END IF;
-- Trigger on UPDATE for specific columns
IF (TG_OP = 'UPDATE') THEN
IF (NEW.routes IS DISTINCT FROM OLD.routes OR
NEW.redirects IS DISTINCT FROM OLD.redirects OR
NEW.schema_name IS DISTINCT FROM OLD.schema_name OR
NEW.rendering_app IS DISTINCT FROM OLD.rendering_app) THEN
PERFORM pg_notify('route_changes', '');
END IF;
END IF;
RETURN COALESCE(NEW, OLD);
END;
$$ LANGUAGE plpgsql;
SQL
end

def down
execute <<-SQL
CREATE OR REPLACE FUNCTION notify_route_change() RETURNS trigger AS $$
BEGIN
PERFORM pg_notify('route_changes', '');
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
SQL
end
end
20 changes: 18 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,23 @@ CREATE FUNCTION public.notify_route_change() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM pg_notify('route_changes', '');
RETURN OLD;
-- Trigger on INSERT or DELETE
IF (TG_OP = 'INSERT' OR TG_OP = 'DELETE') THEN
PERFORM pg_notify('route_changes', '');
RETURN COALESCE(NEW, OLD);
END IF;

-- Trigger on UPDATE for specific columns
IF (TG_OP = 'UPDATE') THEN
IF (NEW.routes IS DISTINCT FROM OLD.routes OR
NEW.redirects IS DISTINCT FROM OLD.redirects OR
NEW.schema_name IS DISTINCT FROM OLD.schema_name OR
NEW.rendering_app IS DISTINCT FROM OLD.rendering_app) THEN
PERFORM pg_notify('route_changes', '');
END IF;
END IF;

RETURN COALESCE(NEW, OLD);
END;
$$;

Expand Down Expand Up @@ -437,6 +452,7 @@ CREATE TRIGGER publish_intent_change_trigger AFTER INSERT OR DELETE OR UPDATE ON
SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
('20241209132444'),
('20241105135438'),
('20240312132747'),
('20240220151333'),
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/notify_route_change_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def postgres_listener(channel)
content_item = create(:content_item)

listener = postgres_listener("route_changes")
content_item.update!(base_path: "/foo")
content_item.update!(schema_name: "gone")

expect(listener.pop).to eq("route_changes")
end
Expand All @@ -62,7 +62,7 @@ def postgres_listener(channel)
it "sends a notification when publish intent updated" do
publish_intent = create(:publish_intent)
listener = postgres_listener("route_changes")
publish_intent.update!(publish_time: 10.minutes.from_now)
publish_intent.update!(rendering_app: "updated")

expect(listener.pop).to eq("route_changes")
end
Expand Down

0 comments on commit 36b17d5

Please sign in to comment.