Spring Boot apps with autoconfiguration for Flyway currently do not trigger the migration if spring.main.lazy-initialization
is set to true independent of what the property spring.flyway.enabled
is set to.
The issue can be worked around by users with a LazyInitializationExcludeFilter
for FlywayMigrationInitializer
but a consistent behavior for spring.flyway.enabled
independent of lazy or eager initialization would be nice.
Comment From: wilkinsona
Thanks for the proposal here as well, but this change also doesn't feel quite right to me.
As with #27144, I think it should be possible for the initialiser to be lazy and for Flyway to then run prior to first use of the DataSource. If Flyway needs to be eagerly initialized for a reason that I have overlooked then I think it should be done with a LazyInitializationExcludeFilter that Spring Boot auto-configures. We should also add a test that replicates the scenario that requires eager initialization and verify that it now works.
Comment From: wilkinsona
I've tested this and the dependencies between other beans, for example JdbcTemplate, and Flyway seem to be sufficient to ensure that the migration initializer is called before the database is accessed.
@hpoettker I assume that you've seen a failure when using Flyway and lazy init is enabled. Can you please provide some details of that failure? I'd like to understand which bean dependencies are missing.
Comment From: hpoettker
@wilkinsona Thank you so much for your interest in getting to the bottom of why this PR was raised. This and all your other efforts to get lazy initialization working seamlessly are really appreciated.
I've created a minimal example with a failing test here: https://github.com/hpoettker/lazyflyway
Thanks to your feedback, I now see that if neither Spring JDBC, nor JPA, nor jOOQ is used, then there is no DependsOnDatabaseInitializationDetector
to create a dependency on FlywayMigrationInitializer
.
I guess there are two opposed ways to look at the minimal example:
- Spring Boot cannot know that I use the DataSource
with a third-party library (i.e. jdbi). Thus, it is my responsibility to (for example) annotate LazyflywayApplication::jdbi
with @DependsOnDatabaseInitialization
.
- There should be a DependsOnDatabaseInitializationDetector
that detects any DataSource
. If Flyway is on the classpath and a DataSource
is (auto-)configured, is there any doubt that I want the DataSource
to be initialized with flyway?
In any case, this PR is not the solution. Thus, I'll close it.