Skip to content

Commit

Permalink
Only throw a storage engine exception for MySQL and MariaDB
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Aug 26, 2020
1 parent aeab9bd commit 140dfcf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
Expand Down Expand Up @@ -555,7 +556,7 @@ private void handleHibernateORMWithNoPersistenceXml(
.filter(i -> i.isDefault())
.findFirst();

Set<String> storageEngines = new HashSet<>();
Set<String> storageEngineCollector = new HashSet<>();

if ((defaultJdbcDataSource.isPresent() && hibernateOrmConfig.persistenceUnits.isEmpty()) ||
hibernateOrmConfig.defaultPersistenceUnit.isAnyPropertySet()) {
Expand All @@ -565,11 +566,8 @@ private void handleHibernateORMWithNoPersistenceXml(
modelClassesPerPersistencesUnits.getOrDefault(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME,
Collections.emptySet()),
jdbcDataSources, applicationArchivesBuildItem, launchMode, capabilities,
systemProperties, nativeImageResources, hotDeploymentWatchedFiles, persistenceUnitDescriptors);

if (hibernateOrmConfig.defaultPersistenceUnit.dialect.storageEngine.isPresent()) {
storageEngines.add(hibernateOrmConfig.defaultPersistenceUnit.dialect.storageEngine.get());
}
systemProperties, nativeImageResources, hotDeploymentWatchedFiles, persistenceUnitDescriptors,
storageEngineCollector);
}

for (Entry<String, HibernateOrmConfigPersistenceUnit> persistenceUnitEntry : hibernateOrmConfig.persistenceUnits
Expand All @@ -578,14 +576,11 @@ private void handleHibernateORMWithNoPersistenceXml(
hibernateOrmConfig, persistenceUnitEntry.getKey(), persistenceUnitEntry.getValue(),
modelClassesPerPersistencesUnits.getOrDefault(persistenceUnitEntry.getKey(), Collections.emptySet()),
jdbcDataSources, applicationArchivesBuildItem, launchMode, capabilities,
systemProperties, nativeImageResources, hotDeploymentWatchedFiles, persistenceUnitDescriptors);

if (persistenceUnitEntry.getValue().dialect.storageEngine.isPresent()) {
storageEngines.add(persistenceUnitEntry.getValue().dialect.storageEngine.get());
}
systemProperties, nativeImageResources, hotDeploymentWatchedFiles, persistenceUnitDescriptors,
storageEngineCollector);
}

if (storageEngines.size() > 1) {
if (storageEngineCollector.size() > 1) {
throw new ConfigurationException(
"The dialect storage engine is a global configuration property: it must be consistent across all persistence units.");
}
Expand All @@ -603,7 +598,8 @@ private static void producePersistenceUnitDescriptorFromConfig(
BuildProducer<SystemPropertyBuildItem> systemProperties,
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles,
BuildProducer<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptors) {
BuildProducer<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptors,
Set<String> storageEngineCollector) {
// Find the associated datasource
JdbcDataSourceBuildItem jdbcDataSource;
String dataSource;
Expand Down Expand Up @@ -786,6 +782,11 @@ private static void producePersistenceUnitDescriptorFromConfig(
descriptor.getProperties().setProperty(AvailableSettings.JPA_VALIDATION_MODE, ValidationMode.CALLBACK.name());
}

// Collect the storage engines if MySQL or MariaDB
if (isMySQLOrMariaDB(dialect.get()) && persistenceUnitConfig.dialect.storageEngine.isPresent()) {
storageEngineCollector.add(persistenceUnitConfig.dialect.storageEngine.get());
}

persistenceUnitDescriptors.produce(
new PersistenceUnitDescriptorBuildItem(descriptor, dataSource,
getMultiTenancyStrategy(hibernateOrmConfig),
Expand Down Expand Up @@ -999,4 +1000,9 @@ private static Class[] toArray(final Set<Class<?>> interfaces) {
}
return interfaces.toArray(new Class[interfaces.size()]);
}

private static boolean isMySQLOrMariaDB(String dialect) {
String lowercaseDialect = dialect.toLowerCase(Locale.ROOT);
return lowercaseDialect.contains("mysql") || lowercaseDialect.contains("mariadb");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.hibernate.orm.multiplepersistenceunits;

import org.hibernate.dialect.H2Dialect;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -30,4 +31,10 @@ public void testInvalidConfiguration() {
// deployment exception should happen first
Assertions.fail();
}

/**
* This is just to have the dialect matching MySQL and trigger the MySQL + storage engines check.
*/
public static class H2DialectWithMySQLInTheName extends H2Dialect {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ quarkus.datasource.users.jdbc.url=jdbc:h2:mem:users;DB_CLOSE_DELAY=-1
quarkus.datasource.inventory.db-kind=h2
quarkus.datasource.inventory.jdbc.url=jdbc:h2:mem:inventory;DB_CLOSE_DELAY=-1

quarkus.hibernate-orm.dialect=org.hibernate.dialect.H2Dialect
quarkus.hibernate-orm.dialect=io.quarkus.hibernate.orm.multiplepersistenceunits.MultiplePersistenceUnitsInconsistentStorageEnginesTest$H2DialectWithMySQLInTheName
quarkus.hibernate-orm.dialect.storage-engine=engine1
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.packages=io.quarkus.hibernate.orm.multiplepersistenceunits.model

quarkus.hibernate-orm."users".dialect=org.hibernate.dialect.H2Dialect
quarkus.hibernate-orm."users".dialect=io.quarkus.hibernate.orm.multiplepersistenceunits.MultiplePersistenceUnitsInconsistentStorageEnginesTest$H2DialectWithMySQLInTheName
quarkus.hibernate-orm."users".dialect.storage-engine=engine2
quarkus.hibernate-orm."users".database.generation=drop-and-create
quarkus.hibernate-orm."users".datasource=users
quarkus.hibernate-orm."users".packages=io.quarkus.hibernate.orm.multiplepersistenceunits.model.user

quarkus.hibernate-orm."inventory".dialect=org.hibernate.dialect.H2Dialect
quarkus.hibernate-orm."inventory".dialect=io.quarkus.hibernate.orm.multiplepersistenceunits.MultiplePersistenceUnitsInconsistentStorageEnginesTest$H2DialectWithMySQLInTheName
quarkus.hibernate-orm."inventory".database.generation=drop-and-create
quarkus.hibernate-orm."inventory".datasource=inventory
quarkus.hibernate-orm."inventory".packages=io.quarkus.hibernate.orm.multiplepersistenceunits.model.inventory

0 comments on commit 140dfcf

Please sign in to comment.