From 0233d8ebd9df0b6ec94e703dac6cf09f2741deaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Mon, 13 Feb 2023 14:14:05 +0100 Subject: [PATCH] HHH-16177 Take into account the default catalog/schema when creating/dropping catalogs/schemas --- .../internal/AbstractSchemaMigrator.java | 35 +++++++++++-------- .../internal/GroupedSchemaMigratorImpl.java | 1 + .../IndividuallySchemaMigratorImpl.java | 1 + .../schema/internal/SchemaCreatorImpl.java | 27 +++++++------- .../schema/internal/SchemaDropperImpl.java | 20 +++++++---- 5 files changed, 51 insertions(+), 33 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java index 2d3d33c2d1..2c41a1d8fa 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java @@ -523,14 +523,18 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator { Formatter formatter, boolean tryToCreateCatalogs, boolean tryToCreateSchemas, - Set exportedCatalogs, Namespace namespace, GenerationTarget[] targets) { + Set exportedCatalogs, Namespace namespace, + SqlStringGenerationContext context, + GenerationTarget[] targets) { if ( tryToCreateCatalogs || tryToCreateSchemas ) { - if ( tryToCreateCatalogs ) { - final Identifier catalogLogicalName = namespace.getName().getCatalog(); - final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog(); + Namespace.Name logicalName = namespace.getName(); + Namespace.Name physicalName = namespace.getPhysicalName(); + if ( tryToCreateCatalogs ) { + final Identifier catalogLogicalName = logicalName.getCatalog(); + final Identifier catalogPhysicalName = context.catalogWithDefault( physicalName.getCatalog() ); if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) - && !existingDatabase.catalogExists( catalogLogicalName ) ) { + && !existingDatabase.catalogExists( catalogPhysicalName ) ) { applySqlStrings( false, dialect.getCreateCatalogCommand( catalogPhysicalName.render( dialect ) ), @@ -542,16 +546,17 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator { } } - if ( tryToCreateSchemas - && namespace.getPhysicalName().getSchema() != null - && !existingDatabase.schemaExists( namespace.getName() ) ) { - applySqlStrings( - false, - dialect.getCreateSchemaCommand( namespace.getPhysicalName().getSchema().render( dialect ) ), - formatter, - options, - targets - ); + if ( tryToCreateSchemas ) { + final Identifier schemaPhysicalName = context.schemaWithDefault( physicalName.getSchema() ); + if ( schemaPhysicalName != null && !existingDatabase.schemaExists( physicalName ) ) { + applySqlStrings( + false, + dialect.getCreateSchemaCommand( schemaPhysicalName.render( dialect ) ), + formatter, + options, + targets + ); + } } } } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/GroupedSchemaMigratorImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/GroupedSchemaMigratorImpl.java index f3bba93db9..9c4ff8131b 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/GroupedSchemaMigratorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/GroupedSchemaMigratorImpl.java @@ -65,6 +65,7 @@ public class GroupedSchemaMigratorImpl extends AbstractSchemaMigrator { tryToCreateSchemas, exportedCatalogs, namespace, + context, targets ); diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/IndividuallySchemaMigratorImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/IndividuallySchemaMigratorImpl.java index ec4791cd7a..68e8611b10 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/IndividuallySchemaMigratorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/IndividuallySchemaMigratorImpl.java @@ -65,6 +65,7 @@ public class IndividuallySchemaMigratorImpl extends AbstractSchemaMigrator { tryToCreateSchemas, exportedCatalogs, namespace, + context, targets ); for ( Table table : namespace.getTables() ) { diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java index 96a0556bba..be354c3b5c 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java @@ -524,10 +524,12 @@ public class SchemaCreatorImpl implements SchemaCreator { Set exportedCatalogs = new HashSet<>(); for ( Namespace namespace : metadata.getDatabase().getNamespaces() ) { if ( options.getSchemaFilter().includeNamespace( namespace ) ) { + Namespace.Name logicalName = namespace.getName(); + Namespace.Name physicalName = namespace.getPhysicalName(); + if ( tryToCreateCatalogs ) { - final Identifier catalogLogicalName = namespace.getName().getCatalog(); - final Identifier catalogPhysicalName = - context.catalogWithDefault( namespace.getPhysicalName().getCatalog() ); + final Identifier catalogLogicalName = logicalName.getCatalog(); + final Identifier catalogPhysicalName = context.catalogWithDefault( physicalName.getCatalog() ); if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) ) { applySqlStrings( dialect.getCreateCatalogCommand( catalogPhysicalName.render( dialect ) ), @@ -539,15 +541,16 @@ public class SchemaCreatorImpl implements SchemaCreator { } } - final Identifier schemaPhysicalName = - context.schemaWithDefault( namespace.getPhysicalName().getSchema() ); - if ( tryToCreateSchemas && schemaPhysicalName != null ) { - applySqlStrings( - dialect.getCreateSchemaCommand( schemaPhysicalName.render( dialect ) ), - formatter, - options, - targets - ); + if ( tryToCreateSchemas ) { + final Identifier schemaPhysicalName = context.schemaWithDefault( physicalName.getSchema() ); + if ( schemaPhysicalName != null ) { + applySqlStrings( + dialect.getCreateSchemaCommand( schemaPhysicalName.render( dialect ) ), + formatter, + options, + targets + ); + } } } } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java index 24cac9739c..9737ff8f10 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaDropperImpl.java @@ -247,7 +247,7 @@ public class SchemaDropperImpl implements SchemaDropper { ); dropAuxiliaryObjectsAfterTables( metadata, options, dialect, formatter, context, targets ); dropUserDefinedTypes( metadata, options, dialect, formatter, context, targets ); - dropSchemasAndCatalogs( metadata, options, dialect, formatter, targets ); + dropSchemasAndCatalogs( metadata, options, dialect, formatter, context, targets ); } private void dropConstraintsTablesSequences( @@ -422,6 +422,7 @@ public class SchemaDropperImpl implements SchemaDropper { ExecutionOptions options, Dialect dialect, Formatter formatter, + SqlStringGenerationContext context, GenerationTarget[] targets) { boolean tryToDropCatalogs = options.shouldManageNamespaces() && dialect.canCreateCatalog(); boolean tryToDropSchemas = options.shouldManageNamespaces() && dialect.canCreateSchema(); @@ -429,13 +430,20 @@ public class SchemaDropperImpl implements SchemaDropper { final Set exportedCatalogs = new HashSet<>(); for ( Namespace namespace : metadata.getDatabase().getNamespaces() ) { if ( options.getSchemaFilter().includeNamespace( namespace ) ) { - if ( tryToDropSchemas && namespace.getPhysicalName().getSchema() != null ) { - final String schemaName = namespace.getPhysicalName().getSchema().render( dialect ); - applySqlStrings( dialect.getDropSchemaCommand( schemaName ), formatter, options, targets); + Namespace.Name logicalName = namespace.getName(); + Namespace.Name physicalName = namespace.getPhysicalName(); + + if ( tryToDropSchemas ) { + final Identifier schemaPhysicalName = context.schemaWithDefault( physicalName.getSchema() ); + if ( schemaPhysicalName != null ) { + final String schemaName = schemaPhysicalName.render( dialect ); + applySqlStrings( dialect.getDropSchemaCommand( schemaName ), formatter, options, targets); + } } + if (tryToDropCatalogs) { - final Identifier catalogLogicalName = namespace.getName().getCatalog(); - final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog(); + final Identifier catalogLogicalName = logicalName.getCatalog(); + final Identifier catalogPhysicalName = context.catalogWithDefault( physicalName.getCatalog() ); if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) ) { final String catalogName = catalogPhysicalName.render( dialect ); applySqlStrings( dialect.getDropCatalogCommand( catalogName ), formatter, options, targets );