75.5 Use a higher-level database migration tool
Spring Boot supports two higher-level migration tools: Flyway and Liquibase.
75.5.1 Execute Flyway database migrations on startup
To automatically run Flyway database migrations on startup, add the org.flywaydb:flyway-core
to your classpath.
The migrations are scripts in the form V<VERSION>__<NAME>.sql
(with <VERSION>
an underscore-separated version, e.g. ‘1’ or ‘2_1’). By default they live in a folder classpath:db/migration
but you can modify that using flyway.locations
(a list). See the Flyway class from flyway-core for details of available settings like schemas etc. In addition Spring Boot provides a small set of properties in FlywayProperties
that can be used to disable the migrations, or switch off the location checking. Spring Boot will call Flyway.migrate()
to perform the database migration. If you would like more control, provide a @Bean
that implements FlywayMigrationStrategy
.
Tip | |
---|---|
If you want to make use of Flyway callbacks, those scripts should also live in the classpath:db/migration folder. |
By default Flyway will autowire the (@Primary
) DataSource
in your context and use that for migrations. If you like to use a different DataSource
you can create one and mark its @Bean
as @FlywayDataSource
- if you do that remember to create another one and mark it as @Primary
if you want two data sources. Or you can use Flyway’s native DataSource
by setting flyway.[url,user,password]
in external properties.
There is a Flyway sample so you can see how to set things up.
75.5.2 Execute Liquibase database migrations on startup
To automatically run Liquibase database migrations on startup, add the org.liquibase:liquibase-core
to your classpath.
The master change log is by default read from db/changelog/db.changelog-master.yaml
but can be set using liquibase.change-log
. In addition to YAML, Liquibase also supports JSON, XML, and SQL change log formats.
See LiquibaseProperties
for details of available settings like contexts, default schema etc.
There is a Liquibase sample so you can see how to set things up.