![]() |
An SQL database can be initialized in different ways depending on what your stack is. Of course, you can also do it manually, provided the database is a separate process. JPA has features for DDL generation, and these can be set up to run on startup against the database. This is controlled through two external properties:
You can set
In addition, a file named Spring Boot can automatically create the schema (DDL scripts) of your
By default, Spring Boot enables the fail-fast feature of the Spring JDBC initializer. This
means that, if the scripts cause exceptions, the application fails to start. You can tune
that behavior by setting
If you use Spring Batch, it comes pre-packaged with SQL initialization scripts for most popular database platforms. Spring Boot can detect your database type and execute those scripts on startup. If you use an embedded database, this happens by default. You can also enable it for any database type, as shown in the following example: spring.batch.initialize-schema=always You can also switch off the initialization explicitly by setting
Spring Boot supports two higher-level migration tools: Flyway and Liquibase. To automatically run Flyway database migrations on startup, add the
The migrations are scripts in the form spring.flyway.locations=classpath:db/migration,filesystem:/opt/migration You can also add a special spring.flyway.locations=classpath:db/migration/{vendor} Rather than using See the Flyway class from flyway-core for details of available settings such as schemas
and others. In addition, Spring Boot provides a small set of properties (in
Flyway supports SQL and Java callbacks.
To use SQL-based callbacks, place the callback scripts in the By default, Flyway autowires the ( There is a Flyway sample so that you can see how to set things up. You can also use Flyway to provide data for specific scenarios. For example, you can
place test-specific migrations in spring.flyway.locations=classpath:/db/migration,classpath:/dev/db/migration With that setup, migrations in To automatically run Liquibase database migrations on startup, add the
By default, the master change log is read from By default, Liquibase autowires the ( See
There is a Liquibase sample so that you can see how to set things up.
|