Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to drop-and-create for DevServices #19951

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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