Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not create trigger on MySQL #71

Open
k4jt opened this issue Apr 7, 2017 · 3 comments
Open

Could not create trigger on MySQL #71

k4jt opened this issue Apr 7, 2017 · 3 comments

Comments

@k4jt
Copy link

k4jt commented Apr 7, 2017

`
-- +migrate Up

CREATE TABLE device_groups ...
CREATE TABLE devices ...

-- +migrate StatementBegin
delimiter |

CREATE TRIGGER update_devicegroup_devices_on_insert AFTER INSERT ON devices
FOR EACH ROW
BEGIN
UPDATE device_groups SET devices = (SELECT COUNT(*) FROM devices WHERE device_group_id = NEW.device_group_id) WHERE id = NEW.device_group_id;
END
|

delimiter ;
-- +migrate StatementEnd

-- +migrate Down
DROP TABLE IF EXISTS devices CASCADE;
DROP TABLE IF EXISTS device_groups CASCADE;
`

Using bindata

Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'delimiter |

CREATE TRIGGER update_devicegroup_devices_on_insert AFTER INSERT ON' at line 2 handling 003_reinit.sql

@kjda
Copy link

kjda commented May 6, 2017

same here!

@neoramos
Copy link

neoramos commented Mar 19, 2020

Hello,
Thank you for this great tool.
I was wondering if anyone has found a solution for migrating triggers.
This works from Sequel Pro IDE on a MySQL:

DROP TRIGGER IF EXISTS surveys.Survey_BEFORE_UPDATE;

DELIMITER $$
USE `surveys`$$
CREATE DEFINER = CURRENT_USER TRIGGER `surveys`.`Survey_BEFORE_UPDATE` BEFORE UPDATE ON `Survey` FOR EACH ROW
BEGIN
	IF NEW.status != OLD.status OR 
       NEW.surveyGroupID != OLD.surveyGroupID OR 
       NEW.surveyMonth != OLD.surveyMonth OR 
       NEW.surveyYear != OLD.surveyYear THEN
		INSERT INTO SurveyHistory (sequenceID,surveyGroupID,surveyYear,surveyMonth,`status`,statusAt,sentAt,lastActiveAt)
		VALUES (OLD.id,OLD.surveyGroupID,OLD.surveyYear,OLD.surveyMonth,OLD.`status`,OLD.statusAt,OLD.sentAt,OLD.lastActiveAt);
	END IF;
END$$
DELIMITER ;`

@ghost
Copy link

ghost commented Jul 29, 2021

As reference for others searching how to migrate MySQL triggers and landing on this issue page.

DELIMITER is a command only understood by MySQL client. You need to get rid of it in order to have sql-migrate sucessfully deploy the migration scripts.

Procedure:

  • First, make sure you have multiStatements enabled in your DSN. For example, in dbconfig.yml you would define a datasource like this: datasource: 'user:pass@tcp(db_host:db_port)/my_db?parseTime=true&multiStatements=true'.

  • Second, remove all DELIMITER commands from your sql code. For example, code looking like this:

DELIMITER $$
USE `my_db`$$
CREATE DEFINER = CURRENT_USER TRIGGER `my_db`.`my_table_BEFORE_INSERT` 
BEFORE INSERT ON `my_table` 
FOR EACH ROW
BEGIN
	IF NEW.blah IS NULL THEN
		SET NEW.blah = '';
	END IF;
END$$

must be cleaned-up to look like this:

-- +migrate StatementBegin
USE `my_db`;
CREATE DEFINER = CURRENT_USER TRIGGER `my_db`.`my_table_BEFORE_INSERT` 
BEFORE INSERT ON `my_table` 
FOR EACH ROW
BEGIN
	IF NEW.blah IS NULL THEN
		SET NEW.blah = '';
	END IF;
END;
-- +migrate StatementEnd

Note that there is no DELIMITER $$ present and all occurrences of $$ were replaced with ;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants