From 7da105be0ab0a217f955858baaaec7940db4315f Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 26 Jul 2018 15:20:05 +0100 Subject: [PATCH] HHH-12861 SchemaUpdate doesn't work with Sybase --- .../org/hibernate/dialect/SybaseDialect.java | 5 + ...dateWithKeywordAutoQuotingEnabledTest.java | 45 ++++---- .../NumericValidationTest.java | 100 +++++++----------- 3 files changed, 73 insertions(+), 77 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/SybaseDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/SybaseDialect.java index 19020e9329..b20284657c 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/SybaseDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/SybaseDialect.java @@ -43,4 +43,9 @@ public class SybaseDialect extends AbstractTransactSQLDialect { public String getNullColumnString() { return " null"; } + + @Override + public String getCurrentSchemaCommand() { + return "select db_name()"; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/SchemaUpdateWithKeywordAutoQuotingEnabledTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/SchemaUpdateWithKeywordAutoQuotingEnabledTest.java index 5e582570b2..b1e903ab95 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/SchemaUpdateWithKeywordAutoQuotingEnabledTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/foreignkeys/SchemaUpdateWithKeywordAutoQuotingEnabledTest.java @@ -6,16 +6,15 @@ */ package org.hibernate.test.schemaupdate.foreignkeys; +import java.util.EnumSet; +import java.util.Map; +import java.util.TreeMap; import javax.persistence.CollectionTable; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.Table; -import java.io.IOException; -import java.util.EnumSet; -import java.util.Map; -import java.util.TreeMap; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; @@ -25,13 +24,12 @@ import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.tool.hbm2ddl.SchemaUpdate; import org.hibernate.tool.schema.TargetType; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseUnitTestCase; - /** * @author Andrea Boriero */ @@ -41,36 +39,36 @@ public class SchemaUpdateWithKeywordAutoQuotingEnabledTest extends BaseUnitTestC private MetadataImplementor metadata; @Before - public void setUp() throws IOException { + public void setUp() { final StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder(); standardServiceRegistryBuilder.applySetting( org.hibernate.cfg.AvailableSettings.KEYWORD_AUTO_QUOTING_ENABLED, "true" ); ssr = standardServiceRegistryBuilder.build(); - final MetadataSources metadataSources = new MetadataSources( ssr ); + final MetadataSources metadataSources = new MetadataSources( ssr ); metadataSources.addAnnotatedClass( Match.class ); metadata = (MetadataImplementor) metadataSources.buildMetadata(); metadata.validate(); - - new SchemaExport().setHaltOnError( true ) - .setFormat( false ) - .createOnly( EnumSet.of( TargetType.DATABASE ), metadata ); + try { + createSchema(); + } + catch (Exception e) { + tearDown(); + throw e; + } } @After - public void tearsDown() { - new SchemaExport().setHaltOnError( true ) - .setFormat( false ) - .drop( EnumSet.of( TargetType.DATABASE ), metadata ); + public void tearDown() { + dropSchema(); StandardServiceRegistryBuilder.destroy( ssr ); } @Test public void testUpdate() { new SchemaUpdate().setHaltOnError( true ) - .setFormat( false ) .execute( EnumSet.of( TargetType.DATABASE ), metadata ); } @@ -84,4 +82,15 @@ public class SchemaUpdateWithKeywordAutoQuotingEnabledTest extends BaseUnitTestC @CollectionTable private Map timeline = new TreeMap<>(); } + + private void createSchema() { + dropSchema(); + new SchemaExport().setHaltOnError( true ) + .createOnly( EnumSet.of( TargetType.DATABASE ), metadata ); + } + + private void dropSchema() { + new SchemaExport() + .drop( EnumSet.of( TargetType.DATABASE ), metadata ); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/NumericValidationTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/NumericValidationTest.java index b39bf2fcfa..536d6f7607 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/NumericValidationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/NumericValidationTest.java @@ -14,7 +14,6 @@ import java.util.Map; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; -import javax.persistence.Table; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; @@ -22,6 +21,7 @@ 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; @@ -50,7 +50,10 @@ public class NumericValidationTest implements ExecutionOptions { @Parameterized.Parameters public static Collection parameters() { return Arrays.asList( - new String[] {JdbcMetadaAccessStrategy.GROUPED.toString(), JdbcMetadaAccessStrategy.INDIVIDUALLY.toString()} + new String[] { + JdbcMetadaAccessStrategy.GROUPED.toString(), + JdbcMetadaAccessStrategy.INDIVIDUALLY.toString() + } ); } @@ -58,16 +61,35 @@ public class NumericValidationTest implements ExecutionOptions { public String jdbcMetadataExtractorStrategy; private StandardServiceRegistry ssr; + private MetadataImplementor metadata; @Before public void beforeTest() { ssr = new StandardServiceRegistryBuilder() - .applySetting( AvailableSettings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, jdbcMetadataExtractorStrategy ) + .applySetting( + AvailableSettings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, + jdbcMetadataExtractorStrategy + ) .build(); + 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 afterTest() { + public void tearDown() { + dropSchema(); if ( ssr != null ) { StandardServiceRegistryBuilder.destroy( ssr ); } @@ -75,31 +97,15 @@ public class NumericValidationTest implements ExecutionOptions { @Test public void testValidation() { - MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) - .addAnnotatedClass( TestEntity.class ) - .buildMetadata(); - metadata.validate(); - - - // create the schema - createSchema( metadata ); - - try { - doValidation( metadata ); - } - finally { - dropSchema( metadata ); - } + doValidation(); } - private void doValidation(MetadataImplementor metadata) { - ssr.getService( SchemaManagementTool.class ).getSchemaValidator( null ).doValidation( - metadata, - this - ); + private void doValidation() { + ssr.getService( SchemaManagementTool.class ).getSchemaValidator( null ) + .doValidation( metadata, this ); } - private void createSchema(MetadataImplementor metadata) { + private void createSchema() { ssr.getService( SchemaManagementTool.class ).getSchemaCreator( null ).doCreation( metadata, this, @@ -128,43 +134,19 @@ public class NumericValidationTest implements ExecutionOptions { ); } - private void dropSchema(MetadataImplementor metadata) { - ssr.getService( SchemaManagementTool.class ).getSchemaDropper( null ).doDrop( - metadata, - this, - new SourceDescriptor() { - @Override - public SourceType getSourceType() { - return SourceType.METADATA; - } - - @Override - public ScriptSourceInput getScriptSourceInput() { - return null; - } - }, - new TargetDescriptor() { - @Override - public EnumSet getTargetTypes() { - return EnumSet.of( TargetType.DATABASE ); - } - - @Override - public ScriptTargetOutput getScriptTargetOutput() { - return null; - } - } - ); + private void dropSchema() { + new SchemaExport() + .drop( EnumSet.of( TargetType.DATABASE ), metadata ); } -@Entity(name = "TestEntity") -public static class TestEntity { - @Id - public Integer id; + @Entity(name = "TestEntity") + public static class TestEntity { + @Id + public Integer id; - @Column(name = "numberValue") - BigDecimal number; -} + @Column(name = "numberValue") + BigDecimal number; + } @Override public Map getConfigurationValues() {