-
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.
Merge pull request #1361 from alphagov/more-selective-router-notifica…
…tiosn Notify Router only on specific column updates
- Loading branch information
Showing
3 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
db/migrate/20241209132444_update_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,47 @@ | ||
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 TG_TABLE_NAME = 'content_items' THEN | ||
-- Specific column checks for the content_items table | ||
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; | ||
ELSIF TG_TABLE_NAME = 'publish_intents' THEN | ||
-- Specific column checks for publish_intents table | ||
IF (NEW.routes IS DISTINCT FROM OLD.routes OR | ||
NEW.rendering_app IS DISTINCT FROM OLD.rendering_app) THEN | ||
PERFORM pg_notify('route_changes', ''); | ||
END IF; | ||
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 |
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