Default Hibernate DDL auto to none with Flyway/Liquibase

This commit adds a strategy interface to specific if a given DataSource
has its schema managed. The Hibernate auto-configuration uses it to set
it to "none" if a mechanism to initialize the DataSource is
found and "create-drop" otherwise.

Both Flyway and Liquibase implements that strategy interface and
register it in the context accordingly.

Closes gh-9262
This commit is contained in:
Stephane Nicoll
2017-08-21 17:48:42 +02:00
parent 8babd5d4c5
commit afda0ec129
18 changed files with 427 additions and 78 deletions

View File

@@ -740,7 +740,7 @@ content into your application; rather pick only the properties that you need.
spring.jpa.database= # Target database to operate on, auto-detected by default. Can be alternatively set using the "databasePlatform" property.
spring.jpa.database-platform= # Name of the target database to operate on, auto-detected by default. Can be alternatively set using the "Database" enum.
spring.jpa.generate-ddl=false # Initialize the schema on startup.
spring.jpa.hibernate.ddl-auto= # DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. Default to "create-drop" when using an embedded database, "none" otherwise.
spring.jpa.hibernate.ddl-auto= # DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. Default to "create-drop" when using an embedded database and no schema manager was detected, "none" otherwise.
spring.jpa.hibernate.naming.implicit-strategy= # Hibernate 5 implicit naming strategy fully qualified name.
spring.jpa.hibernate.naming.physical-strategy= # Hibernate 5 physical naming strategy fully qualified name.
spring.jpa.hibernate.use-new-id-generator-mappings= # Use Hibernate's newer IdentifierGenerator for AUTO, TABLE and SEQUENCE.

View File

@@ -1671,7 +1671,10 @@ configuration properties. Some of them are automatically detected according to t
so you shouldn't have to set them.
The `spring.jpa.hibernate.ddl-auto` is a special case in that it has different defaults
depending on whether you are using an embedded database (`create-drop`) or not (`none`).
depending on runtime conditions. If an embedded database is used and no schema manager,
such as Liquibase or Flyway, is handling the `DataSource` it then defaults to
`create-drop`. In all other cases it defaults to `none`.
The dialect to use is also automatically detected based on the current `DataSource` but
you can set `spring.jpa.database` yourself if you want to be explicit and bypass that
check on startup.
@@ -1890,12 +1893,13 @@ database. This is controlled through two external properties:
=== Initialize a database using Hibernate
You can set `spring.jpa.hibernate.ddl-auto` explicitly and the standard Hibernate property
values are `none`, `validate`, `update`, `create`, `create-drop`. Spring Boot chooses a
default value for you based on whether it thinks your database is embedded (default
`create-drop`) or not (default `none`). An embedded database is detected by looking at the
`Connection` type: `hsqldb`, `h2` and `derby` are embedded, the rest are not. Be careful
when switching from in-memory to a '`real`' database that you don't make assumptions about
the existence of the tables and data in the new platform. You either have to set `ddl-auto`
explicitly, or use one of the other mechanisms to initialize the database.
default value for you based on whether it thinks your database is embedded: default to
`create-drop` if no schema manager has been detected, `none` in all other cases. An
embedded database is detected by looking at the `Connection` type: `hsqldb`, `h2` and
`derby` are embedded, the rest are not. Be careful when switching from in-memory to a
'`real`' database that you don't make assumptions about the existence of the tables and
data in the new platform. You either have to set `ddl-auto` explicitly, or use one of the
other mechanisms to initialize the database.
NOTE: You can output the schema creation by enabling the `org.hibernate.SQL` logger. This
is done for you automatically if you enable the <<boot-features-logging-console-output,debug mode>>.