Skip to content

Commit

Permalink
Hibernate Reactive: fail the build if necessary
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mkouba committed Feb 27, 2024
1 parent 94c41be commit 50073a7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,19 @@ public final class HibernateReactiveProcessor {
void registerBeans(BuildProducer<AdditionalBeanBuildItem> additionalBeans, CombinedIndexBuildItem combinedIndex,
List<PersistenceUnitDescriptorBuildItem> 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()));
}
}

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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() {
Expand Down

0 comments on commit 50073a7

Please sign in to comment.