- Install Trigger functions
- On the table whose rows will trigger these functions, install the trigger and select the trigger function.
Last active
December 12, 2015 08:08
-
-
Save allanlei/4742030 to your computer and use it in GitHub Desktop.
Postgresql triggers to handle CRUD with rows and schemas
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
-- Trigger: auto_create_schema on landlord_tenant | |
-- DROP TRIGGER auto_create_schema ON landlord_tenant; | |
CREATE TRIGGER auto_create_schema | |
AFTER INSERT | |
ON landlord_tenant | |
FOR EACH ROW | |
EXECUTE PROCEDURE create_schema(); |
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
-- Trigger: auto_drop_schema on landlord_tenant | |
-- DROP TRIGGER auto_drop_schema ON landlord_tenant; | |
CREATE TRIGGER auto_drop_schema | |
AFTER DELETE | |
ON landlord_tenant | |
FOR EACH ROW | |
EXECUTE PROCEDURE drop_schema(); |
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
-- Trigger: auto_rename_schema on landlord_tenant | |
-- DROP TRIGGER auto_rename_schema ON landlord_tenant; | |
CREATE TRIGGER auto_rename_schema | |
AFTER UPDATE | |
ON landlord_tenant | |
FOR EACH ROW | |
EXECUTE PROCEDURE rename_schema(); |
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
-- Function: create_schema() | |
-- DROP FUNCTION create_schema(); | |
CREATE OR REPLACE FUNCTION create_schema() | |
RETURNS trigger AS | |
$BODY$ | |
BEGIN | |
EXECUTE FORMAT('DROP SCHEMA IF EXISTS %I;', NEW.name); | |
EXECUTE FORMAT('CREATE SCHEMA %I;', NEW.name); | |
RETURN NEW; | |
END; | |
$BODY$ | |
LANGUAGE plpgsql VOLATILE | |
COST 100; |
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
-- Function: delete_schema() | |
-- DROP FUNCTION delete_schema(); | |
CREATE OR REPLACE FUNCTION delete_schema() | |
RETURNS trigger AS | |
$BODY$ | |
BEGIN | |
EXECUTE FORMAT('DROP SCHEMA IF EXISTS %I CASCADE;', OLD.name); | |
RETURN OLD; | |
END; | |
$BODY$ | |
LANGUAGE plpgsql VOLATILE | |
COST 100; |
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
-- Function: rename_schema() | |
-- DROP FUNCTION rename_schema(); | |
CREATE OR REPLACE FUNCTION rename_schema() | |
RETURNS trigger AS | |
$BODY$ | |
BEGIN | |
EXECUTE FORMAT('ALTER SCHEMA %I RENAME TO %I;', OLD.name, NEW.name); | |
RETURN NEW; | |
END; | |
$BODY$ | |
LANGUAGE plpgsql VOLATILE | |
COST 100; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment