-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21351 from yrodiere/i19660-default-schema
Expose quarkus.hibernate-orm.database.default-catalog and quarkus.hibernate-orm.database.default-schema as runtime properties
- Loading branch information
Showing
12 changed files
with
168 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
.../main/java/io/quarkus/it/jpa/defaultcatalogandschema/DefaultCatalogAndSchemaResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ain/java/io/quarkus/it/jpa/defaultcatalogandschema/EntityWithDefaultCatalogAndSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
23 changes: 23 additions & 0 deletions
23
...ain/java/io/quarkus/it/jpa/defaultcatalogandschema/Schema1MetadataBuilderContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...a/src/test/java/io/quarkus/it/jpa/defaultcatalogandschema/DefaultSchemaInGraalITCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
36 changes: 36 additions & 0 deletions
36
...-tests/jpa/src/test/java/io/quarkus/it/jpa/defaultcatalogandschema/DefaultSchemaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |