I want to create a simple trigger in mysql using liquibase. The following script works directly from mysql:
delimiter $$
CREATE TRIGGER myTrigger
BEFORE INSERT ON myTable FOR EACH ROW
BEGIN
IF(NEW.my_timestamp IS NULL) THEN
SET NEW.my_timestamp = now();
END IF;
END$$
delimiter ;
So, I want to create a changeset for liquibase to use that can apply this trigger using the update command, and will also create a suitable sql script when using the updateSQL command.
I have tried a variety of options in the changeset including splitStatements and endDelimiter, but have only been able to get something that works either with the update command or with the updateSQL command. Not with both.
here's a sample change set using formatted sql which works fine when I use the update command, but does not create suitable sql when I use the updateSQL command
-- liquibase formatted sql
-- changeset pcoates33:trigger-1 splitStatements:false
CREATE TRIGGER myTrigger
BEFORE INSERT ON myTable FOR EACH ROW
BEGIN
IF(NEW.my_timestamp IS NULL) THEN
SET NEW.my_timestamp = now();
END IF;
END
-- rollback DROP TRIGGER IF EXISTS myTrigger;
and here's one that works how I want for updateSQL, but fails for update:
-- liquibase formatted sql
-- changeset pcoates33:trigger-1 splitStatements:false
delimiter $$
CREATE TRIGGER myTrigger
BEFORE INSERT ON myTable FOR EACH ROW
BEGIN
IF(NEW.my_timestamp IS NULL) THEN
SET NEW.my_timestamp = now();
END IF;
END$$
delimiter ;
-- rollback DROP TRIGGER IF EXISTS myTrigger;
The basic problem is
- The mysql script needs both the delimiter $$ and delimiter ; in it
- The jdbc call made by liquibase will fail if it has the delimiter $$ at the start
DELIMITER
command is a command of CLI. liquibase may not support it... – Akina