2

I'm trying to execute (migrate) SQL scripts (MariaDB flavour). I am using Flyway in order to run the scripts. However, some scripts fail...

The script which fails:

CREATE TRIGGER my_cool_trigger
BEFORE UPDATE ON my_db
FOR EACH ROW
BEGIN
    DECLARE n INT;
    SET n = (SELECT COUNT(uuid) FROM my_db WHERE uuid != NEW.uuid AND a_uuid = NEW.a_uuid AND number = NEW.number AND event_id IS NULL AND t_begin < NEW.t_end AND t_end > NEW.t_begin);
    IF n > 0 THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Oof. ERROR!!!';
    END IF;
END;

Error message:

Migration cool_script.sql failed ------------------------------------------------------ SQL State : 42000 Error Code : 1064 Message : (conn=2661) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 6

When I try to execute the same scripts thru Java (they are run as Prepared Statements, sent over JDBC to MariaDB) everything goes as planned without any errors.

Java:

connection.prepareStatement(sql).execute();

I can really not explain to myself why that is happening.

8
  • What you mean by *execute the same scripts thru Java *? Flyway is also Java. Commented Jan 6, 2020 at 13:21
  • they are being run as Prepared Statements Commented Jan 6, 2020 at 13:26
  • What "they"? What exactly is the content of prepared statement? Commented Jan 6, 2020 at 13:29
  • they = the statements contained in the scripts. Commented Jan 6, 2020 at 13:34
  • Show your Java code. Commented Jan 6, 2020 at 13:37

1 Answer 1

2

Turns out the problem lies within not using delimiters. In order for the statements to parse successfully delimiters should be used as following:

DELIMITER //
CREATE TRIGGER my_cool_trigger
BEFORE UPDATE ON my_db
FOR EACH ROW
BEGIN
    DECLARE n INT;
    SET n = (SELECT COUNT(uuid) FROM my_db WHERE uuid != NEW.uuid AND a_uuid = NEW.a_uuid AND number = NEW.number AND event_id IS NULL AND t_begin < NEW.t_end AND t_end > NEW.t_begin);
    IF n > 0 THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='Oof. ERROR!!!';
    END IF;
END//
DELIMITER ;

The reason why it worked in Java was that the Java prepareStatement seems to add custom delimiters or something else...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.