FlyWay migration doesn't run before the datasource or jdbctemplate is used.
See original GitHub issueI am using autoconfiguration with @EnableAutoConfiguration and I have a Configuration class that depends on the database schema. I find it weird that the FlyWay migration isn’t executed before I am able to use the DataSource or the JdbcTemplate.
What have I done to work around this?
@Configuration
public class DatabaseConfiguration {
private final JdbcTemplate jdbcTemplate;
@Autowired
public DatabaseConfiguration(JdbcTemplate jdbcTemplate, FlywayMigrationInitializer flywayMigrationInitializer) {
this.jdbcTemplate = jdbcTemplate;
}
}
As you can see I am not using the FlywayMigrationInitializer since I have no use for it but it does what I want but I do not like how.
This would be a lot nicer but it doesn’t force the migration to run.
@Configuration
@AutoConfigureAfter(FlywayAutoConfiguration.class)
public class DatabaseConfiguration {
private final JdbcTemplate jdbcTemplate;
@Autowired
public DatabaseConfiguration(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Using JdbcTemplate in Flyway's Java migration files causes ...
This is how Spring Boot decided to integrate Flyway. In the default autoconfiguration, you cannot do anything with the database before the ...
Read more >Flyway by Redgate • Database Migrations Made Easy.
New SQL-based migrations are discovered automatically through filesystem and Java classpath scanning at runtime. Once you have configured the locations you ...
Read more >One-Stop Guide to Database Migration with Flyway and ...
A comprehensive guide for database migrations using Flyway with and without Spring Boot.
Read more >Database Migration with Flyway - Spring Framework Guru
Flyway is a tool that anyone with basic knowledge of SQL can use to set up and maintain database schemas. Database migration with...
Read more >“How-to” Guides - Spring
If you cannot do that (for example, you run two applications from the ... Database Migration Tool, like Flyway or Liquibase, you should...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Actually, @olayinkasf had a valid point here, but he was just trying to solve it in a wrong way. I’ve run into similar problem, but I don’t want to open new issue so let me add few words here.
I have a spring boot application with jooq and flyway. Some of my
@Repositorybeans during@PostConstructinitialization try to access database to initialize some caches before the application can start running. Unfortunately this happens beforeFlywayorFlywayMigrationInitializerare configured and running, so my bean tries to access an old version of schema (or empty schema, if running for the first time against new db).This doesn’t seem right to me. My repository class depends on jooq’s
DSLContextand in my opinion spring should run flyway migrations before my repositories, or even jooq is initialized. To solve this I had to add@DependsOn({"flyway", "flywayInitializer"})either on my repositories or on jooq’sDSLContextbean creation to make it initialize after migrations have run.Is there a way spring boot could make sure to run flyway migrations before any
DataSourcedependant beans are created and initialized?And if not, at least this feature could be better documented. I only figured out the solution with
@DependsOnafter analysing spring boot code and the order in which Flyway and FlywayMigrationInitializer are created and initialized. Note that@DependsOn({"flyway", "flywayInitializer"})may stop working if spring boot implementation changes the names of the flyway beans. Besides it makes the nice and automatic configuration of flyway with spring not so nice anymore.@wilkinsona what do you think?
Not sure if this is related but flyway is not running for me either (see here). The server crashes on startup since I’ve set
but no tables were created at that time hence I’m getting