5

we are using Flyway since 1 month without any problems encountered.

But, today I tried to add a new migration script which is very long (more than 1500 lines) and encountered a strange MySQL syntax error.

I opened this script in MySQL Workbench and no syntax error where reported, the script executed without errors.

This script named 'V10012__insert-acceptance-testing-event-moment-passed.sql' contains following instructions.

  • INSERT statement 1
  • INSERT statement 2
  • ...
  • INSERT statement LAST - 1
  • INSERT statement LAST

The error reported by MySQL is the following :

[ERROR] Caused by com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'INSERT INTO video_feedback (id, youtube_video_id) VALUES ( 1102, /* id */' at line 232 [ERROR] com.googlecode.flyway.core.migration.MigrationException: Migration to version 10012 failed! Please restore backups and roll back database and code!

The error is reported on the 'INSERT statement LAST' statement.

But if I invert the 'INSERT statement LAST' and 'INSERT statement LAST - 1' in my script the error is reported on the 'INSERT statement LAST - 1' (which is at the end of the file now). So I have no error in the 'INSERT statement LAST' because Flyway executed it successfully.

Also, if now I completely remove the 'INSERT statement LAST' statement from the script 'V10012_insert-acceptance-testing-event-moment-passed.sql' and put this statement into a new file called 'V10013_test.sql' flyway executes all my migration scripts successfully !

So, what can be the problem in my initial 'V10012__insert-acceptance-testing-event-moment-passed.sql' script ?

Is there a possible script size limitation ?

Here are usefull informations about my environment :

  • My scripts uses lots of /* */ comments
  • Flyway Maven Plugin 1.7
  • Maven 2.0.3
  • mysql:mysql-connector-java:5.1.21
  • MySQL 5.5.X
  • Java JDK 1.7.0_09-b05
  • Windows 7

2 Answers 2

4

I finally found the reason of the problem.

In my script I had :

... 
...

/* INSERT statement LAST - 1 */ 
INSERT INTO `table_1` (`id`, `string_1`, `string_2`)
VALUES (
    1, 
    'aaa',   /* COMMENT 1*/
    'bbb'    /* COMMENT 2*/
);

/* INSERT statement LAST */
INSERT INTO `table_2` (`id`, `string_3`)
VALUES (
    1,       /* COMMENT 3   */
    'cccc'   /* COMMENT 4    */
);

The problems seems to be on COMMENT 2 and COMMENT 4. If I delete theme flyway succeed to execute all my migration scripts.

For example this script will work:

... 
...

/* INSERT statement LAST - 1 */ 
INSERT INTO `table_1` (`id`, `string_1`, `string_2`)
VALUES (
    1, 
    'aaa',   /* COMMENT 1*/
    'bbb'    
);

/* INSERT statement LAST */
INSERT INTO `table_2` (`id`, `string_3`)
VALUES (
    1,   /* COMMENT 3   */
    'cccc'   
);

So, perhaps this is a bug inside the Flyway parser ?

I do not have the time to test it today but it appears that this error occurs only when :

  • We have a comment after a VARCHAR (or other string column types I suppose)
  • This comment is positioned just before a ')' character

This problems seems to only be reproducible if multiple insert statements are present in one SQL script.

Besides, I suppose that comments which are not placed before a ')' are correct, like :

/* INSERT statement LAST */
INSERT INTO `table_2` (`id`, `string_3`)
VALUES (
    1,   /* COMMENT 3   */
    'cccc',  /* COMMENT 4   */
    'dddd'
);

Here the COMMENT 3 and COMMENT 4 passes in Flyway because they are positioned after a ',' and before a string value but they are not placed before a ')' character.

Hope this can help ;-)

1
  • I had a similar issue. Wasn't there any quotes inside your comments?
    – Lætitia
    Feb 15, 2013 at 11:48
1

This is very unlikely due to flyway. This almost certainly has to do with the MySQL driver that flyway is using. With the MySql native client you would naturally assume it can handle any legal syntax. Even the 'optional' comment blocks

One problem I know of for SURE in the jdbc driver for MySql is it can't handle on-the-fly delimiter swapping. So triggers and stored procedures can be a problem particularly if they were generated using mysqldump but then you use a Java app to re ingest.

if you look here and go to spring.datasource.separator you will see that this is set system wide (essentially) to ;.

Now imagine how it will behave when it comes to this line in a script:

DELIMITER ;;
CREATE FUNCTION `int2vancode`(id BIGINT(20)) RETURNS varchar(255)
BEGIN
    DECLARE num VARCHAR(10);
    DECLARE length INT;
    SET num = conv(id, 10, 36);
    SET length = char_length(num);

    return CONCAT(conv(length -1 , 10, 36), num);
END ;;
DELIMITER ;

the java driver is dumb and doesn't interpret any of those statements even though the DELIMITER statement is clearly meant for the client

I am encountering an issue right now where the native client executes every-script without issue but flyway says I have a syntax error that even intellij doesn't see.

** Update**

I solved my problem and it more-or-less confirms the problem I am seeing: I had a create table:

CREATE TABLE `Position` (
  PositionID INT NOT NULL AUTO_INCREMENT,
  `Name` varchar(200) COLLATE utf8_unicode_ci NOT NULL
);

I had to escape Name and Position for flyway to accept it even though my bash script that iteratively executes these has no issue.

Maybe I can tell the Java driver something to help it out?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.