I would like to use Flyway to migrate the database. In Grails (Spring) application one could simply define a custom bean and mark Hibernate SessionFactory depending on that bean.
flyway(Flyway) { bean ->
bean.initMethod = 'migrate'
dataSource = ref('dataSource')
locations = 'classpath:migration/db/mysql'
baselineOnMigrate = true
table = 'schema_version'
}
if (springConfig.containsBean('hibernateDatastore')) {
BeanDefinition beanToInlineWithFlyway = getBeanDefinition('hibernateDatastore')
def dependsOnList = ['flyway'] as Set
if (beanToInlineWithFlyway.dependsOn?.length > 0) {
dependsOnList.addAll(beanToInlineWithFlyway.dependsOn)
}
beanToInlineWithFlyway.dependsOn = dependsOnList as String[]
}
Is there a way in Micronaut achieving the same?
The reason why I like to do this is:
When doing database migration with Flyway you delegate all the DDL stuff to Flyway and set Hibernate jpa.default.properties.hibernate.hbm2dll.auto
to validate
.
jpa:
default:
properties:
hibernate:
hbm2ddl:
auto: validate
Flyway will the start before Hibernate and will check if any migration of the database tables is necessary. After that Hibernate kicks in and validates the database schema against the current JPA entities.
Rather than relying on bean names which is brittle, you can instead define a bean that implements BeanCreatedEventListener<DataSource>
. This would run after the DataSource
bean is created but before the SessionFactory
is created.