Skip to content

Commit

Permalink
Allow to configure HBM2DDL script generation via application.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasHofman committed Apr 13, 2021
1 parent af74476 commit 44f9fe7
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
Expand Up @@ -340,6 +340,19 @@ private static void injectRuntimeConfiguration(String persistenceUnitName,
runtimeSettingsBuilder.put(AvailableSettings.HBM2DDL_HALT_ON_ERROR, "true");
}

runtimeSettingsBuilder.put(AvailableSettings.HBM2DDL_SCRIPTS_ACTION,
persistenceUnitConfig.scripts.generation.generation);

if (persistenceUnitConfig.scripts.generation.createTarget.isPresent()) {
runtimeSettingsBuilder.put(AvailableSettings.HBM2DDL_SCRIPTS_CREATE_TARGET,
persistenceUnitConfig.scripts.generation.createTarget.get());
}

if (persistenceUnitConfig.scripts.generation.dropTarget.isPresent()) {
runtimeSettingsBuilder.put(AvailableSettings.HBM2DDL_SCRIPTS_DROP_TARGET,
persistenceUnitConfig.scripts.generation.dropTarget.get());
}

// Logging
if (persistenceUnitConfig.log.sql) {
runtimeSettingsBuilder.put(AvailableSettings.SHOW_SQL, "true");
Expand Down
Expand Up @@ -16,6 +16,13 @@ public class HibernateOrmRuntimeConfigPersistenceUnit {
@ConfigDocSection
public HibernateOrmConfigPersistenceUnitDatabase database = new HibernateOrmConfigPersistenceUnitDatabase();

/**
* Database scripts related configuration.
*/
@ConfigItem
@ConfigDocSection
public HibernateOrmConfigPersistenceUnitScripts scripts = new HibernateOrmConfigPersistenceUnitScripts();

/**
* Logging configuration.
*/
Expand All @@ -25,6 +32,7 @@ public class HibernateOrmRuntimeConfigPersistenceUnit {

public boolean isAnyPropertySet() {
return database.isAnyPropertySet() ||
scripts.isAnyPropertySet() ||
log.isAnyPropertySet();
}

Expand All @@ -42,6 +50,20 @@ public boolean isAnyPropertySet() {
}
}

@ConfigGroup
public static class HibernateOrmConfigPersistenceUnitScripts {

/**
* Schema generation configuration.
*/
@ConfigItem
public HibernateOrmConfigPersistenceUnitScriptGeneration generation = new HibernateOrmConfigPersistenceUnitScriptGeneration();

public boolean isAnyPropertySet() {
return generation.isAnyPropertySet();
}
}

@ConfigGroup
public static class HibernateOrmConfigPersistenceUnitDatabaseGeneration {

Expand Down Expand Up @@ -74,6 +96,36 @@ public boolean isAnyPropertySet() {
}
}

@ConfigGroup
public static class HibernateOrmConfigPersistenceUnitScriptGeneration {

/**
* Select whether the database schema DDL files are generated or not.
*
* Accepted values: `none`, `create`, `drop-and-create`, `drop`, `update`.
*/
@ConfigItem(name = ConfigItem.PARENT, defaultValue = "none")
public String generation = "none";

/**
* Filename or URL where the database create DDL file should be generated.
*/
@ConfigItem
public Optional<String> createTarget = Optional.empty();

/**
* Filename or URL where the database drop DDL file should be generated.
*/
@ConfigItem
public Optional<String> dropTarget = Optional.empty();

public boolean isAnyPropertySet() {
return !"none".equals(generation)
|| createTarget.isPresent()
|| dropTarget.isPresent();
}
}

@ConfigGroup
public static class HibernateOrmConfigPersistenceUnitLog {

Expand Down
@@ -0,0 +1,13 @@
quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:test
quarkus.datasource.jdbc.max-size=8

quarkus.hibernate-orm.dialect=org.hibernate.dialect.H2Dialect
quarkus.hibernate-orm.database.generation=drop-and-create

quarkus.hibernate-orm.scripts.generation=drop-and-create
quarkus.hibernate-orm.scripts.generation.create-target=create.sql
quarkus.hibernate-orm.scripts.generation.drop-target=drop.sql

quarkus.hibernate-orm.statistics=true
quarkus.hibernate-orm.metrics.enabled=true
@@ -0,0 +1,43 @@
package io.quarkus.it.panache;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.builder.Version;
import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;
import io.restassured.RestAssured;

/**
* Verifies that DDL scripts are generated when script generation is configured in application.properties.
*/
public class DDLGenerationPMT {

@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(PageItem.class, TestResources.class, NoPagingTestEndpoint.class))
.setApplicationName("ddl-generation")
.setApplicationVersion(Version.getVersion())
.setRun(true)
.setLogFileName("ddl-generation-test.log")
.withConfigurationResource("ddlgeneration.properties");

@ProdBuildResults
private ProdModeTestResults prodModeTestResults;

@Test
public void test() {
RestAssured.when().get("/no-paging-test").then().body(is("OK"));

assertThat(prodModeTestResults.getBuildDir().resolve("quarkus-app/create.sql").toFile().exists()).isTrue();
assertThat(prodModeTestResults.getBuildDir().resolve("quarkus-app/drop.sql").toFile().exists()).isTrue();
}

}

0 comments on commit 44f9fe7

Please sign in to comment.