Skip to content

Commit

Permalink
Merge pull request #21351 from yrodiere/i19660-default-schema
Browse files Browse the repository at this point in the history
Expose quarkus.hibernate-orm.database.default-catalog and quarkus.hibernate-orm.database.default-schema as runtime properties
  • Loading branch information
gsmet authored Dec 18, 2021
2 parents 6badaca + ca3f7d8 commit b93e50e
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,6 @@ public static class HibernateOrmConfigPersistenceUnitDatabase {

private static final String DEFAULT_CHARSET = "UTF-8";

/**
* The default catalog to use for the database objects.
*/
@ConfigItem
public Optional<String> defaultCatalog;

/**
* The default schema to use for the database objects.
*/
@ConfigItem
public Optional<String> defaultSchema;

/**
* The charset of the database.
* <p>
Expand All @@ -342,9 +330,7 @@ public static class HibernateOrmConfigPersistenceUnitDatabase {
public boolean globallyQuotedIdentifiers;

public boolean isAnyPropertySet() {
return defaultCatalog.isPresent()
|| defaultSchema.isPresent()
|| !DEFAULT_CHARSET.equals(charset.name())
return !DEFAULT_CHARSET.equals(charset.name())
|| globallyQuotedIdentifiers;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1039,12 +1039,6 @@ private static void producePersistenceUnitDescriptorFromConfig(
descriptor.getProperties().setProperty(AvailableSettings.HBM2DDL_CHARSET_NAME,
persistenceUnitConfig.database.charset.name());

persistenceUnitConfig.database.defaultCatalog.ifPresent(
catalog -> descriptor.getProperties().setProperty(AvailableSettings.DEFAULT_CATALOG, catalog));

persistenceUnitConfig.database.defaultSchema.ifPresent(
schema -> descriptor.getProperties().setProperty(AvailableSettings.DEFAULT_SCHEMA, schema));

if (persistenceUnitConfig.database.globallyQuotedIdentifiers) {
descriptor.getProperties().setProperty(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, "true");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ private static void injectRuntimeConfiguration(String persistenceUnitName,
persistenceUnitConfig.scripts.generation.dropTarget.get());
}

persistenceUnitConfig.database.defaultCatalog.ifPresent(
catalog -> runtimeSettingsBuilder.put(AvailableSettings.DEFAULT_CATALOG, catalog));

persistenceUnitConfig.database.defaultSchema.ifPresent(
schema -> runtimeSettingsBuilder.put(AvailableSettings.DEFAULT_SCHEMA, schema));

// Logging
if (persistenceUnitConfig.log.sql) {
runtimeSettingsBuilder.put(AvailableSettings.SHOW_SQL, "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,22 @@ public static class HibernateOrmConfigPersistenceUnitDatabase {
@ConfigItem
public HibernateOrmConfigPersistenceUnitDatabaseGeneration generation = new HibernateOrmConfigPersistenceUnitDatabaseGeneration();

/**
* The default catalog to use for the database objects.
*/
@ConfigItem
public Optional<String> defaultCatalog;

/**
* The default schema to use for the database objects.
*/
@ConfigItem
public Optional<String> defaultSchema;

public boolean isAnyPropertySet() {
return generation.isAnyPropertySet();
return generation.isAnyPropertySet()
|| defaultCatalog.isPresent()
|| defaultSchema.isPresent();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,6 @@ private static ParsedPersistenceXmlDescriptor generateReactivePersistenceUnit(
desc.getProperties().setProperty(AvailableSettings.HBM2DDL_CHARSET_NAME,
persistenceUnitConfig.database.charset.name());

persistenceUnitConfig.database.defaultCatalog.ifPresent(
catalog -> desc.getProperties().setProperty(AvailableSettings.DEFAULT_CATALOG, catalog));

persistenceUnitConfig.database.defaultSchema.ifPresent(
schema -> desc.getProperties().setProperty(AvailableSettings.DEFAULT_SCHEMA, schema));

if (persistenceUnitConfig.database.globallyQuotedIdentifiers) {
desc.getProperties().setProperty(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, "true");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ private static void injectRuntimeConfiguration(String persistenceUnitName,
runtimeSettingsBuilder.put(AvailableSettings.HBM2DDL_HALT_ON_ERROR, "true");
}

persistenceUnitConfig.database.defaultCatalog.ifPresent(
catalog -> runtimeSettingsBuilder.put(AvailableSettings.DEFAULT_CATALOG, catalog));

persistenceUnitConfig.database.defaultSchema.ifPresent(
schema -> runtimeSettingsBuilder.put(AvailableSettings.DEFAULT_SCHEMA, schema));

// Logging
if (persistenceUnitConfig.log.sql) {
runtimeSettingsBuilder.put(AvailableSettings.SHOW_SQL, "true");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.quarkus.it.jpa.defaultcatalogandschema;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.Session;
import org.hibernate.type.LongType;
import org.jboss.resteasy.annotations.jaxrs.QueryParam;

@Path("/default-catalog-and-schema")
@ApplicationScoped
public class DefaultCatalogAndSchemaResource {

@Inject
Session session;

@GET
@Path("/test")
@Produces(MediaType.TEXT_PLAIN)
@Transactional
public String test(@QueryParam String expectedSchema) {
assertThat(findUsingNativeQuery(expectedSchema, "foo")).isEmpty();

EntityWithDefaultCatalogAndSchema entity = new EntityWithDefaultCatalogAndSchema();
entity.basic = "foo";
session.persist(entity);
session.flush();
session.clear();

assertThat(findUsingNativeQuery(expectedSchema, "foo")).containsExactly(entity.id);

return "OK";
}

@SuppressWarnings("unchecked")
private List<Long> findUsingNativeQuery(String schema, String value) {
return session
.createNativeQuery(
"select id from \"" + schema + "\"." + EntityWithDefaultCatalogAndSchema.NAME
+ " where basic = :basic")
.addScalar("id", LongType.INSTANCE)
.setParameter("basic", value)
.getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.it.jpa.defaultcatalogandschema;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity(name = EntityWithDefaultCatalogAndSchema.NAME)
public class EntityWithDefaultCatalogAndSchema {
public static final String NAME = "ent_with_defaults";

@Id
@GeneratedValue
public Long id;

@Basic
public String basic;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.it.jpa.defaultcatalogandschema;

import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.model.relational.AbstractAuxiliaryDatabaseObject;
import org.hibernate.boot.spi.MetadataBuilderContributor;
import org.hibernate.dialect.Dialect;

public class Schema1MetadataBuilderContributor implements MetadataBuilderContributor {
@Override
public void contribute(MetadataBuilder metadataBuilder) {
metadataBuilder.applyAuxiliaryDatabaseObject(new AbstractAuxiliaryDatabaseObject(true) {
@Override
public String[] sqlCreateStrings(Dialect dialect) {
return new String[] { "create schema \"SCHEMA1\"" };
}

@Override
public String[] sqlDropStrings(Dialect dialect) {
return new String[0];
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:test

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

quarkus.hibernate-orm.metadata-builder-contributor=io.quarkus.it.jpa.defaultcatalogandschema.Schema1MetadataBuilderContributor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.it.jpa.defaultcatalogandschema;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class DefaultSchemaInGraalITCase extends DefaultSchemaTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.quarkus.it.jpa.defaultcatalogandschema;

import static io.restassured.RestAssured.given;
import static org.hamcrest.core.Is.is;

import java.util.Map;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;

@QuarkusTest
@TestProfile(DefaultSchemaTest.DefaultSchema1Profile.class)
public class DefaultSchemaTest {
public static class DefaultSchema1Profile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"quarkus.hibernate-orm.database.default-schema", "SCHEMA1",
// import.sql doesn't take our custom schema into account.
// It should be modified in order to work in this test, but we simply don't need it.
"quarkus.hibernate-orm.sql-load-script", "no-file");
}
}

@Test
public void test() {
given().queryParam("expectedSchema", "SCHEMA1")
.when().get("/jpa-test/default-catalog-and-schema/test").then()
.body(is("OK"))
.statusCode(200);
}

}

0 comments on commit b93e50e

Please sign in to comment.