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;
}
}
As explained in the question on Stack Overflow upon which you've already commented, you can't use @AutoConfigureAfter
on an ordinary configuration class.
If you want to establish a dependency between Flyway and your configuration class, you can use @DependsOn
. If you have further questions please post a question of your own on Stack Overflow because, as mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.
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?
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
That sounds like a bug. Please open a new issue, ideally with a minimal sample that reproduces the problem.
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]
@silentsnooc that's impossible to say with the details you've provided. Please share a minimal sample that reproduces the problem in your SO question.
Most helpful comment
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 beforeFlyway
orFlywayMigrationInitializer
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'sDSLContext
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?