From 19566de4ef3c82173f7e6f7bd06b8e072ff68942 Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Tue, 7 Sep 2021 15:49:55 +1000 Subject: [PATCH] Default to drop-and-create for DevServices This allows for hibernate to be used without configuration. --- .../orm/deployment/HibernateOrmProcessor.java | 52 +++++++++++++++++++ ...ernateOrmRuntimeConfigPersistenceUnit.java | 3 ++ .../deployment/test/PanacheJAXBTest.java | 5 +- ...eous-multiple-persistence-units.properties | 2 +- ...ersistence-units-for-repository.properties | 2 +- ...tion-multiple-persistence-units.properties | 2 +- 6 files changed, 62 insertions(+), 4 deletions(-) diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java index 214456677d76f..b51cb54fb6641 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java @@ -41,6 +41,7 @@ import javax.persistence.spi.PersistenceUnitTransactionType; import javax.transaction.TransactionManager; +import org.eclipse.microprofile.config.ConfigProvider; import org.hibernate.MultiTenancyStrategy; import org.hibernate.boot.archive.scan.spi.ClassDescriptor; import org.hibernate.boot.archive.scan.spi.PackageDescriptor; @@ -87,12 +88,14 @@ import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem; import io.quarkus.deployment.builditem.BytecodeTransformerBuildItem; import io.quarkus.deployment.builditem.CombinedIndexBuildItem; +import io.quarkus.deployment.builditem.DevServicesLauncherConfigResultBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; import io.quarkus.deployment.builditem.GeneratedClassBuildItem; import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem; import io.quarkus.deployment.builditem.LaunchModeBuildItem; import io.quarkus.deployment.builditem.LiveReloadBuildItem; import io.quarkus.deployment.builditem.LogCategoryBuildItem; +import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem; import io.quarkus.deployment.builditem.ServiceStartBuildItem; import io.quarkus.deployment.builditem.SystemPropertyBuildItem; import io.quarkus.deployment.builditem.nativeimage.NativeImageProxyDefinitionBuildItem; @@ -177,6 +180,55 @@ void includeArchivesHostingEntityPackagesInIndex(HibernateOrmConfig hibernateOrm } } + @BuildStep + void devServicesAutoGenerateByDefault(DevServicesLauncherConfigResultBuildItem devServicesResult, + List schemaReadyBuildItems, + HibernateOrmConfig config, + BuildProducer runTimeConfigurationDefaultBuildItemBuildProducer) { + if (!schemaReadyBuildItems.isEmpty()) { + //we don't want to enable auto generation if somebody else is managing the schema + return; + } + String dsName; + if (config.defaultPersistenceUnit.datasource.isEmpty()) { + dsName = "quarkus.datasource.username"; + } else { + dsName = "quarkus.datasource." + config.defaultPersistenceUnit.datasource.get() + ".username"; + } + + if (ConfigProvider.getConfig().getOptionalValue(dsName, String.class).isEmpty()) { + if (devServicesResult.getConfig().containsKey(dsName)) { + if (ConfigProvider.getConfig().getOptionalValue("quarkus.hibernate-orm.database.generation", String.class) + .isEmpty()) { + LOG.info( + "Setting quarkus.hibernate-orm.database.generation=drop-and-create to initialize Dev Services managed database"); + runTimeConfigurationDefaultBuildItemBuildProducer.produce(new RunTimeConfigurationDefaultBuildItem( + "quarkus.hibernate-orm.database.generation", "drop-and-create")); + } + } + } + + for (Entry entry : config.persistenceUnits.entrySet()) { + + if (entry.getValue().datasource.isEmpty()) { + dsName = "quarkus.datasource.jdbc.url"; + } else { + dsName = "quarkus.datasource." + entry.getValue().datasource.get() + ".username"; + } + if (ConfigProvider.getConfig().getOptionalValue(dsName, String.class).isEmpty()) { + if (devServicesResult.getConfig().containsKey(dsName)) { + String propertyName = "quarkus.hibernate-orm." + entry.getKey() + ".database.generation"; + if (ConfigProvider.getConfig().getOptionalValue(propertyName, String.class).isEmpty()) { + LOG.info("Setting " + propertyName + "=drop-and-create to initialize Dev Services managed database"); + runTimeConfigurationDefaultBuildItemBuildProducer + .produce(new RunTimeConfigurationDefaultBuildItem(propertyName, "drop-and-create")); + } + } + } + } + + } + @BuildStep AdditionalIndexedClassesBuildItem addPersistenceUnitAnnotationToIndex() { return new AdditionalIndexedClassesBuildItem(ClassNames.QUARKUS_PERSISTENCE_UNIT.toString()); diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRuntimeConfigPersistenceUnit.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRuntimeConfigPersistenceUnit.java index 3ca13e90a4354..3be50c5a48834 100644 --- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRuntimeConfigPersistenceUnit.java +++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/HibernateOrmRuntimeConfigPersistenceUnit.java @@ -72,6 +72,9 @@ public static class HibernateOrmConfigPersistenceUnitDatabaseGeneration { * * `drop-and-create` is awesome in development mode. * + * This defaults to 'none', however if Dev Services is in use and no other extensions that manage the schema are present + * this will default to 'drop-and-create'. + * * Accepted values: `none`, `create`, `drop-and-create`, `drop`, `update`, `validate`. */ @ConfigItem(name = ConfigItem.PARENT, defaultValue = "none") diff --git a/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/PanacheJAXBTest.java b/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/PanacheJAXBTest.java index 8672238e57def..ea251d1fcc913 100644 --- a/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/PanacheJAXBTest.java +++ b/extensions/panache/hibernate-orm-panache/deployment/src/test/java/io/quarkus/hibernate/orm/panache/deployment/test/PanacheJAXBTest.java @@ -17,6 +17,7 @@ import javax.xml.bind.annotation.XmlTransient; import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -30,7 +31,9 @@ public class PanacheJAXBTest { @RegisterExtension static QuarkusUnitTest runner = new QuarkusUnitTest() .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) - .addClasses(JAXBEntity.class, JAXBTestResource.class)); + .addClasses(JAXBEntity.class, JAXBTestResource.class) + .addAsResource(new StringAsset("quarkus.hibernate-orm.database.generation=none"), + "application.properties")); @Test public void testJaxbAnnotationTransfer() throws Exception { diff --git a/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-erroneous-multiple-persistence-units.properties b/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-erroneous-multiple-persistence-units.properties index cbcb3eec5ffa3..d6b78c1f0c86d 100644 --- a/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-erroneous-multiple-persistence-units.properties +++ b/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-erroneous-multiple-persistence-units.properties @@ -1,5 +1,5 @@ quarkus.datasource.db-kind=h2 -quarkus.datasource.jdbc.url=jdbc:h2:mem:default;DB_CLOSE_DELAY=-1 +quarkus.datasource.jdbc.url=jdbc:h2:mem:first;DB_CLOSE_DELAY=-1 quarkus.datasource.second.db-kind=h2 quarkus.datasource.second.jdbc.url=jdbc:h2:mem:second;DB_CLOSE_DELAY=-1 diff --git a/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-multiple-persistence-units-for-repository.properties b/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-multiple-persistence-units-for-repository.properties index d257a345b938f..7111224eafffe 100644 --- a/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-multiple-persistence-units-for-repository.properties +++ b/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-multiple-persistence-units-for-repository.properties @@ -1,5 +1,5 @@ quarkus.datasource.db-kind=h2 -quarkus.datasource.jdbc.url=jdbc:h2:mem:default;DB_CLOSE_DELAY=-1 +quarkus.datasource.jdbc.url=jdbc:h2:mem:first;DB_CLOSE_DELAY=-1 quarkus.datasource.repository.db-kind=h2 quarkus.datasource.repository.jdbc.url=jdbc:h2:mem:repository;DB_CLOSE_DELAY=-1 diff --git a/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-multiple-persistence-units.properties b/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-multiple-persistence-units.properties index 777cc07197332..2a5bbd813f053 100644 --- a/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-multiple-persistence-units.properties +++ b/extensions/panache/hibernate-orm-panache/deployment/src/test/resources/application-multiple-persistence-units.properties @@ -1,5 +1,5 @@ quarkus.datasource.db-kind=h2 -quarkus.datasource.jdbc.url=jdbc:h2:mem:default;DB_CLOSE_DELAY=-1 +quarkus.datasource.jdbc.url=jdbc:h2:mem:first;DB_CLOSE_DELAY=-1 quarkus.datasource.second.db-kind=h2 quarkus.datasource.second.jdbc.url=jdbc:h2:mem:second;DB_CLOSE_DELAY=-1