Skip to content

Commit 674b01c

Browse files
onobcsnicoll
authored andcommitted
Take JPA database action into account when setting ddlAuto
See gh-25129
1 parent e4fa39d commit 674b01c

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DataSourceInitializedPublisher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ private boolean isInitializingDatabase(DataSource dataSource) {
129129
: "none");
130130
Map<String, Object> hibernate = this.hibernateProperties.determineHibernateProperties(
131131
this.jpaProperties.getProperties(), new HibernateSettings().ddlAuto(defaultDdlAuto));
132-
return hibernate.containsKey("hibernate.hbm2ddl.auto");
132+
return hibernate.containsKey("hibernate.hbm2ddl.auto") || !hibernate
133+
.getOrDefault("javax.persistence.schema-generation.database.action", "none").equals("none");
133134
}
134135

135136
/**

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* Configuration properties for Hibernate.
3636
*
3737
* @author Stephane Nicoll
38+
* @author Chris Bono
3839
* @since 2.1.0
3940
* @see JpaProperties
4041
*/
@@ -129,6 +130,9 @@ private void applyScanner(Map<String, Object> result) {
129130
}
130131

131132
private String determineDdlAuto(Map<String, String> existing, Supplier<String> defaultDdlAuto) {
133+
if (existing.get(AvailableSettings.HBM2DDL_DATABASE_ACTION) != null) {
134+
return null;
135+
}
132136
String ddlAuto = existing.get(AvailableSettings.HBM2DDL_AUTO);
133137
if (ddlAuto != null) {
134138
return ddlAuto;

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
* @author Andy Wilkinson
9292
* @author Kazuki Shimizu
9393
* @author Stephane Nicoll
94+
* @author Chris Bono
9495
*/
9596
class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTests {
9697

@@ -371,6 +372,50 @@ void hibernatePropertiesCustomizerCanDisableBeanContainer() {
371372
.run((context) -> assertThat(context).doesNotHaveBean(City.class));
372373
}
373374

375+
@Test
376+
void dataSourceSchemaCreatedEventFiredWhenDdlAutoPropertyIsSet() {
377+
dataSourceSchemaCreatedEventFired("spring.jpa.hibernate.ddl-auto:create-drop", true);
378+
}
379+
380+
@Test
381+
void dataSourceSchemaCreatedEventNotFiredWhenDdlAutoPropertyIsSetToNone() {
382+
dataSourceSchemaCreatedEventFired("spring.jpa.hibernate.ddl-auto:none", false);
383+
}
384+
385+
@Test
386+
void dataSourceSchemaCreatedEventFiredWhenHibernateSpecificDdlAutoPropertyIsSet() {
387+
dataSourceSchemaCreatedEventFired("spring.jpa.properties.hibernate.hbm2ddl.auto=create", true);
388+
}
389+
390+
@Test
391+
void dataSourceSchemaCreatedEventNotFiredWhenHibernateSpecificDdlAutoPropertyIsSetToNone() {
392+
dataSourceSchemaCreatedEventFired("spring.jpa.properties.hibernate.hbm2ddl.auto=none", false);
393+
}
394+
395+
@Test
396+
void dataSourceSchemaCreatedEventFiredWhenJpaDbActionPropertyIsSet() {
397+
dataSourceSchemaCreatedEventFired(
398+
"spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create", true);
399+
}
400+
401+
@Test
402+
void dataSourceSchemaCreatedEventNotFiredWhenJpaDbActionPropertyIsSetToNone() {
403+
dataSourceSchemaCreatedEventFired(
404+
"spring.jpa.properties.javax.persistence.schema-generation.database.action=none", false);
405+
}
406+
407+
private void dataSourceSchemaCreatedEventFired(String schemaGenerationPropertyWithValue,
408+
boolean expectEventToBeFired) {
409+
contextRunner().withUserConfiguration(JpaUsingApplicationListenerConfiguration.class)
410+
.withPropertyValues("spring.datasource.initialization-mode=never", schemaGenerationPropertyWithValue)
411+
.run((context) -> {
412+
assertThat(context).hasNotFailed();
413+
assertThat(context.getBean(EventCapturingApplicationListener.class).events.stream()
414+
.filter(DataSourceSchemaCreatedEvent.class::isInstance))
415+
.hasSize(expectEventToBeFired ? 1 : 0);
416+
});
417+
}
418+
374419
@Test
375420
void withSyncBootstrappingAnApplicationListenerThatUsesJpaDoesNotTriggerABeanCurrentlyInCreationException() {
376421
contextRunner().withUserConfiguration(JpaUsingApplicationListenerConfiguration.class)

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernatePropertiesTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
*
4545
* @author Stephane Nicoll
4646
* @author Artsiom Yudovin
47+
* @author Chris Bono
4748
*/
4849
class HibernatePropertiesTests {
4950

@@ -135,6 +136,19 @@ void defaultDdlAutoIsNotInvokedIfHibernateSpecificPropertyIsSet() {
135136
.run(assertDefaultDdlAutoNotInvoked("create"));
136137
}
137138

139+
@Test
140+
void defaultDdlAutoIsNotInvokedAndDdlAutoIsNotSetIfJpaDbActionPropertyIsSet() {
141+
this.contextRunner
142+
.withPropertyValues(
143+
"spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create")
144+
.run(assertHibernateProperties((hibernateProperties) -> {
145+
assertThat(hibernateProperties).doesNotContainKey(AvailableSettings.HBM2DDL_AUTO);
146+
assertThat(hibernateProperties).containsEntry(AvailableSettings.HBM2DDL_DATABASE_ACTION,
147+
"drop-and-create");
148+
verify(this.ddlAutoSupplier, never()).get();
149+
}));
150+
}
151+
138152
private ContextConsumer<AssertableApplicationContext> assertDefaultDdlAutoNotInvoked(String expectedDdlAuto) {
139153
return assertHibernateProperties((hibernateProperties) -> {
140154
assertThat(hibernateProperties).containsEntry(AvailableSettings.HBM2DDL_AUTO, expectedDdlAuto);

0 commit comments

Comments
 (0)