HHH-16177 Take into account the default catalog/schema when creating/dropping catalogs/schemas

This commit is contained in:
Yoann Rodière 2023-02-13 14:14:05 +01:00
parent 35fbd6695a
commit 0233d8ebd9
5 changed files with 51 additions and 33 deletions

View File

@ -523,14 +523,18 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
Formatter formatter,
boolean tryToCreateCatalogs,
boolean tryToCreateSchemas,
Set<Identifier> exportedCatalogs, Namespace namespace, GenerationTarget[] targets) {
Set<Identifier> 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
);
}
}
}
}

View File

@ -65,6 +65,7 @@ public class GroupedSchemaMigratorImpl extends AbstractSchemaMigrator {
tryToCreateSchemas,
exportedCatalogs,
namespace,
context,
targets
);

View File

@ -65,6 +65,7 @@ public class IndividuallySchemaMigratorImpl extends AbstractSchemaMigrator {
tryToCreateSchemas,
exportedCatalogs,
namespace,
context,
targets
);
for ( Table table : namespace.getTables() ) {

View File

@ -524,10 +524,12 @@ public class SchemaCreatorImpl implements SchemaCreator {
Set<Identifier> 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
);
}
}
}
}

View File

@ -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<Identifier> 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 );