Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB2Parser struggles with block depth count #2379

Closed
wallenborn opened this issue May 8, 2019 · 6 comments
Closed

DB2Parser struggles with block depth count #2379

wallenborn opened this issue May 8, 2019 · 6 comments

Comments

@wallenborn
Copy link

@wallenborn wallenborn commented May 8, 2019

Which version and edition of Flyway are you using?

Cloned from git, most recent commit is
51dc662

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)

commandline

Which database are you using (type & version)?

DB2/LINUXX8664 10.5.5

Which operating system are you using?

Windows 10/cygwin

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.)

After upgrade to the current version i ran a
flyway -configFiles=/path/to/flyway-TEST.conf -locations=filesystem:TEST info
on a database that was setup with an earlier version of flyway

What did you expect to see?

The usual migration list with status success for the migrations applied before

What did you see instead?

Flyway Community Edition 5.2.3 by Boxfuse
Database: jdbc:db2://localhost:50000/test (DB2/LINUXX8664 10.5)
ERROR: Unable to parse statement in TEST\R__0.0.1_foo.sql at line 2 col 1: Flyway parsing bug: unable to decrease block depth below 0

Rerunning the command with the -X flag produced the following stacktrace:

org.flywaydb.core.api.FlywayException: Unable to parse statement in TEST\R__0.0.1_foo.sql at line 2 col 1: Flyway parsing bug: unable to decrease block depth below 0
at org.flywaydb.core.internal.parser.Parser.getNextStatement(Parser.java:271)
at org.flywaydb.core.internal.parser.Parser.access$000(Parser.java:41)
at org.flywaydb.core.internal.parser.Parser$ParserSqlStatementIterator.(Parser.java:586)
at org.flywaydb.core.internal.parser.Parser.parse(Parser.java:103)
at org.flywaydb.core.internal.sqlscript.ParserSqlScript.(ParserSqlScript.java:70)
at org.flywaydb.core.internal.database.db2.DB2Database.createSqlScript(DB2Database.java:88)
at org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver.addMigrations(SqlMigrationResolver.java:140)
at org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver.resolveMigrations(SqlMigrationResolver.java:110)
at org.flywaydb.core.internal.resolver.sql.SqlMigrationResolver.resolveMigrations(SqlMigrationResolver.java:47)
at org.flywaydb.core.internal.resolver.CompositeMigrationResolver.collectMigrations(CompositeMigrationResolver.java:130)
at org.flywaydb.core.internal.resolver.CompositeMigrationResolver.doFindAvailableMigrations(CompositeMigrationResolver.java:112)
at org.flywaydb.core.internal.resolver.CompositeMigrationResolver.resolveMigrations(CompositeMigrationResolver.java:98)
at org.flywaydb.core.internal.resolver.CompositeMigrationResolver.resolveMigrations(CompositeMigrationResolver.java:44)
at org.flywaydb.core.internal.info.MigrationInfoServiceImpl.refresh(MigrationInfoServiceImpl.java:133)
at org.flywaydb.core.internal.command.DbInfo.info(DbInfo.java:52)
at org.flywaydb.core.Flyway$4.execute(Flyway.java:308)
at org.flywaydb.core.Flyway$4.execute(Flyway.java:301)
at org.flywaydb.core.Flyway.execute(Flyway.java:453)
at org.flywaydb.core.Flyway.info(Flyway.java:301)
at org.flywaydb.commandline.Main.executeOperation(Main.java:171)
at org.flywaydb.commandline.Main.main(Main.java:112)
Caused by: org.flywaydb.core.api.FlywayException: Flyway parsing bug: unable to decrease block depth below 0
at org.flywaydb.core.internal.parser.ParserContext.decreaseBlockDepth(ParserContext.java:48)
at org.flywaydb.core.internal.database.db2.DB2Parser.adjustBlockDepth(DB2Parser.java:58)
at org.flywaydb.core.internal.parser.Parser.getNextStatement(Parser.java:189)
... 20 more

The migration in question is a stored procedure definition, one of many. Previous migrations with other stored procedures went through without a problem. One difference between the migration is that this one contains a construct like this:

INSERT INTO FOO(ID, X_CODE, Y_CODE)
    SELECT S.ID,
        CASE S.IS_X
            WHEN 1 THEN S.CODE
            ELSE NULL END,
        CASE S.IS_Y
            WHEN 1 THEN S.CODE
            ELSE NULL END
    FROM SOURCE S
    WHERE S.ID = IDENTIFIER;

In the source i see that DB2Parser increases the block depth counter on

"CASE".equals(token.getText()) && !"END".equals(previousToken.getText())

Wouldn't this break in the case above when a CASE is immediately preceeded by the END of the previous clause?

@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented May 8, 2019

Please try again with 6.0.0-beta. The parser was rewritten from the ground up and this change eliminated entire classes of errors. Did this fix the issue?

@wallenborn
Copy link
Author

@wallenborn wallenborn commented May 8, 2019

@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented May 8, 2019

We just tested this internally and this statement parses correctly with the latest code.

@axelfontaine
Copy link
Contributor

@axelfontaine axelfontaine commented May 10, 2019

Reopening as we now have managed to reproduce this.

axelfontaine pushed a commit to flyway/flywaydb.org that referenced this issue May 10, 2019
@wallenborn
Copy link
Author

@wallenborn wallenborn commented May 14, 2019

@axelfontaine axelfontaine removed this from the Flyway 6.0.0 milestone May 22, 2019
@axelfontaine axelfontaine added this to the Flyway 6.0.0-beta2 milestone May 22, 2019
dohrayme pushed a commit to dohrayme/flyway that referenced this issue Feb 3, 2020
@FilenkovMaxim
Copy link

@FilenkovMaxim FilenkovMaxim commented Feb 11, 2020

Hi!
I had this bug on 6.0.1 version
The report from crashlitycs:

Non-fatal Exception: org.flywaydb.core.api.FlywayException
Unable to parse statement in db/migration/V1.2__triggers.sql at line 30 col 1: Flyway parsing bug: unable to decrease block depth below 0
org.flywaydb.core.internal.parser.Parser.getNextStatement (Parser.java:284)
org.flywaydb.core.internal.parser.Parser.access$000 (Parser.java:44)
org.flywaydb.core.internal.parser.Parser$ParserSqlStatementIterator.next (Parser.java:649)
org.flywaydb.core.internal.parser.Parser$ParserSqlStatementIterator.next (Parser.java:614)
org.flywaydb.core.internal.sqlscript.ParserSqlScript.parse (ParserSqlScript.java:79)
org.flywaydb.core.internal.sqlscript.ParserSqlScript.validate (ParserSqlScript.java:124)
org.flywaydb.core.internal.sqlscript.ParserSqlScript.executeInTransaction (ParserSqlScript.java:187)
org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor.canExecuteInTransaction (SqlMigrationExecutor.java:75)
org.flywaydb.core.internal.command.DbMigrate.isExecuteGroupInTransaction (DbMigrate.java:312)
org.flywaydb.core.internal.command.DbMigrate.applyMigrations (DbMigrate.java:275)
org.flywaydb.core.internal.command.DbMigrate.migrateGroup (DbMigrate.java:244)
org.flywaydb.core.internal.command.DbMigrate.access$100 (DbMigrate.java:54)
org.flywaydb.core.internal.command.DbMigrate$2.call (DbMigrate.java:162)
org.flywaydb.core.internal.command.DbMigrate$2.call (DbMigrate.java:159)
org.flywaydb.core.internal.database.base.Connection$1.call (Connection.java:131)
org.flywaydb.core.internal.jdbc.TransactionTemplate.execute (TransactionTemplate.java:74)
org.flywaydb.core.internal.database.base.Connection.lock (Connection.java:127)
org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory.lock (JdbcTableSchemaHistory.java:139)
org.flywaydb.core.internal.command.DbMigrate.migrateAll (DbMigrate.java:159)
org.flywaydb.core.internal.command.DbMigrate.migrate (DbMigrate.java:137)
org.flywaydb.core.Flyway$1.execute (Flyway.java:182)
org.flywaydb.core.Flyway$1.execute (Flyway.java:142)
org.flywaydb.core.Flyway.execute (Flyway.java:504)
org.flywaydb.core.Flyway.migrate (Flyway.java:142)
database.Database.migrate (Database.java:268)

The device was Samsung Galaxy J5, Android 9

It cannot parse this:

CREATE TRIGGER product_type_mobile_uatr
after update of price
on product_type_mobile_t
for each row
-- when old.update_f = 0
begin
select case
when new.product_type_id != old.product_type_id
then raise(abort, 'No change product_type_mobile_t(product_type_id)!')
when new.id != old.id
then raise(abort, 'No change product_type_mobile_t(id)!')
end/* comment for fix parser bug */;
update product_type_mobile_t
set update_f = 1,
update_dt = strftime('%Y-%m-%dT%H:%M:%S', 'now', 'localtime'),
u_dt = strftime('%Y-%m-%dT%H:%M:%f', 'now', 'localtime'),
u_user_cd = (select v.var_value
from app_var_t v
where v.var_name = 'SESSION_CODE'
)
where id = old.id;
end;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants