-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Postgres notify trigger for route updates
This adds triggers that send Postgres notifications for route_changes when rows are inserted, updated and deleted for content items and publish intents.
- Loading branch information
1 parent
9b5984f
commit 5ff28b0
Showing
3 changed files
with
116 additions
and
2 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
db/migrate/20241105135438_add_notify_trigger_for_route_changes.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class AddNotifyTriggerForRouteChanges < ActiveRecord::Migration[7.2] | ||
def up | ||
execute <<-SQL | ||
CREATE OR REPLACE FUNCTION notify_route_change() RETURNS trigger AS $$ | ||
BEGIN | ||
PERFORM pg_notify('route_changes', ''); | ||
RETURN OLD; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
SQL | ||
|
||
execute <<-SQL | ||
CREATE TRIGGER content_item_change_trigger | ||
AFTER INSERT OR UPDATE OR DELETE ON content_items | ||
FOR EACH ROW EXECUTE PROCEDURE notify_route_change(); | ||
SQL | ||
|
||
execute <<-SQL | ||
CREATE TRIGGER publish_intent_change_trigger | ||
AFTER INSERT OR UPDATE OR DELETE ON publish_intents | ||
FOR EACH ROW EXECUTE PROCEDURE notify_route_change(); | ||
SQL | ||
end | ||
|
||
def down | ||
execute <<-SQL | ||
DROP TRIGGER IF EXISTS content_item_change_trigger ON content_items; | ||
SQL | ||
|
||
execute <<-SQL | ||
DROP TRIGGER IF EXISTS publish_intent_change_trigger ON publish_intents; | ||
SQL | ||
|
||
execute <<-SQL | ||
DROP FUNCTION IF EXISTS notify_route_change(); | ||
SQL | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require "rails_helper" | ||
|
||
def postgres_listener(channel) | ||
queue = Queue.new | ||
|
||
Thread.new do | ||
conn = ActiveRecord::Base.lease_connection | ||
conn.execute("LISTEN #{channel}") | ||
loop do | ||
conn.raw_connection.wait_for_notify do |event, _id, _data| | ||
queue << event | ||
end | ||
end | ||
ensure | ||
conn.execute "UNLISTEN #{channel}" | ||
end | ||
|
||
# Wait for the thread to be ready to listen for nofitications | ||
sleep 0.1 | ||
|
||
queue | ||
end | ||
|
||
describe "Postgres trigger", type: :request, skip_db_cleaner: true do | ||
before do | ||
# We need to use truncation instead of transaction to ensure the changes are commited and the trigger is fired | ||
DatabaseCleaner.clean | ||
DatabaseCleaner.strategy = :truncation | ||
end | ||
|
||
it "sends a notification when content item created" do | ||
listener = postgres_listener("route_changes") | ||
create(:content_item) | ||
|
||
expect(listener.pop).to eq("route_changes") | ||
end | ||
|
||
it "sends a notification when content item updated" do | ||
content_item = create(:content_item) | ||
|
||
listener = postgres_listener("route_changes") | ||
content_item.update!(base_path: "/foo") | ||
|
||
expect(listener.pop).to eq("route_changes") | ||
end | ||
|
||
it "sends a notification when content item destroyed" do | ||
content_item = create(:content_item) | ||
listener = postgres_listener("route_changes") | ||
content_item.destroy! | ||
|
||
expect(listener.pop).to eq("route_changes") | ||
end | ||
|
||
it "sends a notification when publish intent created" do | ||
listener = postgres_listener("route_changes") | ||
create(:publish_intent) | ||
|
||
expect(listener.pop).to eq("route_changes") | ||
end | ||
|
||
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) | ||
|
||
expect(listener.pop).to eq("route_changes") | ||
end | ||
|
||
it "sends a notification when publish intent destroyed" do | ||
publish_intent = create(:publish_intent) | ||
listener = postgres_listener("route_changes") | ||
publish_intent.destroy! | ||
|
||
expect(listener.pop).to eq("route_changes") | ||
end | ||
end |