Last active
April 14, 2022 20:50
-
-
Save PandaWhoCodes/ea6693333c7c565714324ec1c186f962 to your computer and use it in GitHub Desktop.
lock tables from being altered in postgres
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
CREATE OR REPLACE FUNCTION schema.ddl_issue() | |
RETURNS event_trigger | |
LANGUAGE plpgsql | |
AS $function$ | |
DECLARE r RECORD; | |
BEGIN | |
FOR r IN SELECT * FROM pg_event_trigger_ddl_commands() LOOP | |
IF ( r.objid::regclass::text = 'schema.tablename' ) | |
THEN | |
RAISE EXCEPTION 'You are not allowed to change %', r.object_identity; | |
END IF; | |
END LOOP; | |
END; | |
$function$ | |
; | |
-- event trigger | |
CREATE EVENT TRIGGER no_ddl_allowed | |
ON ddl_command_end WHEN TAG IN ('ALTER TABLE') | |
EXECUTE PROCEDURE schema.ddl_issue(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment