Skip to content

Commit

Permalink
Merge pull request #19951 from stuartwdouglas/auto-create-devservices
Browse files Browse the repository at this point in the history
Default to drop-and-create for DevServices
  • Loading branch information
stuartwdouglas authored Sep 8, 2021
2 parents 0809f59 + 19566de commit 4621f42
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -177,6 +180,55 @@ void includeArchivesHostingEntityPackagesInIndex(HibernateOrmConfig hibernateOrm
}
}

@BuildStep
void devServicesAutoGenerateByDefault(DevServicesLauncherConfigResultBuildItem devServicesResult,
List<JdbcDataSourceSchemaReadyBuildItem> schemaReadyBuildItems,
HibernateOrmConfig config,
BuildProducer<RunTimeConfigurationDefaultBuildItem> 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<String, HibernateOrmConfigPersistenceUnit> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 4621f42

Please sign in to comment.