question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FlyWay migration doesn't run before the datasource or jdbctemplate is used.

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
maraswronacommented, Mar 27, 2018

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 @Repository beans during @PostConstruct initialization try to access database to initialize some caches before the application can start running. Unfortunately this happens before Flyway or FlywayMigrationInitializer are 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 DSLContext and 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’s DSLContext bean creation to make it initialize after migrations have run.

Is there a way spring boot could make sure to run flyway migrations before any DataSource dependant beans are created and initialized?

And if not, at least this feature could be better documented. I only figured out the solution with @DependsOn after 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?

2reactions
ghostcommented, May 6, 2018

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

spring.jpa.hibernate.ddl-auto=validate

but no tables were created at that time hence I’m getting

SchemaManagementException: Schema-validation: missing table [app_user]
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found