Mysql parser error with CASE operator and IF function
See original GitHub issueWhich version and edition of Flyway are you using?
Flyway Community Edition 6.5.5 by Redgate
If this is not the latest version, can you reproduce the issue with the latest one as well?
(Many bugs are fixed in newer releases and upgrading will often resolve the issue)
Which client are you using? (Command-line, Java API, Maven plugin, Gradle plugin)
Command-line and Java API
Which database are you using (type & version)?
mysql 5.7.31 (installed from homebrew)
Which operating system are you using?
Mac OS 10.15.6
What did you do?
(Please include the content causing the issue, any relevant configuration settings, the SQL statement that failed (if relevant) and the command you ran.)
We recently upgraded the Flyway version we use via the Java API from 4.1.2 to 6.5.2 which caused an existing migration script to fail when initialising a new database. Confirmed that the issue is still present in 6.5.5 with both the Java API and command-line versions.
Sample script that generates a parse error:
CREATE TABLE `test_table` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`field` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `test_join` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`test_table_id` bigint(20) unsigned NOT NULL,
`field1` varchar(64) NOT NULL DEFAULT '',
`field2` varchar(64) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
UPDATE `test_table` t
LEFT JOIN test_join j
ON t.id=j.test_table_id and j.field1 in ('VALUE_1', 'VALUE_2', 'VALUE_3')
SET t.field =
CASE j.field1
WHEN 'VALUE_1' THEN IF(STRCMP(j.field2, 'A')=0, 'VALUE_A1', 'VALUE_A2')
WHEN 'VALUE_2' THEN 'VALUE_B'
WHEN 'VALUE_3' THEN 'VALUE_C'
ELSE NULL
END;
What did you expect to see?
Database migration script successfully applied.
What did you see instead?
Parse error:
ERROR: Unable to parse statement in /Users/amccarthy/Temp/flyway-parse/V002__Test.sql at line 15 col 1. See https://flywaydb.org/documentation/knownparserlimitations for more information: Incomplete statement at line 15 col 1: UPDATE `test_table` t
LEFT JOIN test_join j
ON t.id=j.test_table_id and j.field1 in ('VALUE_1', 'VALUE_2', 'VALUE_3')
SET t.field =
CASE j.field1
WHEN 'VALUE_1' THEN IF(STRCMP(j.field2, 'A')=0, 'VALUE_A1', 'VALUE_A2')
WHEN 'VALUE_2' THEN 'VALUE_B'
WHEN 'VALUE_3' THEN 'VALUE_C'
ELSE NULL
END;
Caused by: Incomplete statement at line 15 col 1: UPDATE `test_table` t
LEFT JOIN test_join j
ON t.id=j.test_table_id and j.field1 in ('VALUE_1', 'VALUE_2', 'VALUE_3')
SET t.field =
CASE j.field1
WHEN 'VALUE_1' THEN IF(STRCMP(j.field2, 'A')=0, 'VALUE_A1', 'VALUE_A2')
WHEN 'VALUE_2' THEN 'VALUE_B'
WHEN 'VALUE_3' THEN 'VALUE_C'
ELSE NULL
END;
The script is valid mysql sql. The parse error can be worked around by surrounding the CASE operator with parentheses, but this is not a solution as that would cause checksum errors on existing deployments.
CREATE TABLE `test_table` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`field` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `test_join` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`test_table_id` bigint(20) unsigned NOT NULL,
`field1` varchar(64) NOT NULL DEFAULT '',
`field2` varchar(64) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
UPDATE `test_table` t
LEFT JOIN test_join j
ON t.id=j.test_table_id and j.field1 in ('VALUE_1', 'VALUE_2', 'VALUE_3')
SET t.field =
(CASE j.field1
WHEN 'VALUE_1' THEN IF(STRCMP(j.field2, 'A')=0, 'VALUE_A1', 'VALUE_A2')
WHEN 'VALUE_2' THEN 'VALUE_B'
WHEN 'VALUE_3' THEN 'VALUE_C'
ELSE NULL
END);
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Hi @Lyeeedar,
This doesn’t appear to be fixed with the command-line version. Attempting to apply the first script to an empty database still produces the error.
Additionally other scripts that use the CASE operator are no also failing. These scripts were parsed and applied successfully in version 6.5.5. For example this script which create a stored procedure with an ORDER BY CASE. As with the previous example the parser error does not happen if the CASE operator is wrapped in parentheses.
The error:
@yuvalprtn @amccarthy Thank you for these examples, I will make sure that our next fix fully supports these cases. If you are happy to share more of your scripts, you can post them here or email them to me at philip.liddell@red-gate.com