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 7be545574a
commit 82b89e2a22
5 changed files with 59 additions and 45 deletions

View File

@ -535,14 +535,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 ) ),
@ -554,16 +558,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

@ -63,6 +63,7 @@ public class GroupedSchemaMigratorImpl extends AbstractSchemaMigrator {
tryToCreateSchemas,
exportedCatalogs,
namespace,
sqlStringGenerationContext,
targets
);
final NameSpaceTablesInformation tables = existingDatabase.getTablesInformation( namespace );

View File

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

View File

@ -233,11 +233,12 @@ public class SchemaCreatorImpl implements SchemaCreator {
continue;
}
if ( tryToCreateCatalogs ) {
final Identifier catalogLogicalName = namespace.getName().getCatalog();
final Identifier catalogPhysicalName =
sqlStringGenerationContext.catalogWithDefault( namespace.getPhysicalName().getCatalog() );
Namespace.Name logicalName = namespace.getName();
Namespace.Name physicalName = namespace.getPhysicalName();
if ( tryToCreateCatalogs ) {
final Identifier catalogLogicalName = logicalName.getCatalog();
final Identifier catalogPhysicalName = sqlStringGenerationContext.catalogWithDefault( physicalName.getCatalog() );
if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) ) {
applySqlStrings(
dialect.getCreateCatalogCommand( catalogPhysicalName.render( dialect ) ),
@ -249,15 +250,16 @@ public class SchemaCreatorImpl implements SchemaCreator {
}
}
final Identifier schemaPhysicalName =
sqlStringGenerationContext.schemaWithDefault( namespace.getPhysicalName().getSchema() );
if ( tryToCreateSchemas && schemaPhysicalName != null ) {
applySqlStrings(
dialect.getCreateSchemaCommand( schemaPhysicalName.render( dialect ) ),
formatter,
options,
targets
);
if ( tryToCreateSchemas ) {
final Identifier schemaPhysicalName = sqlStringGenerationContext.schemaWithDefault( physicalName.getSchema() );
if ( schemaPhysicalName != null ) {
applySqlStrings(
dialect.getCreateSchemaCommand( schemaPhysicalName.render( dialect ) ),
formatter,
options,
targets
);
}
}
}
}

View File

@ -275,29 +275,34 @@ public class SchemaDropperImpl implements SchemaDropper {
);
}
if ( tryToDropCatalogs || tryToDropSchemas ) {
Set<Identifier> exportedCatalogs = new HashSet<Identifier>();
for ( Namespace namespace : database.getNamespaces() ) {
if ( tryToDropCatalogs || tryToDropSchemas) {
final Set<Identifier> exportedCatalogs = new HashSet<Identifier>();
for ( Namespace namespace : metadata.getDatabase().getNamespaces() ) {
if ( !schemaFilter.includeNamespace( namespace ) ) {
continue;
}
if ( tryToDropSchemas && namespace.getPhysicalName().getSchema() != null ) {
applySqlStrings(
dialect.getDropSchemaCommand(
namespace.getPhysicalName().getSchema().render( dialect )
),
formatter,
options,
targets
);
}
if ( tryToDropCatalogs ) {
final Identifier catalogLogicalName = namespace.getName().getCatalog();
final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();
Namespace.Name logicalName = namespace.getName();
Namespace.Name physicalName = namespace.getPhysicalName();
if ( tryToDropSchemas ) {
final Identifier schemaPhysicalName = sqlStringGenerationContext.schemaWithDefault( physicalName.getSchema() );
if ( schemaPhysicalName != null ) {
applySqlStrings(
dialect.getDropSchemaCommand(
schemaPhysicalName.render( dialect )
),
formatter,
options,
targets
);
}
}
if (tryToDropCatalogs) {
final Identifier catalogLogicalName = logicalName.getCatalog();
final Identifier catalogPhysicalName = sqlStringGenerationContext.catalogWithDefault( physicalName.getCatalog() );
if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) ) {
applySqlStrings(
dialect.getDropCatalogCommand(