Skip to content

Instantly share code, notes, and snippets.

@jpstacey
Created August 9, 2011 10:29
Show Gist options
  • Save jpstacey/1133723 to your computer and use it in GitHub Desktop.
Save jpstacey/1133723 to your computer and use it in GitHub Desktop.
Causing MySQL to raise an exception from a trigger
-- MySQL does not implement raising exceptions within triggers. Here's a hacky model for doing so.
-- This permits unwanted conditions to bubble up to the application layer, to be dealt with
-- Only use this during debugging. Don't rely on this in production code as it's a hack.
DELIMITER $$
-- Procedure guaranteed to raise an exception
CREATE PROCEDURE die_horribly() SQL SECURITY INVOKER DETERMINISTIC
BEGIN
INSERT INTO does_not_exist (nid) VALUES (NULL);
END;
$$
-- Procedure guaranteed to raise an exception
CREATE TRIGGER call_die BEFORE UPDATE ON node_revisions
FOR EACH ROW
BEGIN
IF NEW.status = 0 THEN CALL die_horribly();
END IF;
END;
$$
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment