From 0a2bf3fd5c3cbc75aa1c9c993beec9fe073a83ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Mon, 13 Feb 2023 13:32:22 +0100 Subject: [PATCH] HHH-16177 Test catalog/schema creation/dropping in DefaultCatalogAndSchemaTest --- .../DefaultCatalogAndSchemaTest.java | 66 ++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/boot/database/qualifiedTableNaming/DefaultCatalogAndSchemaTest.java b/hibernate-core/src/test/java/org/hibernate/test/boot/database/qualifiedTableNaming/DefaultCatalogAndSchemaTest.java index d1fe7f74ce..a23b1ac046 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/boot/database/qualifiedTableNaming/DefaultCatalogAndSchemaTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/boot/database/qualifiedTableNaming/DefaultCatalogAndSchemaTest.java @@ -52,11 +52,13 @@ import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Environment; +import org.hibernate.dialect.Dialect; import org.hibernate.dialect.MariaDB102Dialect; import org.hibernate.dialect.MariaDB10Dialect; import org.hibernate.dialect.MariaDB53Dialect; import org.hibernate.dialect.MySQLDialect; import org.hibernate.dialect.SQLServer2012Dialect; +import org.hibernate.dialect.SQLServerDialect; import org.hibernate.dialect.SybaseDialect; import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport; @@ -77,6 +79,7 @@ import org.hibernate.tool.schema.spi.TargetDescriptor; import org.hibernate.testing.AfterClassOnce; import org.hibernate.testing.BeforeClassOnce; import org.hibernate.testing.SkipForDialect; +import org.hibernate.testing.SkipForDialect; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.jdbc.SharedDriverManagerConnectionProviderImpl; import org.hibernate.testing.junit4.CustomParameterized; @@ -85,7 +88,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @RunWith(CustomParameterized.class) -@TestForIssue(jiraKey = { "HHH-14921", "HHH-14922", "HHH-15212" }) +@TestForIssue(jiraKey = { "HHH-14921", "HHH-14922", "HHH-15212", "HHH-16177" }) @SkipForDialect(value = SybaseDialect.class, comment = "Sybase doesn't support sequences") @SkipForDialect(value = MySQLDialect.class, comment = "MySQL doesn't support sequences") @SkipForDialect(value = MariaDB53Dialect.class, strictMatching = true, @@ -100,6 +103,8 @@ public class DefaultCatalogAndSchemaTest { private static final String EXPLICIT_CATALOG = "someExplicitCatalog"; private static final String EXPLICIT_SCHEMA = "someExplicitSchema"; + private static final String IMPLICIT_FILE_LEVEL_CATALOG = "someImplicitFileLevelCatalog"; + private static final String IMPLICIT_FILE_LEVEL_SCHEMA = "someImplicitFileLevelSchema"; // Yes this is invalid SQL, and in most cases it simply wouldn't work because of missing columns, // but in this case we don't care: we just want to check catalog/schema substitution. @@ -288,6 +293,7 @@ public class DefaultCatalogAndSchemaTest { final Map settings = new HashMap<>(); settings.put( GlobalTemporaryTableBulkIdStrategy.DROP_ID_TABLES, "true" ); settings.put( LocalTemporaryTableBulkIdStrategy.DROP_ID_TABLES, "true" ); + settings.put( AvailableSettings.JAKARTA_HBM2DDL_CREATE_SCHEMAS, "true" ); if ( !Environment.getProperties().containsKey( Environment.CONNECTION_PROVIDER ) ) { settings.put( AvailableSettings.CONNECTION_PROVIDER, @@ -311,24 +317,40 @@ public class DefaultCatalogAndSchemaTest { @Test public void createSchema_fromSessionFactory() { String script = generateScriptFromSessionFactory( "create" ); + verifyDDLCreateCatalogOrSchema( script ); + verifyDDLQualifiers( script ); + } + + @Test + @SkipForDialect(value = { SQLServerDialect.class, SybaseDialect.class }, + comment = "SQL Server and Sybase support catalogs but their implementation of DatabaseMetaData" + + " throws exceptions when calling getSchemas/getTables with a non-existing catalog," + + " which results in nasty errors when generating an update script" + + " and some catalogs don't exist.") + public void updateSchema_fromSessionFactory() { + String script = generateScriptFromSessionFactory( "update" ); + verifyDDLCreateCatalogOrSchema( script ); verifyDDLQualifiers( script ); } @Test public void dropSchema_fromSessionFactory() { String script = generateScriptFromSessionFactory( "drop" ); + verifyDDLDropCatalogOrSchema( script ); verifyDDLQualifiers( script ); } @Test public void createSchema_fromMetadata() { String script = generateScriptFromMetadata( SchemaExport.Action.CREATE ); + verifyDDLCreateCatalogOrSchema( script ); verifyDDLQualifiers( script ); } @Test public void dropSchema_fromMetadata() { String script = generateScriptFromMetadata( SchemaExport.Action.DROP ); + verifyDDLDropCatalogOrSchema( script ); verifyDDLQualifiers( script ); } @@ -550,6 +572,46 @@ public class DefaultCatalogAndSchemaTest { return expectedType.cast( persister.getIdentifierGenerator() ); } + private void verifyDDLCreateCatalogOrSchema(String sql) { + Dialect dialect = sessionFactory.getJdbcServices().getDialect(); + + if ( sessionFactory.getJdbcServices().getDialect().canCreateCatalog() ) { + assertThat( sql ).contains( dialect.getCreateCatalogCommand( EXPLICIT_CATALOG ) ); + assertThat( sql ).contains( dialect.getCreateCatalogCommand( IMPLICIT_FILE_LEVEL_CATALOG ) ); + if ( expectedDefaultCatalog != null ) { + assertThat( sql ).contains( dialect.getCreateCatalogCommand( expectedDefaultCatalog ) ); + } + } + + if ( sessionFactory.getJdbcServices().getDialect().canCreateSchema() ) { + assertThat( sql ).contains( dialect.getCreateSchemaCommand( EXPLICIT_SCHEMA ) ); + assertThat( sql ).contains( dialect.getCreateSchemaCommand( IMPLICIT_FILE_LEVEL_SCHEMA ) ); + if ( expectedDefaultSchema != null ) { + assertThat( sql ).contains( dialect.getCreateSchemaCommand( expectedDefaultSchema ) ); + } + } + } + + private void verifyDDLDropCatalogOrSchema(String sql) { + Dialect dialect = sessionFactory.getJdbcServices().getDialect(); + + if ( sessionFactory.getJdbcServices().getDialect().canCreateCatalog() ) { + assertThat( sql ).contains( dialect.getDropCatalogCommand( EXPLICIT_CATALOG ) ); + assertThat( sql ).contains( dialect.getDropCatalogCommand( IMPLICIT_FILE_LEVEL_CATALOG ) ); + if ( expectedDefaultCatalog != null ) { + assertThat( sql ).contains( dialect.getDropCatalogCommand( expectedDefaultCatalog ) ); + } + } + + if ( sessionFactory.getJdbcServices().getDialect().canCreateSchema() ) { + assertThat( sql ).contains( dialect.getDropSchemaCommand( EXPLICIT_SCHEMA ) ); + assertThat( sql ).contains( dialect.getDropSchemaCommand( IMPLICIT_FILE_LEVEL_SCHEMA ) ); + if ( expectedDefaultSchema != null ) { + assertThat( sql ).contains( dialect.getDropSchemaCommand( expectedDefaultSchema ) ); + } + } + } + private void verifyDDLQualifiers(String sql) { // Here, to simplify assertions, we assume: // - that all entity types have a table name identical to the entity name @@ -665,7 +727,7 @@ public class DefaultCatalogAndSchemaTest { } private ExpectedQualifier expectedImplicitFileLevelQualifier() { - return expectedQualifier( "someImplicitFileLevelCatalog", "someImplicitFileLevelSchema" ); + return expectedQualifier( IMPLICIT_FILE_LEVEL_CATALOG, IMPLICIT_FILE_LEVEL_SCHEMA ); } private ExpectedQualifier expectedQualifier(String catalog, String schema) {