Last active
December 19, 2015 18:49
-
-
Save jdmullin/6001598 to your computer and use it in GitHub Desktop.
Pattern for idempotent sql alter scripts using temporary stored procedures to implement conditional logic.
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
delimiter // | |
drop procedure if exists trySomething// | |
create procedure trySomething(in _param1 int, in _param2 int) | |
begin | |
-- Condition could be a count, a boolean, whatever. In this case assuming might be running a "select count(*)" | |
-- to determine if a certain row has already been inserted. | |
declare _condition int default null; | |
set _condition = (<SOME_SELECT_THAT_SIGNIFIES_IF_YOUVE_ALREADY_RUN_THIS_SCRIPT>); | |
if _condition = 0 then | |
select 'have not run before, doing the work'; | |
-- your code here | |
else | |
select 'nothing to do, been here before'; | |
end if; | |
end// | |
delimiter ; | |
call trySomething(<importantParam1>, <importantParam2>); | |
-- cleanup temp proc(s) | |
drop procedure if exists trySomething; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment