Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-15548 Fix schema validation issues on PostgreSQL with Instant type
- Loading branch information
Showing
4 changed files
with
234 additions
and
19 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
186 changes: 186 additions & 0 deletions
186
...ate-core/src/test/java/org/hibernate/orm/test/schemavalidation/InstantValidationTest.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,186 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.orm.test.schemavalidation; | ||
|
||
import java.time.Instant; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.EnumSet; | ||
import java.util.Map; | ||
|
||
import org.hibernate.boot.MetadataSources; | ||
import org.hibernate.boot.registry.StandardServiceRegistry; | ||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.boot.spi.MetadataImplementor; | ||
import org.hibernate.cfg.AvailableSettings; | ||
import org.hibernate.engine.config.spi.ConfigurationService; | ||
import org.hibernate.tool.hbm2ddl.SchemaExport; | ||
import org.hibernate.tool.schema.JdbcMetadaAccessStrategy; | ||
import org.hibernate.tool.schema.SourceType; | ||
import org.hibernate.tool.schema.TargetType; | ||
import org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl; | ||
import org.hibernate.tool.schema.spi.ContributableMatcher; | ||
import org.hibernate.tool.schema.spi.ExceptionHandler; | ||
import org.hibernate.tool.schema.spi.ExecutionOptions; | ||
import org.hibernate.tool.schema.spi.SchemaFilter; | ||
import org.hibernate.tool.schema.spi.SchemaManagementTool; | ||
import org.hibernate.tool.schema.spi.ScriptSourceInput; | ||
import org.hibernate.tool.schema.spi.ScriptTargetOutput; | ||
import org.hibernate.tool.schema.spi.SourceDescriptor; | ||
import org.hibernate.tool.schema.spi.TargetDescriptor; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
|
||
/** | ||
* Test that an existing timestamp with timezone column works for fields that use java.time.Instant. | ||
*/ | ||
@TestForIssue(jiraKey = "HHH-15548") | ||
@RunWith(Parameterized.class) | ||
public class InstantValidationTest implements ExecutionOptions { | ||
@Parameterized.Parameters | ||
public static Collection<String> parameters() { | ||
return Arrays.asList( | ||
JdbcMetadaAccessStrategy.GROUPED.toString(), | ||
JdbcMetadaAccessStrategy.INDIVIDUALLY.toString() | ||
); | ||
} | ||
|
||
@Parameterized.Parameter | ||
public String jdbcMetadataExtractorStrategy; | ||
|
||
private StandardServiceRegistry ssr; | ||
private MetadataImplementor metadata; | ||
private MetadataImplementor oldMetadata; | ||
|
||
@Before | ||
public void beforeTest() { | ||
ssr = new StandardServiceRegistryBuilder() | ||
.applySetting( | ||
AvailableSettings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, | ||
jdbcMetadataExtractorStrategy | ||
) | ||
.build(); | ||
oldMetadata = (MetadataImplementor) new MetadataSources( ssr ) | ||
.addAnnotatedClass( TestEntityOld.class ) | ||
.buildMetadata(); | ||
oldMetadata.validate(); | ||
metadata = (MetadataImplementor) new MetadataSources( ssr ) | ||
.addAnnotatedClass( TestEntity.class ) | ||
.buildMetadata(); | ||
metadata.validate(); | ||
|
||
try { | ||
dropSchema(); | ||
// create the schema | ||
createSchema(); | ||
} | ||
catch (Exception e) { | ||
tearDown(); | ||
throw e; | ||
} | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
dropSchema(); | ||
if ( ssr != null ) { | ||
StandardServiceRegistryBuilder.destroy( ssr ); | ||
} | ||
} | ||
|
||
@Test | ||
public void testValidation() { | ||
doValidation(); | ||
} | ||
|
||
private void doValidation() { | ||
ssr.getService( SchemaManagementTool.class ).getSchemaValidator( null ) | ||
.doValidation( metadata, this, ContributableMatcher.ALL ); | ||
} | ||
|
||
private void createSchema() { | ||
ssr.getService( SchemaManagementTool.class ).getSchemaCreator( null ).doCreation( | ||
oldMetadata, | ||
this, | ||
ContributableMatcher.ALL, | ||
new SourceDescriptor() { | ||
@Override | ||
public SourceType getSourceType() { | ||
return SourceType.METADATA; | ||
} | ||
|
||
@Override | ||
public ScriptSourceInput getScriptSourceInput() { | ||
return null; | ||
} | ||
}, | ||
new TargetDescriptor() { | ||
@Override | ||
public EnumSet<TargetType> getTargetTypes() { | ||
return EnumSet.of( TargetType.DATABASE ); | ||
} | ||
|
||
@Override | ||
public ScriptTargetOutput getScriptTargetOutput() { | ||
return null; | ||
} | ||
} | ||
); | ||
} | ||
|
||
private void dropSchema() { | ||
new SchemaExport() | ||
.drop( EnumSet.of( TargetType.DATABASE ), oldMetadata ); | ||
} | ||
|
||
@Entity(name = "TestEntity") | ||
public static class TestEntityOld { | ||
@Id | ||
public Integer id; | ||
|
||
@Column(name = "instantVal") | ||
Instant instantVal; | ||
} | ||
|
||
@Entity(name = "TestEntity") | ||
public static class TestEntity { | ||
@Id | ||
public Integer id; | ||
|
||
@Column(name = "instantVal") | ||
Instant instantVal; | ||
} | ||
|
||
@Override | ||
public Map getConfigurationValues() { | ||
return ssr.getService( ConfigurationService.class ).getSettings(); | ||
} | ||
|
||
@Override | ||
public boolean shouldManageNamespaces() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public ExceptionHandler getExceptionHandler() { | ||
return ExceptionHandlerLoggedImpl.INSTANCE; | ||
} | ||
|
||
@Override | ||
public SchemaFilter getSchemaFilter() { | ||
return SchemaFilter.ALL; | ||
} | ||
} |