Coder Social home page Coder Social logo

Comments (3)

 avatar commented on June 1, 2024 3

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 ;

from sql-migrate.

kjda avatar kjda commented on June 1, 2024

same here!

from sql-migrate.

neoramos avatar neoramos commented on June 1, 2024

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 ;`

from sql-migrate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.