9.10. Database initialization
You can use different ways to initialize SQL database, depending on what the stack yes. Of course, if the database is a separate process, you can also perform a manual. We recommend the use of a single mechanism for pattern generation.
9.10.1. Initialize the database using JPA
JPA has a function for generating a DDL, which can be set to run against the database at startup. This is controlled by two external properties:
spring.jpa.generate-ddl
(Boolean value) to open and close the function, and independent suppliers.spring.jpa.hibernate.ddl-auto
(Enumeration) is a function Hibernate can be more finely controlled behavior.
9.10.2. Initialize the database using Hibernate
You can set up spring.jpa.hibernate.ddl-auto
as a standard of clear and Hibernate property none
values: validate
, update
, , create
, and create-drop
. Spring Boot your database is embedded to select the default value for you. If the manager does not detect any pattern or by default in all other cases none create-drop
. By looking at Connection
can detect embedded database type. hsqldb, h2 and embedded derby, while others are not. When the in-memory database is converted to "real" database from, please do not make assumptions about the existence of the new platform and data tables. You must explicitly set ddl-auto
or use one of the other mechanisms to initialize the database.
You can enable org.hibernate.SQL
to create recorder output mode. If debug mode is enabled, this will be done for you automatically.
Further, if Hibernate mode from scratch (i.e., if the ddl-auto
property is set to create
or create-drop
), at the time of startup will be performed in the root directory named classpath import.sql
files. If you are careful enough, which is useful for demonstration and testing, but may not want to appear in the class path production environment. This is a Hibernate function (nothing to do with Spring).
9.10.3. Initialize the database
Spring Boot can automatically create your DataSource model (DDL scripts) and initialize (DML script). Respectively, from which the standard root class path locations schema.sql
and data.sql
loading SQL. Further, Spring Boot processing schema-${platform}.sql
and data-${platform}.sql
files (if present), wherein platform
the value spring.datasource.platform
. This allows you to switch to a database-specific script when necessary. For example, you can choose to set it to the database vendor name (hsqldb, h2, oracle, mysql , postgresql , etc.).
Spring Boot automatically creates an embedded mode DataSource. You can use spring.datasource.initialization-mode
the properties to customize this behavior. For example, if you want to always initialized, regardless of the type of DataSource how:
spring.datasource.initialization-mode =always
By default, Spring Boot Enable rapid-fail function Spring JDBC initialization procedure. This means that if the script causes an exception, then the application will not start. You can set spring.datasource.continue-on-error
to adjust the behavior.
In JPA-based application, you can choose to use Hibernate mode or create schema.sql
, but you can not do both. If you are using schema.sql, be sure to disable spring.jpa.hibernate.ddl-auto
.
9.10.4. Spring Batch initialize a database
If you are using Spring Batch, then it is the most popular database platforms with pre-packaged with the SQL initialization script. Spring Boot can detect your database type and execute scripts at startup. If you use an embedded database, this situation will occur by default. You can also think that it, as the following example to enable any database type as shown:
spring.batch.initialize-schema =always
You can also set up spring.batch.initialize-schema=never
clear close initialized.
9.10.5. Use advanced database migration tool
Spring Boot supports two higher levels of migration tools: Flyway
and Liquibase
.
Flyway perform database migration at startup
To automatically run at startup Flyway database migration, please org.flywaydb:flyway-core
add it to your classpath.
Typically, migration is of the following form script V<VERSION>__<NAME>.sql
(with <VERSION>
underlined separate versions, for example, "1" or "2_1"). By default, they are located is named classpath:db/migration
files in the folder, but you can set by spring.flyway.locations
modifying the position. It is one or more classpath:
or a filesystem:
comma-separated list position. For example, the following configuration script search path location and a default category / opt / migration directory:
spring.flyway.locations=classpath:db/migration,filesystem:/opt/migration
You can also add a special {vendor}
placeholder to use vendor-specific scripts. Assume the following:
spring.flyway.locations=classpath:db/migration/{vendor}
The foregoing configuration is not using db / migration, but according to the type of database (e.g., db/migration/mysql
for MySQL) to be used to set the file folder. In the list of supported databases DatabaseDriver
to provide in.
Migration can be written in Java. Flyway will use any implementation JavaMigration
bean automatic configuration.
FlywayProperties
Most settings provide Flyway and small amounts of other properties that can be used to disable the migration or closed position check. If you need more control over the configuration, consider registering FlywayConfigurationCustomizer
Bean.
Spring Boot call Flyway.migrate()
to perform database migration. If you want more control, provide implementations for FlywayMigrationStrategy
@Bean of.
Flyway support SQL and Java callback. To put SQL callback, set a callback-based scripts classpath:db/migration
folder. To use the Java-based callback, create one or more implementations Callback
of the bean. Any such bean will automatically register with the Flyway. It can be used @Order
or implemented Ordered
to adjust the order. Also it can detect realized deprecated FlywayCallback
Bean interface, but can not be used with Callback Bean.
By default, Flyway automatic automatic connection in your context ( @Primary
) and the DataSource for migration. If you want to use other DataSource, you can create one and mark it as @FlywayDataSource
the @Bean. If this does want two data sources, remember to create another data source and mark it as @Primary. In addition, you can set up spring.flyway.[url,user,password]
to use external Flyway native DataSource property. Set spring.flyway.url
or spring.flyway.user
enough to make Flyway use your own DataSource. If any of these three attributes are not set, then the use of the equivalent property spring.datasource
value.
You can also use Flyway provide data for a particular situation. For example, you can src/test/resources
put the migration-specific tests, and only started to run when test them in your application. In addition, you can use a specific profile to customize the configuration spring.flyway.locations
to run only when active in certain specific migration profiles. For example, in application-dev.properties, you can specify the following settings:
spring.flyway.locations=classpath:/db/migration,classpath:/dev/db/migration
With this setting, dev/db/migration
run the migration if only in dev profile is active.
Liquibase perform database migration at startup
To automatically run Liquibase database migration, set when you start org.liquibase:liquibase-core
to add to your classpath.
By default, from db/changelog/db.changelog-master.yaml
reading the main change log, but you can set spring.liquibase.change-log
to change its position. In addition to YAML, Liquibase also supports JSON, XML and SQL change log format.
By default, Liquibase automatic assembly (@Primary) DataSource your and context for the migration. If you need to use other DataSource, you can create one and mark it as @LiquibaseDataSource
the @Bean. If you do, and you want two data sources, remember to create another data source and mark it as @Primary. In addition, you can set up spring.liquibase.[url,user,password]
to use external Liquibase native DataSource property. Set spring.liquibase.url
or spring.liquibase.user
enough to make Liquibase use its own DataSource. If any of these three attributes are not set, then the use of the equivalent property spring.datasource
value.
See LiquibaseProperties
for more information about the available settings, such as context, default and other architecture.