Skip to content

Commit

Permalink
Merge pull request #16282 from gsmet/devservices-fixes
Browse files Browse the repository at this point in the history
Only resolve db kind implicitly if we have no named datasources around
  • Loading branch information
gsmet authored Apr 7, 2021
2 parents f3dd729 + 91a2d8f commit 5ea4530
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,15 @@ private List<AggregatedDataSourceBuildTimeConfigBuildItem> getAggregatedConfigBu
DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig,
DataSourcesJdbcBuildTimeConfig dataSourcesJdbcBuildTimeConfig,
CurateOutcomeBuildItem curateOutcomeBuildItem,
List<JdbcDriverBuildItem> jdbcDriverBuildItems, List<DefaultDataSourceDbKindBuildItem> defaultDbKinds) {
List<JdbcDriverBuildItem> jdbcDriverBuildItems,
List<DefaultDataSourceDbKindBuildItem> defaultDbKinds) {
List<AggregatedDataSourceBuildTimeConfigBuildItem> dataSources = new ArrayList<>();

Optional<String> 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) {
Expand All @@ -285,7 +289,9 @@ private List<AggregatedDataSourceBuildTimeConfigBuildItem> getAggregatedConfigBu
continue;
}
Optional<String> dbKind = DefaultDataSourceDbKindBuildItem
.resolve(entry.getValue().dbKind, defaultDbKinds, curateOutcomeBuildItem);
.resolve(entry.getValue().dbKind, defaultDbKinds,
true,
curateOutcomeBuildItem);
if (!dbKind.isPresent()) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
quarkus.datasource.devservices=true
quarkus.datasource.users.devservices=true
quarkus.datasource.inventory.devservices=true
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,25 @@ public String getScope(CurateOutcomeBuildItem curateOutcomeBuildItem) {
return scope;
}

public static Optional<String> resolve(Optional<String> configured, List<DefaultDataSourceDbKindBuildItem> defaultDbKinds,
public static Optional<String> resolve(Optional<String> configured,
List<DefaultDataSourceDbKindBuildItem> 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<String> resolve(List<DefaultDataSourceDbKindBuildItem> defaultDbKinds,
private static Optional<String> resolveImplicitDbKind(List<DefaultDataSourceDbKindBuildItem> defaultDbKinds,
CurateOutcomeBuildItem curateOutcomeBuildItem) {
if (defaultDbKinds.isEmpty()) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -111,7 +113,7 @@ DevServicesDatasourceResultBuildItem launchDatabases(CurateOutcomeBuildItem cura
}
for (Map.Entry<String, DataSourceBuildTimeConfig> 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);
Expand Down Expand Up @@ -160,12 +162,23 @@ public void run() {
private DevServicesDatasourceResultBuildItem.DbResult startDevDb(String dbName,
CurateOutcomeBuildItem curateOutcomeBuildItem,
List<DefaultDataSourceDbKindBuildItem> installedDrivers,
boolean hasNamedDatasources,
Map<String, DevServicesDatasourceProvider> devDBProviders, DataSourceBuildTimeConfig dataSourceBuildTimeConfig,
Map<String, List<DevServicesDatasourceConfigurationHandlerBuildItem>> configurationHandlerBuildItems,
Map<String, String> propertiesMap, List<Closeable> closeableList) {
Optional<Boolean> 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<String> defaultDbKind = DefaultDataSourceDbKindBuildItem.resolve(
dataSourceBuildTimeConfig.dbKind,
installedDrivers, curateOutcomeBuildItem);
installedDrivers,
dbName != null || enabled.orElse(!hasNamedDatasources),
curateOutcomeBuildItem);

if (!defaultDbKind.isPresent()) {
//nothing we can do
Expand All @@ -181,15 +194,7 @@ private DevServicesDatasourceResultBuildItem.DbResult startDevDb(String dbName,
return null;
}

Optional<Boolean> 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
Expand All @@ -200,6 +205,7 @@ private DevServicesDatasourceResultBuildItem.DbResult startDevDb(String dbName,
}
}
}

//ok, so we know we need to start one

String prefix = "quarkus.datasource.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class EntitiesInDefaultPUWithImplicitUnconfiguredDatasourceTest {
assertThat(t)
.isInstanceOf(ConfigurationException.class)
.hasMessageContainingAll(
"Model classes are defined for the default persistence unit <default> but configured datasource <default> 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ public void buildReactivePersistenceUnit(

// we only support the default pool for now
Optional<String> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ private static boolean isReactiveDB2PoolDefined(DataSourcesBuildTimeConfig dataS
.getDataSourceReactiveBuildTimeConfig(dataSourceName);

Optional<String> dbKind = DefaultDataSourceDbKindBuildItem.resolve(dataSourceBuildTimeConfig.dbKind,
defaultDataSourceDbKindBuildItems, curateOutcomeBuildItem);
defaultDataSourceDbKindBuildItems,
!DataSourceUtil.isDefault(dataSourceName) || dataSourceBuildTimeConfig.devservices.enabled
.orElse(dataSourcesBuildTimeConfig.namedDataSources.isEmpty()),
curateOutcomeBuildItem);

if (!dbKind.isPresent()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ private static boolean isReactiveMySQLPoolDefined(DataSourcesBuildTimeConfig dat
.getDataSourceReactiveBuildTimeConfig(dataSourceName);

Optional<String> dbKind = DefaultDataSourceDbKindBuildItem.resolve(dataSourceBuildTimeConfig.dbKind,
defaultDataSourceDbKindBuildItems, curateOutcomeBuildItem);
defaultDataSourceDbKindBuildItems,
!DataSourceUtil.isDefault(dataSourceName) || dataSourceBuildTimeConfig.devservices.enabled
.orElse(dataSourcesBuildTimeConfig.namedDataSources.isEmpty()),
curateOutcomeBuildItem);
if (!dbKind.isPresent()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ private static boolean isReactivePostgreSQLPoolDefined(DataSourcesBuildTimeConfi
.getDataSourceReactiveBuildTimeConfig(dataSourceName);

Optional<String> dbKind = DefaultDataSourceDbKindBuildItem.resolve(dataSourceBuildTimeConfig.dbKind,
defaultDataSourceDbKindBuildItems, curateOutcomeBuildItem);
defaultDataSourceDbKindBuildItems,
!DataSourceUtil.isDefault(dataSourceName) || dataSourceBuildTimeConfig.devservices.enabled
.orElse(dataSourcesBuildTimeConfig.namedDataSources.isEmpty()),
curateOutcomeBuildItem);

if (!dbKind.isPresent()) {
return false;
Expand Down

0 comments on commit 5ea4530

Please sign in to comment.