From 91a2d8f14775a91b93a013081694f29a1961ffa2 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Tue, 6 Apr 2021 15:52:52 +0200 Subject: [PATCH] Only resolve db kind implicitly if we have no named datasources around Also explicitly disable automatic resolution if devservices have been disabled. --- .../agroal/deployment/AgroalProcessor.java | 12 ++++++-- ...ultiple-devservices-datasources.properties | 1 + .../spi/DefaultDataSourceDbKindBuildItem.java | 15 +++++++--- .../DevServicesDatasourceProcessor.java | 30 +++++++++++-------- ...ithImplicitUnconfiguredDatasourceTest.java | 2 +- .../HibernateReactiveProcessor.java | 6 +++- .../ErroneousConfigHotReloadTestCase.java | 3 +- .../ReactiveDB2ClientProcessor.java | 6 +++- .../ReactiveMySQLClientProcessor.java | 5 +++- .../deployment/ReactivePgClientProcessor.java | 5 +++- 10 files changed, 60 insertions(+), 25 deletions(-) diff --git a/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java b/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java index 69b4dd00f82c2..78f2190fcdbf1 100644 --- a/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java +++ b/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java @@ -261,11 +261,15 @@ private List getAggregatedConfigBu DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig, DataSourcesJdbcBuildTimeConfig dataSourcesJdbcBuildTimeConfig, CurateOutcomeBuildItem curateOutcomeBuildItem, - List jdbcDriverBuildItems, List defaultDbKinds) { + List jdbcDriverBuildItems, + List defaultDbKinds) { List dataSources = new ArrayList<>(); Optional effectiveDbKind = DefaultDataSourceDbKindBuildItem - .resolve(dataSourcesBuildTimeConfig.defaultDataSource.dbKind, defaultDbKinds, curateOutcomeBuildItem); + .resolve(dataSourcesBuildTimeConfig.defaultDataSource.dbKind, defaultDbKinds, + dataSourcesBuildTimeConfig.defaultDataSource.devservices.enabled + .orElse(dataSourcesBuildTimeConfig.namedDataSources.isEmpty()), + curateOutcomeBuildItem); if (effectiveDbKind.isPresent()) { if (dataSourcesJdbcBuildTimeConfig.jdbc.enabled) { @@ -285,7 +289,9 @@ private List getAggregatedConfigBu continue; } Optional dbKind = DefaultDataSourceDbKindBuildItem - .resolve(entry.getValue().dbKind, defaultDbKinds, curateOutcomeBuildItem); + .resolve(entry.getValue().dbKind, defaultDbKinds, + true, + curateOutcomeBuildItem); if (!dbKind.isPresent()) { continue; } diff --git a/extensions/agroal/deployment/src/test/resources/application-multiple-devservices-datasources.properties b/extensions/agroal/deployment/src/test/resources/application-multiple-devservices-datasources.properties index 3101fa09fec0c..3d0d5887ba24d 100644 --- a/extensions/agroal/deployment/src/test/resources/application-multiple-devservices-datasources.properties +++ b/extensions/agroal/deployment/src/test/resources/application-multiple-devservices-datasources.properties @@ -1,2 +1,3 @@ +quarkus.datasource.devservices=true quarkus.datasource.users.devservices=true quarkus.datasource.inventory.devservices=true diff --git a/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DefaultDataSourceDbKindBuildItem.java b/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DefaultDataSourceDbKindBuildItem.java index 52a25ccdb2cd0..051e90f078318 100644 --- a/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DefaultDataSourceDbKindBuildItem.java +++ b/extensions/datasource/deployment-spi/src/main/java/io/quarkus/datasource/deployment/spi/DefaultDataSourceDbKindBuildItem.java @@ -53,18 +53,25 @@ public String getScope(CurateOutcomeBuildItem curateOutcomeBuildItem) { return scope; } - public static Optional resolve(Optional configured, List defaultDbKinds, + public static Optional resolve(Optional configured, + List defaultDbKinds, + boolean enableImplicitResolution, CurateOutcomeBuildItem curateOutcomeBuildItem) { if (configured.isPresent()) { return Optional.of(DatabaseKind.normalize(configured.get())); } - return resolve(defaultDbKinds, curateOutcomeBuildItem); + + if (!enableImplicitResolution) { + return Optional.empty(); + } + + return resolveImplicitDbKind(defaultDbKinds, curateOutcomeBuildItem); } /** - * Attempts to resolve the default DB kind for the case where none has been specified. + * Attempts to resolve the implicit DB kind for the case where none has been specified. */ - public static Optional resolve(List defaultDbKinds, + private static Optional resolveImplicitDbKind(List defaultDbKinds, CurateOutcomeBuildItem curateOutcomeBuildItem) { if (defaultDbKinds.isEmpty()) { return Optional.empty(); diff --git a/extensions/datasource/deployment/src/main/java/io/quarkus/datasource/deployment/devservices/DevServicesDatasourceProcessor.java b/extensions/datasource/deployment/src/main/java/io/quarkus/datasource/deployment/devservices/DevServicesDatasourceProcessor.java index c90126381936e..31ab21817b8a0 100644 --- a/extensions/datasource/deployment/src/main/java/io/quarkus/datasource/deployment/devservices/DevServicesDatasourceProcessor.java +++ b/extensions/datasource/deployment/src/main/java/io/quarkus/datasource/deployment/devservices/DevServicesDatasourceProcessor.java @@ -100,7 +100,9 @@ DevServicesDatasourceResultBuildItem launchDatabases(CurateOutcomeBuildItem cura .collect(Collectors.toMap(DevServicesDatasourceProviderBuildItem::getDatabase, DevServicesDatasourceProviderBuildItem::getDevServicesProvider)); - defaultResult = startDevDb(null, curateOutcomeBuildItem, installedDrivers, devDBProviderMap, + defaultResult = startDevDb(null, curateOutcomeBuildItem, installedDrivers, + !dataSourceBuildTimeConfig.namedDataSources.isEmpty(), + devDBProviderMap, dataSourceBuildTimeConfig.defaultDataSource, configHandlersByDbType, propertiesMap, closeableList); if (defaultResult != null) { @@ -111,7 +113,7 @@ DevServicesDatasourceResultBuildItem launchDatabases(CurateOutcomeBuildItem cura } for (Map.Entry entry : dataSourceBuildTimeConfig.namedDataSources.entrySet()) { DevServicesDatasourceResultBuildItem.DbResult result = startDevDb(entry.getKey(), curateOutcomeBuildItem, - installedDrivers, + installedDrivers, true, devDBProviderMap, entry.getValue(), configHandlersByDbType, propertiesMap, closeableList); if (result != null) { namedResults.put(entry.getKey(), result); @@ -160,12 +162,23 @@ public void run() { private DevServicesDatasourceResultBuildItem.DbResult startDevDb(String dbName, CurateOutcomeBuildItem curateOutcomeBuildItem, List installedDrivers, + boolean hasNamedDatasources, Map devDBProviders, DataSourceBuildTimeConfig dataSourceBuildTimeConfig, Map> configurationHandlerBuildItems, Map propertiesMap, List closeableList) { + Optional enabled = dataSourceBuildTimeConfig.devservices.enabled; + if (enabled.isPresent() && !enabled.get()) { + //explicitly disabled + log.debug("Not starting devservices for " + (dbName == null ? "default datasource" : dbName) + + " as it has been disabled in the config"); + return null; + } + Optional defaultDbKind = DefaultDataSourceDbKindBuildItem.resolve( dataSourceBuildTimeConfig.dbKind, - installedDrivers, curateOutcomeBuildItem); + installedDrivers, + dbName != null || enabled.orElse(!hasNamedDatasources), + curateOutcomeBuildItem); if (!defaultDbKind.isPresent()) { //nothing we can do @@ -181,15 +194,7 @@ private DevServicesDatasourceResultBuildItem.DbResult startDevDb(String dbName, return null; } - Optional enabled = dataSourceBuildTimeConfig.devservices.enabled; - if (enabled.isPresent()) { - if (!enabled.get()) { - //explicitly disabled - log.debug("Not starting devservices for " + (dbName == null ? "default datasource" : dbName) - + " as it has been disabled in the config"); - return null; - } - } else { + if (!enabled.isPresent()) { for (DevServicesDatasourceConfigurationHandlerBuildItem i : configHandlers) { if (i.getCheckConfiguredFunction().test(dbName)) { //this database has explicit configuration @@ -200,6 +205,7 @@ private DevServicesDatasourceResultBuildItem.DbResult startDevDb(String dbName, } } } + //ok, so we know we need to start one String prefix = "quarkus.datasource."; diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest.java index 738e4d1e683a9..244ec2335b403 100644 --- a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest.java +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest.java @@ -19,7 +19,7 @@ public class EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest { assertThat(t) .isInstanceOf(ConfigurationException.class) .hasMessageContainingAll( - "Model classes are defined for the default persistence unit but configured datasource not found: the default EntityManagerFactory will not be created. To solve this, configure the default datasource. Refer to https://quarkus.io/guides/datasource for guidance."); + "Model classes are defined for the default persistence unit, but no default datasource was found. The default EntityManagerFactory will not be created. To solve this, configure the default datasource. Refer to https://quarkus.io/guides/datasource for guidance."); }) .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) .addClass(MyEntity.class)) 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 e06bdcfb65c1a..b90af31d87418 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 @@ -147,7 +147,11 @@ public void buildReactivePersistenceUnit( // we only support the default pool for now Optional dbKindOptional = DefaultDataSourceDbKindBuildItem.resolve( - dataSourcesBuildTimeConfig.defaultDataSource.dbKind, defaultDataSourceDbKindBuildItems, curateOutcomeBuildItem); + dataSourcesBuildTimeConfig.defaultDataSource.dbKind, + defaultDataSourceDbKindBuildItems, + dataSourcesBuildTimeConfig.defaultDataSource.devservices.enabled + .orElse(dataSourcesBuildTimeConfig.namedDataSources.isEmpty()), + curateOutcomeBuildItem); if (dbKindOptional.isPresent()) { final String dbKind = dbKindOptional.get(); ParsedPersistenceXmlDescriptor reactivePU = generateReactivePersistenceUnit( diff --git a/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/ErroneousConfigHotReloadTestCase.java b/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/ErroneousConfigHotReloadTestCase.java index 1317e60c9f85e..7c2e308850083 100644 --- a/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/ErroneousConfigHotReloadTestCase.java +++ b/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/ErroneousConfigHotReloadTestCase.java @@ -22,7 +22,8 @@ public class ErroneousConfigHotReloadTestCase { @Test public void test() { - RestAssured.when().get("/unannotatedEntity").then().statusCode(500).body(containsString("No entities were found")) + RestAssured.when().get("/unannotatedEntity").then().statusCode(500) + .body(containsString("The default datasource has not been properly configured.")) .body(not(containsString("NullPointer"))); TEST.modifyResourceFile("application.properties", new Function() { diff --git a/extensions/reactive-db2-client/deployment/src/main/java/io/quarkus/reactive/db2/client/deployment/ReactiveDB2ClientProcessor.java b/extensions/reactive-db2-client/deployment/src/main/java/io/quarkus/reactive/db2/client/deployment/ReactiveDB2ClientProcessor.java index 3d882b0cbaf7a..3dab30323ae19 100644 --- a/extensions/reactive-db2-client/deployment/src/main/java/io/quarkus/reactive/db2/client/deployment/ReactiveDB2ClientProcessor.java +++ b/extensions/reactive-db2-client/deployment/src/main/java/io/quarkus/reactive/db2/client/deployment/ReactiveDB2ClientProcessor.java @@ -161,7 +161,11 @@ private static boolean isReactiveDB2PoolDefined(DataSourcesBuildTimeConfig dataS .getDataSourceReactiveBuildTimeConfig(dataSourceName); Optional dbKind = DefaultDataSourceDbKindBuildItem.resolve(dataSourceBuildTimeConfig.dbKind, - defaultDataSourceDbKindBuildItems, curateOutcomeBuildItem); + defaultDataSourceDbKindBuildItems, + !DataSourceUtil.isDefault(dataSourceName) || dataSourceBuildTimeConfig.devservices.enabled + .orElse(dataSourcesBuildTimeConfig.namedDataSources.isEmpty()), + curateOutcomeBuildItem); + if (!dbKind.isPresent()) { return false; } diff --git a/extensions/reactive-mysql-client/deployment/src/main/java/io/quarkus/reactive/mysql/client/deployment/ReactiveMySQLClientProcessor.java b/extensions/reactive-mysql-client/deployment/src/main/java/io/quarkus/reactive/mysql/client/deployment/ReactiveMySQLClientProcessor.java index 79168330f17fa..324ba72cb853d 100644 --- a/extensions/reactive-mysql-client/deployment/src/main/java/io/quarkus/reactive/mysql/client/deployment/ReactiveMySQLClientProcessor.java +++ b/extensions/reactive-mysql-client/deployment/src/main/java/io/quarkus/reactive/mysql/client/deployment/ReactiveMySQLClientProcessor.java @@ -167,7 +167,10 @@ private static boolean isReactiveMySQLPoolDefined(DataSourcesBuildTimeConfig dat .getDataSourceReactiveBuildTimeConfig(dataSourceName); Optional dbKind = DefaultDataSourceDbKindBuildItem.resolve(dataSourceBuildTimeConfig.dbKind, - defaultDataSourceDbKindBuildItems, curateOutcomeBuildItem); + defaultDataSourceDbKindBuildItems, + !DataSourceUtil.isDefault(dataSourceName) || dataSourceBuildTimeConfig.devservices.enabled + .orElse(dataSourcesBuildTimeConfig.namedDataSources.isEmpty()), + curateOutcomeBuildItem); if (!dbKind.isPresent()) { return false; } diff --git a/extensions/reactive-pg-client/deployment/src/main/java/io/quarkus/reactive/pg/client/deployment/ReactivePgClientProcessor.java b/extensions/reactive-pg-client/deployment/src/main/java/io/quarkus/reactive/pg/client/deployment/ReactivePgClientProcessor.java index 88dee4c0d2888..15767fdcaf364 100644 --- a/extensions/reactive-pg-client/deployment/src/main/java/io/quarkus/reactive/pg/client/deployment/ReactivePgClientProcessor.java +++ b/extensions/reactive-pg-client/deployment/src/main/java/io/quarkus/reactive/pg/client/deployment/ReactivePgClientProcessor.java @@ -179,7 +179,10 @@ private static boolean isReactivePostgreSQLPoolDefined(DataSourcesBuildTimeConfi .getDataSourceReactiveBuildTimeConfig(dataSourceName); Optional dbKind = DefaultDataSourceDbKindBuildItem.resolve(dataSourceBuildTimeConfig.dbKind, - defaultDataSourceDbKindBuildItems, curateOutcomeBuildItem); + defaultDataSourceDbKindBuildItems, + !DataSourceUtil.isDefault(dataSourceName) || dataSourceBuildTimeConfig.devservices.enabled + .orElse(dataSourcesBuildTimeConfig.namedDataSources.isEmpty()), + curateOutcomeBuildItem); if (!dbKind.isPresent()) { return false;