J
J
johnmohnachev2017-03-31 16:51:13
MySQL
johnmohnachev, 2017-03-31 16:51:13

How to write mysql trigger to database via Flyway?

There is a sql file containing:

DROP TRIGGER IF EXISTS update_run_result_trigger_update_runner_and_user_names;
CREATE TRIGGER update_run_result_trigger_update_runner_and_user_names
BEFORE UPDATE ON `Run_Result`
FOR EACH ROW
  BEGIN
    IF NEW.user_id != OLD.user_id AND NEW.user_id IS NOT NULL THEN
      SELECT first_name, last_name, nickname INTO @first_name, @last_name, @nickname FROM User WHERE id = NEW.user_id;
      SET NEW.user_name = RTRIM(LTRIM(CONCAT(IFNULL(@first_name, ''), ' ', IFNULL(@last_name, ''), ' ', IFNULL(@nickname, ''))));
    END IF;
    IF NEW.runner_id != OLD.runner_id AND NEW.runner_id IS NOT NULL THEN
      SELECT first_name, last_name INTO @first_name, @last_name FROM Runner WHERE id = NEW.runner_id;
      SET NEW.runner_name = RTRIM(LTRIM(CONCAT(IFNULL(@first_name, ''), ' ', IFNULL(@last_name, ''))));
    END IF;
  END;

If this code is inserted through the Database plugin for IDEA, the sql data is processed without problems.
uyO2UuQZsXrXryleAfgRuDNJor6wJa.png
But if I start the migration process via FlyWay, I get an error:
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 '' at line 6

jO71jffqj278Kg2QWlCs4H6ebdjZ2t.png
log file

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
johnmohnachev, 2017-04-01
@johnmohnachev

I removed all tabs and spaces, in general I wrote it in one line, it worked

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question