From 50073a728722ce5646c8e03099e6edfb5256ec76 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Mon, 26 Feb 2024 12:57:52 +0100 Subject: [PATCH] Hibernate Reactive: fail the build if necessary - fail the build if the default datasource is not configured and Dev Services are disabled - fail the build unless exactly one persistent unit is configured --- .../HibernateReactiveProcessor.java | 19 +++++++++++++++---- ...ithExplicitUnconfiguredDatasourceTest.java | 9 ++++++++- ...ithImplicitUnconfiguredDatasourceTest.java | 9 ++++++++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/extensions/hibernate-reactive/deployment/src/main/java/io/quarkus/hibernate/reactive/deployment/HibernateReactiveProcessor.java b/extensions/hibernate-reactive/deployment/src/main/java/io/quarkus/hibernate/reactive/deployment/HibernateReactiveProcessor.java index 7a4abf1fe751ef..37303a0f579a1f 100644 --- a/extensions/hibernate-reactive/deployment/src/main/java/io/quarkus/hibernate/reactive/deployment/HibernateReactiveProcessor.java +++ b/extensions/hibernate-reactive/deployment/src/main/java/io/quarkus/hibernate/reactive/deployment/HibernateReactiveProcessor.java @@ -93,13 +93,19 @@ public final class HibernateReactiveProcessor { void registerBeans(BuildProducer additionalBeans, CombinedIndexBuildItem combinedIndex, List descriptors, JpaModelBuildItem jpaModel) { + if (!hasEntities(jpaModel)) { + LOG.warnf("Skip registration of the %s bean - no JPA entities were found", + ReactiveSessionFactoryProducer.class.getSimpleName()); + return; + } if (descriptors.size() == 1) { // Only register the bean if their EMF dependency is also available, so use the same guard as the ORM extension additionalBeans.produce(new AdditionalBeanBuildItem(ReactiveSessionFactoryProducer.class)); } else { - LOG.warnf( - "Skipping registration of %s bean because exactly one persistence unit is required for their registration", - ReactiveSessionFactoryProducer.class.getSimpleName()); + throw new ConfigurationException( + "The Hibernate Reactive extension requires exactly one persistence unit configured: " + descriptors + .stream() + .map(PersistenceUnitDescriptorBuildItem::getPersistenceUnitName).collect(Collectors.toList())); } } @@ -172,7 +178,12 @@ public void buildReactivePersistenceUnit( .orElse(!dataSourcesBuildTimeConfig.hasNamedDataSources()), curateOutcomeBuildItem); - if (dbKindOptional.isPresent()) { + if (dbKindOptional.isEmpty()) { + throw new ConfigurationException( + "The default datasource must be configured for Hibernate Reactive. Refer to https://quarkus.io/guides/datasource for guidance.", + Set.of("quarkus.datasource.db-kind", "quarkus.datasource.username", + "quarkus.datasource.password")); + } else { HibernateOrmConfigPersistenceUnit persistenceUnitConfig = hibernateOrmConfig.defaultPersistenceUnit(); ParsedPersistenceXmlDescriptor reactivePU = generateReactivePersistenceUnit( hibernateOrmConfig, index, persistenceUnitConfig, jpaModel, diff --git a/extensions/hibernate-reactive/deployment/src/test/java/io/quarkus/hibernate/reactive/config/datasource/EntitiesInDefaultPUWithExplicitUnconfiguredDatasourceTest.java b/extensions/hibernate-reactive/deployment/src/test/java/io/quarkus/hibernate/reactive/config/datasource/EntitiesInDefaultPUWithExplicitUnconfiguredDatasourceTest.java index c4c11e2307d013..a70236bce167b0 100644 --- a/extensions/hibernate-reactive/deployment/src/test/java/io/quarkus/hibernate/reactive/config/datasource/EntitiesInDefaultPUWithExplicitUnconfiguredDatasourceTest.java +++ b/extensions/hibernate-reactive/deployment/src/test/java/io/quarkus/hibernate/reactive/config/datasource/EntitiesInDefaultPUWithExplicitUnconfiguredDatasourceTest.java @@ -1,9 +1,12 @@ package io.quarkus.hibernate.reactive.config.datasource; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import io.quarkus.hibernate.reactive.config.MyEntity; +import io.quarkus.runtime.configuration.ConfigurationException; import io.quarkus.test.QuarkusUnitTest; public class EntitiesInDefaultPUWithExplicitUnconfiguredDatasourceTest { @@ -13,7 +16,11 @@ public class EntitiesInDefaultPUWithExplicitUnconfiguredDatasourceTest { .withApplicationRoot((jar) -> jar .addClass(MyEntity.class)) .overrideConfigKey("quarkus.hibernate-orm.datasource", "ds-1") - .overrideConfigKey("quarkus.hibernate-orm.database.generation", "drop-and-create"); + .overrideConfigKey("quarkus.hibernate-orm.database.generation", "drop-and-create") + .assertException(t -> assertThat(t) + .isInstanceOf(ConfigurationException.class) + .hasMessageContaining( + "The default datasource must be configured for Hibernate Reactive. Refer to https://quarkus.io/guides/datasource for guidance.")); @Test public void testInvalidConfiguration() { diff --git a/extensions/hibernate-reactive/deployment/src/test/java/io/quarkus/hibernate/reactive/config/datasource/EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest.java b/extensions/hibernate-reactive/deployment/src/test/java/io/quarkus/hibernate/reactive/config/datasource/EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest.java index 74f0f25029c80a..495a8b8dc4fc69 100644 --- a/extensions/hibernate-reactive/deployment/src/test/java/io/quarkus/hibernate/reactive/config/datasource/EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest.java +++ b/extensions/hibernate-reactive/deployment/src/test/java/io/quarkus/hibernate/reactive/config/datasource/EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest.java @@ -1,9 +1,12 @@ package io.quarkus.hibernate.reactive.config.datasource; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import io.quarkus.hibernate.reactive.config.MyEntity; +import io.quarkus.runtime.configuration.ConfigurationException; import io.quarkus.test.QuarkusUnitTest; public class EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest { @@ -13,7 +16,11 @@ public class EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest { .withApplicationRoot((jar) -> jar .addClass(MyEntity.class)) // The datasource won't be truly "unconfigured" if dev services are enabled - .overrideConfigKey("quarkus.devservices.enabled", "false"); + .overrideConfigKey("quarkus.devservices.enabled", "false") + .assertException(t -> assertThat(t) + .isInstanceOf(ConfigurationException.class) + .hasMessageContaining( + "The default datasource must be configured for Hibernate Reactive. Refer to https://quarkus.io/guides/datasource for guidance.")); @Test public void testInvalidConfiguration() {