Search code examples
spring-bootjpaspring-data-jpaddl

Content gets appended to DDL files generated via JPA script generation instead of being replaced


Since updating to Spring Boot 2, I noticed a change in behavior in the way the DDL files are generated when using the: spring.jpa.properties.javax.persistence.schema-generation.scripts.* options.

Previously, in Spring Boot 1.5, every time I was running my application or tests (I also have integration tests that check the content of these files), the DDL files were getting re-generated, for example:

drop table testSchema.App if exists
drop table testSchema.AppMetadata if exists
drop table testSchema.Server if exists
drop table testSchema.User if exists
drop table testSchema.UserRole if exists
drop table testSchema.UserToken if exists

Now, after upgrading to Spring Boot 2.0.5, every time I run my tests or app, the content gets appended to the DDL file in this fashion:

drop table testSchema.App if exists
drop table testSchema.AppMetadata if exists
drop table testSchema.Server if exists
drop table testSchema.User if exists
drop table testSchema.UserRole if exists
drop table testSchema.UserToken if exists
drop table testSchema.App if exists
drop table testSchema.AppMetadata if exists
drop table testSchema.Server if exists
drop table testSchema.User if exists
drop table testSchema.UserRole if exists
drop table testSchema.UserToken if exists
drop table testSchema.App if exists
drop table testSchema.AppMetadata if exists
drop table testSchema.Server if exists
drop table testSchema.User if exists
drop table testSchema.UserRole if exists
drop table testSchema.UserToken if exists

Not sure whether this is a new behaviour of the framework or a configuration problem I have which has only come up now. Here is my test.properties file (as already said, the same issue happens when running the app normally but the configs are relatively similar).

# ========= DATA SOURCE : DB connection ========
spring.datasource.url=jdbc:h2:mem:myDb;INIT=CREATE SCHEMA IF NOT EXISTS testSchema
spring.datasource.username=________
spring.datasource.password=________
# ========= JPA / HIBERNATE =========
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.default_schema=testSchema
# DDL operations via Hibernate =========
spring.jpa.hibernate.ddl-auto = none
spring.jpa.generate-ddl = false
spring.jpa.show-sql=false
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
# DDL operations via JPA =========
spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=drop-and-create
spring.jpa.properties.javax.persistence.schema-generation.drop-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.drop-target=./src/test/resources/test_data/dbCreationTestFiles/jpaGenerated_drop_test_sys_db.ddl
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=./src/test/resources/test_data/dbCreationTestFiles/jpaGenerated_create_test_sys_db.ddl

Is there an issue with my configuration? How can I set things up so that the DDL files are re-written on every run?

Thanks.


Solution

  • In order to fix this you need to add this property to your configuration

    spring.jpa.properties.hibernate.hbm2ddl.schema-generation.script.append=false
    

    See https://hibernate.atlassian.net/browse/HHH-11817 for details.