-
Notifications
You must be signed in to change notification settings - Fork 40.8k
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
Database migrations may not have run before JdbcTemplate is used when creating Flyway @Bean #21436
Comments
Thanks for the analysis. It's not clear why you've defined both a @Bean
public Flyway myFlyway(DataSource dataSource, ObjectProvider<FlywayMigrationStrategy> migrationStrategy) {
Flyway customFlyway = new Flyway();
migrationStrategy.getIfAvailable(() -> (flyway) -> flyway.migrate()).migrate(customFlyway);
return customFlyway;
}
The current intent of the auto-configuration is that it will back off if you provide your own |
Thanks for the reply, @wilkinsona.
As I said, I define it because I want to use
I understand the current intent, but for me, they are two different concerns. One is to configure Flyway and the other is to run the migration. Your proposed solution is mixing those two concerns as both are done in the same method -- and a bean declaration is not supposed to do anything else. The other point is that it is basically duplicating code from I do think that separating the concerns is the best approach as it is the cleaner solution and FlywayMigrationStrategy does it very well. I just think that they should be separated in the configuration as well. |
Putting it in another way, it should be possible to configure flyway just like that:
This is currently not possible. The minimum you have to do is:
|
This is really a question of how and when the auto-configuration should back off. I am not sure that I agree that it should be possible to configure Flyway with just a |
We all agree that any setup we choose will be inconvenient for some use cases. Therefore, we have to choose the one that is convenient for the majority of the use cases. Spring boot definitely does that with auto-configuration the way it is, using application.propperties to configure Flyway. Then we have the less common use cases (the ones we are discussing here):
Solution with the current implementation:
The problem with that solution is that while your intent is to manually configure Flyway, you are forced to trigger the migration too. You also lose the flywayMigrationStrategy feature (unless you configure it manually too). Basically, you are forced into use case 3.
Exactly what you expect.
While the developers get the behaviour they want, the mechanism for disabling auto migration is hidden in the auto-configuration logic. If the developer is not aware of the details, one might spend a considerable amount of time to figure out why the FlywayMigrationStrategy that was defined doesn't work. Solution with the proposed implementation:
Exactly what you expect.
Exactly what you expect.
Few lines more of code, but both concerns are clearly overridden (no hidden magic), which reduces the chances of a mistake from the developer, makes the code cleaner and easier to maintain. Last but not least, among the three use cases we discussed here, the third is clearly the least frequent. |
We've discussed this one today and concluded that things should be left as they are. |
Using Spring Boot 2.2.4.RELEASE.
This is the same problem as in #13155, the difference is that a
Flyway
bean is being provided using a@Configuration
class.I checked the code and the problem is that in
FlywayAutoConfiguration
theFlywayMigrationInitializer*DependsOnPostProcessor
classes are only imported if Flyway is not defined:As a workaround we can define our own DependsOnPostProcessor class and import it on the
@Configuration
class.However, this is not ideal because those things are supposed to work out of the box.
The desired behavior is that we would be able to declare our own Flyway bean and everything would work the same way as using the auto-config declared bean.
The solution, in my opinion, is to declare
flywayInitializer
bean outsideFlywayConfiguration
class and importFlywayMigrationInitializer*DependsOnPostProcessor
inFlywayAutoConfiguration
class.In the attached demo application, uncomment the commented out code in
FlywayConfiguration
to start the application without error.demo.zip
The text was updated successfully, but these errors were encountered: