Open nhoughto opened 4 years ago
@nhoughto You didn't mention Micronaut and Micronaut-Flyway version. With Micronaut 2.0.1 I'm not able to reproduce the issue. Please take a look at this test application https://github.com/micronaut-core-issues-test-app/issue-flyway-77
I've added a migration with an SQL error and this is the log when starting the application. As you can see there is plenty of information regarding the migration that failed:
$ ./gradlew run
> Task :run FAILED
12:54:07.844 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
12:54:08.026 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
12:54:08.031 [main] INFO i.m.flyway.AbstractFlywayMigration - Running migrations for database with qualifier [default]
12:54:08.035 [main] INFO o.f.c.i.license.VersionPrinter - Flyway Community Edition 6.4.4 by Redgate
12:54:08.062 [main] INFO o.f.c.i.database.DatabaseFactory - Database: jdbc:h2:mem:devDb (H2 1.4)
12:54:08.116 [main] INFO o.f.core.internal.command.DbValidate - Successfully validated 2 migrations (execution time 00:00.021s)
12:54:08.129 [main] INFO o.f.c.i.s.JdbcTableSchemaHistory - Creating Schema History table "PUBLIC"."flyway_schema_history" ...
12:54:08.164 [main] INFO o.f.core.internal.command.DbMigrate - Current version of schema "PUBLIC": << Empty Schema >>
12:54:08.167 [main] INFO o.f.core.internal.command.DbMigrate - Migrating schema "PUBLIC" to version 1 - create-books-schema
12:54:08.190 [main] INFO o.f.core.internal.command.DbMigrate - Migrating schema "PUBLIC" to version 2 - insert-data-books
12:54:08.195 [main] ERROR o.f.core.internal.command.DbMigrate - Migration of schema "PUBLIC" to version 2 - insert-data-books failed! Please restore backups and roll back database and code!
12:54:08.209 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [javax.sql.DataSource] could not be loaded:
Migration V2__insert-data-books.sql failed
------------------------------------------
SQL State : 42000
Error Code : 42000
Message : Syntax error in SQL statement "INSERT INTO books (id, name) VALUES (1, Building Microservice[*]');
"; SQL statement:
INSERT INTO books (id, name) VALUES (1, Building Microservice');
[42000-199]
Location : db/migration/V2__insert-data-books.sql (/home/ivan/workspaces/micronaut-apps/issue-flyway-77/build/resources/main/db/migration/V2__insert-data-books.sql)
Line : 1
Statement : INSERT INTO books (id, name) VALUES (1, Building Microservice');
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [javax.sql.DataSource] could not be loaded:
Migration V2__insert-data-books.sql failed
------------------------------------------
SQL State : 42000
Error Code : 42000
Message : Syntax error in SQL statement "INSERT INTO books (id, name) VALUES (1, Building Microservice[*]');
"; SQL statement:
INSERT INTO books (id, name) VALUES (1, Building Microservice');
[42000-199]
Location : db/migration/V2__insert-data-books.sql (/home/ivan/workspaces/micronaut-apps/issue-flyway-77/build/resources/main/db/migration/V2__insert-data-books.sql)
Line : 1
Statement : INSERT INTO books (id, name) VALUES (1, Building Microservice');
at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1540)
at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:220)
at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:2768)
at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:228)
at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:166)
at io.micronaut.runtime.Micronaut.start(Micronaut.java:64)
at io.micronaut.runtime.Micronaut.run(Micronaut.java:294)
at io.micronaut.runtime.Micronaut.run(Micronaut.java:280)
at com.example.Application.main(Application.java:8)
Caused by: org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateException:
Migration V2__insert-data-books.sql failed
------------------------------------------
SQL State : 42000
Error Code : 42000
Message : Syntax error in SQL statement "INSERT INTO books (id, name) VALUES (1, Building Microservice[*]');
"; SQL statement:
INSERT INTO books (id, name) VALUES (1, Building Microservice');
[42000-199]
Location : db/migration/V2__insert-data-books.sql (/home/ivan/workspaces/micronaut-apps/issue-flyway-77/build/resources/main/db/migration/V2__insert-data-books.sql)
Line : 1
Statement : INSERT INTO books (id, name) VALUES (1, Building Microservice');
at org.flywaydb.core.internal.command.DbMigrate.doMigrateGroup(DbMigrate.java:369)
at org.flywaydb.core.internal.command.DbMigrate.access$200(DbMigrate.java:54)
at org.flywaydb.core.internal.command.DbMigrate$3.call(DbMigrate.java:282)
at org.flywaydb.core.internal.jdbc.TransactionalExecutionTemplate.execute(TransactionalExecutionTemplate.java:66)
at org.flywaydb.core.internal.command.DbMigrate.applyMigrations(DbMigrate.java:279)
at org.flywaydb.core.internal.command.DbMigrate.migrateGroup(DbMigrate.java:244)
at org.flywaydb.core.internal.command.DbMigrate.access$100(DbMigrate.java:54)
at org.flywaydb.core.internal.command.DbMigrate$2.call(DbMigrate.java:162)
at org.flywaydb.core.internal.command.DbMigrate$2.call(DbMigrate.java:159)
at org.flywaydb.core.internal.jdbc.TableLockingExecutionTemplate$1.call(TableLockingExecutionTemplate.java:38)
at org.flywaydb.core.internal.jdbc.TransactionalExecutionTemplate.execute(TransactionalExecutionTemplate.java:66)
at org.flywaydb.core.internal.jdbc.TableLockingExecutionTemplate.execute(TableLockingExecutionTemplate.java:33)
at org.flywaydb.core.internal.database.base.Connection.lock(Connection.java:129)
at org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory.lock(JdbcTableSchemaHistory.java:140)
at org.flywaydb.core.internal.command.DbMigrate.migrateAll(DbMigrate.java:159)
at org.flywaydb.core.internal.command.DbMigrate.migrate(DbMigrate.java:137)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:199)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:159)
at org.flywaydb.core.Flyway.execute(Flyway.java:530)
at org.flywaydb.core.Flyway.migrate(Flyway.java:159)
at io.micronaut.flyway.AbstractFlywayMigration.runFlyway(AbstractFlywayMigration.java:90)
at io.micronaut.flyway.AbstractFlywayMigration.run(AbstractFlywayMigration.java:74)
at io.micronaut.flyway.DataSourceMigrationRunner.lambda$null$0(DataSourceMigrationRunner.java:64)
at java.util.Optional.ifPresent(Optional.java:159)
at io.micronaut.flyway.DataSourceMigrationRunner.lambda$onCreated$1(DataSourceMigrationRunner.java:64)
at java.util.Optional.ifPresent(Optional.java:159)
at io.micronaut.flyway.DataSourceMigrationRunner.onCreated(DataSourceMigrationRunner.java:61)
at io.micronaut.flyway.DataSourceMigrationRunner.onCreated(DataSourceMigrationRunner.java:37)
at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1945)
at io.micronaut.context.DefaultBeanContext.createAndRegisterSingletonInternal(DefaultBeanContext.java:2635)
at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:2621)
at io.micronaut.context.DefaultBeanContext.loadContextScopeBean(DefaultBeanContext.java:2163)
at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1538)
... 8 common frames omitted
Caused by: org.flywaydb.core.internal.sqlscript.FlywaySqlScriptException:
Migration V2__insert-data-books.sql failed
------------------------------------------
SQL State : 42000
Error Code : 42000
Message : Syntax error in SQL statement "INSERT INTO books (id, name) VALUES (1, Building Microservice[*]');
"; SQL statement:
INSERT INTO books (id, name) VALUES (1, Building Microservice');
[42000-199]
Location : db/migration/V2__insert-data-books.sql (/home/ivan/workspaces/micronaut-apps/issue-flyway-77/build/resources/main/db/migration/V2__insert-data-books.sql)
Line : 1
Statement : INSERT INTO books (id, name) VALUES (1, Building Microservice');
at org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor.handleException(DefaultSqlScriptExecutor.java:275)
at org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor.executeStatement(DefaultSqlScriptExecutor.java:220)
at org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor.execute(DefaultSqlScriptExecutor.java:127)
at org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor.executeOnce(SqlMigrationExecutor.java:88)
at org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor.access$000(SqlMigrationExecutor.java:33)
at org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor$1.call(SqlMigrationExecutor.java:77)
at org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor$1.call(SqlMigrationExecutor.java:74)
at org.flywaydb.core.internal.database.DefaultExecutionStrategy.execute(DefaultExecutionStrategy.java:28)
at org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor.execute(SqlMigrationExecutor.java:74)
at org.flywaydb.core.internal.command.DbMigrate.doMigrateGroup(DbMigrate.java:366)
... 40 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "INSERT INTO books (id, name) VALUES (1, Building Microservice[*]');
"; SQL statement:
INSERT INTO books (id, name) VALUES (1, Building Microservice');
[42000-199]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:451)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:427)
at org.h2.message.DbException.get(DbException.java:205)
at org.h2.message.DbException.get(DbException.java:181)
at org.h2.message.DbException.getSyntaxError(DbException.java:229)
at org.h2.command.Parser.getSyntaxError(Parser.java:989)
at org.h2.command.Parser.checkRunOver(Parser.java:5129)
at org.h2.command.Parser.initialize(Parser.java:5031)
at org.h2.command.Parser.parse(Parser.java:775)
at org.h2.command.Parser.parse(Parser.java:764)
at org.h2.command.Parser.prepareCommand(Parser.java:683)
at org.h2.engine.Session.prepareLocal(Session.java:627)
at org.h2.engine.Session.prepareCommand(Session.java:565)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1292)
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:217)
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:205)
at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95)
at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
at org.flywaydb.core.internal.jdbc.JdbcTemplate.executeStatement(JdbcTemplate.java:244)
at org.flywaydb.core.internal.sqlscript.ParsedSqlStatement.execute(ParsedSqlStatement.java:111)
at org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor.executeStatement(DefaultSqlScriptExecutor.java:208)
... 48 common frames omitted
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command '/home/ivan/.sdkman/candidates/java/8.0.252-open/bin/java'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 2s
3 actionable tasks: 1 executed, 2 up-to-date
Hmm nope can repro on 2.0.1 / latest versions
Yours seems to fail on DataSource, my run fails on org.hibernate.SessionFactory
, its like if you just use a DataSource it works, but if it is trying to inject a DataSource into JPA/hibernate it swallows the error..
I guess ill see if I can make your repro repo match my experience and then get back to you 👍
Closing due to lack of feedback. If the problem persist please try with the latest version and open a new issue. Thanks.
Closing due to lack of feedback. If the problem persist please try with the latest version and open a new issue. Thanks.
Looks like I have something similar? https://stackoverflow.com/q/70413053/7776688
@sambalmueslie please include a project that reproduces the problem and I'll take a look at it.
What i found while debugging, within DefaultBeanContext.java:3051 the beanConcreteCandidateCache returns an Optional.empty, but the check afterwards DefaultBeanContext.java:3053 checks on null?
Investigated any further. Within DefaultBeanContext.java :2081 he cannot find the type within beanDefinitionClasses. Results in zero candiated for bean Type
and here are some more infos
Good Case (after upgrading to micronaut 3.1.0 gradle plugin)
11:26:41.422 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loaded 388 services of type io.micronaut.inject.BeanDefinitionReference 11:26:41.425 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loading io.micronaut.inject.BeanDefinitionReference services took 271ms 11:26:41.743 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loaded 7 services of type io.micronaut.context.env.PropertySourceLoader 11:26:41.743 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loading io.micronaut.context.env.PropertySourceLoader services took 6ms 11:26:41.807 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loaded 8 services of type io.micronaut.inject.BeanConfiguration 11:26:41.807 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loading io.micronaut.inject.BeanConfiguration services took 3ms
Bad case, after changing @KafkaClient (removing value for id, so nothing which causes the problem)
11:28:29.636 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loaded 373 services of type io.micronaut.inject.BeanDefinitionReference 11:28:29.639 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loading io.micronaut.inject.BeanDefinitionReference services took 242ms 11:28:29.810 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loaded 7 services of type io.micronaut.context.env.PropertySourceLoader 11:28:29.810 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loading io.micronaut.context.env.PropertySourceLoader services took 7ms 11:28:29.878 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loaded 8 services of type io.micronaut.inject.BeanConfiguration 11:28:29.878 [main] DEBUG i.m.c.io.service.SoftServiceLoader - Loading io.micronaut.inject.BeanConfiguration services took 5ms
There is definitively a difference between the loaded References.
I found a workaround, if he did not find all classes, running gradle clean build manually solves the issue.
If a migration fails (invalid SQL etc, whatever) whilst being applied by micronaut-flyway in the context of a micronaut-jpa project a bean creation error is thrown but no migration failure logging from flyway is shown, making it very hard to diagnose what the actual problem is.
Looking at the BeanCreatedEventListener, DataSourceMigrationRunner it doesn't do any logging of the exception?
https://github.com/micronaut-projects/micronaut-flyway/blob/9863860ed1baf9a6fe461f4c13a06e9cccb54dcb/flyway/src/main/java/io/micronaut/flyway/DataSourceMigrationRunner.java#L64
Logs: