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, Formatter formatter,
boolean tryToCreateCatalogs, boolean tryToCreateCatalogs,
boolean tryToCreateSchemas, boolean tryToCreateSchemas,
Set<Identifier> exportedCatalogs, Namespace namespace, GenerationTarget[] targets) { Set<Identifier> exportedCatalogs, Namespace namespace,
SqlStringGenerationContext context,
GenerationTarget[] targets) {
if ( tryToCreateCatalogs || tryToCreateSchemas ) { if ( tryToCreateCatalogs || tryToCreateSchemas ) {
if ( tryToCreateCatalogs ) { Namespace.Name logicalName = namespace.getName();
final Identifier catalogLogicalName = namespace.getName().getCatalog(); Namespace.Name physicalName = namespace.getPhysicalName();
final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();
if ( tryToCreateCatalogs ) {
final Identifier catalogLogicalName = logicalName.getCatalog();
final Identifier catalogPhysicalName = context.catalogWithDefault( physicalName.getCatalog() );
if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName )
&& !existingDatabase.catalogExists( catalogLogicalName ) ) { && !existingDatabase.catalogExists( catalogPhysicalName ) ) {
applySqlStrings( applySqlStrings(
false, false,
dialect.getCreateCatalogCommand( catalogPhysicalName.render( dialect ) ), dialect.getCreateCatalogCommand( catalogPhysicalName.render( dialect ) ),
@ -542,12 +546,12 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
} }
} }
if ( tryToCreateSchemas if ( tryToCreateSchemas ) {
&& namespace.getPhysicalName().getSchema() != null final Identifier schemaPhysicalName = context.schemaWithDefault( physicalName.getSchema() );
&& !existingDatabase.schemaExists( namespace.getName() ) ) { if ( schemaPhysicalName != null && !existingDatabase.schemaExists( physicalName ) ) {
applySqlStrings( applySqlStrings(
false, false,
dialect.getCreateSchemaCommand( namespace.getPhysicalName().getSchema().render( dialect ) ), dialect.getCreateSchemaCommand( schemaPhysicalName.render( dialect ) ),
formatter, formatter,
options, options,
targets targets
@ -555,6 +559,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
} }
} }
} }
}
private static void applySqlString( private static void applySqlString(
boolean quiet, boolean quiet,

View File

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

View File

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

View File

@ -524,10 +524,12 @@ public class SchemaCreatorImpl implements SchemaCreator {
Set<Identifier> exportedCatalogs = new HashSet<>(); Set<Identifier> exportedCatalogs = new HashSet<>();
for ( Namespace namespace : metadata.getDatabase().getNamespaces() ) { for ( Namespace namespace : metadata.getDatabase().getNamespaces() ) {
if ( options.getSchemaFilter().includeNamespace( namespace ) ) { if ( options.getSchemaFilter().includeNamespace( namespace ) ) {
Namespace.Name logicalName = namespace.getName();
Namespace.Name physicalName = namespace.getPhysicalName();
if ( tryToCreateCatalogs ) { if ( tryToCreateCatalogs ) {
final Identifier catalogLogicalName = namespace.getName().getCatalog(); final Identifier catalogLogicalName = logicalName.getCatalog();
final Identifier catalogPhysicalName = final Identifier catalogPhysicalName = context.catalogWithDefault( physicalName.getCatalog() );
context.catalogWithDefault( namespace.getPhysicalName().getCatalog() );
if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) ) { if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) ) {
applySqlStrings( applySqlStrings(
dialect.getCreateCatalogCommand( catalogPhysicalName.render( dialect ) ), dialect.getCreateCatalogCommand( catalogPhysicalName.render( dialect ) ),
@ -539,9 +541,9 @@ public class SchemaCreatorImpl implements SchemaCreator {
} }
} }
final Identifier schemaPhysicalName = if ( tryToCreateSchemas ) {
context.schemaWithDefault( namespace.getPhysicalName().getSchema() ); final Identifier schemaPhysicalName = context.schemaWithDefault( physicalName.getSchema() );
if ( tryToCreateSchemas && schemaPhysicalName != null ) { if ( schemaPhysicalName != null ) {
applySqlStrings( applySqlStrings(
dialect.getCreateSchemaCommand( schemaPhysicalName.render( dialect ) ), dialect.getCreateSchemaCommand( schemaPhysicalName.render( dialect ) ),
formatter, formatter,
@ -553,6 +555,7 @@ public class SchemaCreatorImpl implements SchemaCreator {
} }
} }
} }
}
private static void checkExportIdentifier(Exportable exportable, Set<String> exportIdentifiers) { private static void checkExportIdentifier(Exportable exportable, Set<String> exportIdentifiers) {
final String exportIdentifier = exportable.getExportIdentifier(); final String exportIdentifier = exportable.getExportIdentifier();

View File

@ -247,7 +247,7 @@ public class SchemaDropperImpl implements SchemaDropper {
); );
dropAuxiliaryObjectsAfterTables( metadata, options, dialect, formatter, context, targets ); dropAuxiliaryObjectsAfterTables( metadata, options, dialect, formatter, context, targets );
dropUserDefinedTypes( 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( private void dropConstraintsTablesSequences(
@ -422,6 +422,7 @@ public class SchemaDropperImpl implements SchemaDropper {
ExecutionOptions options, ExecutionOptions options,
Dialect dialect, Dialect dialect,
Formatter formatter, Formatter formatter,
SqlStringGenerationContext context,
GenerationTarget[] targets) { GenerationTarget[] targets) {
boolean tryToDropCatalogs = options.shouldManageNamespaces() && dialect.canCreateCatalog(); boolean tryToDropCatalogs = options.shouldManageNamespaces() && dialect.canCreateCatalog();
boolean tryToDropSchemas = options.shouldManageNamespaces() && dialect.canCreateSchema(); boolean tryToDropSchemas = options.shouldManageNamespaces() && dialect.canCreateSchema();
@ -429,13 +430,20 @@ public class SchemaDropperImpl implements SchemaDropper {
final Set<Identifier> exportedCatalogs = new HashSet<>(); final Set<Identifier> exportedCatalogs = new HashSet<>();
for ( Namespace namespace : metadata.getDatabase().getNamespaces() ) { for ( Namespace namespace : metadata.getDatabase().getNamespaces() ) {
if ( options.getSchemaFilter().includeNamespace( namespace ) ) { if ( options.getSchemaFilter().includeNamespace( namespace ) ) {
if ( tryToDropSchemas && namespace.getPhysicalName().getSchema() != null ) { Namespace.Name logicalName = namespace.getName();
final String schemaName = namespace.getPhysicalName().getSchema().render( dialect ); 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); applySqlStrings( dialect.getDropSchemaCommand( schemaName ), formatter, options, targets);
} }
}
if (tryToDropCatalogs) { if (tryToDropCatalogs) {
final Identifier catalogLogicalName = namespace.getName().getCatalog(); final Identifier catalogLogicalName = logicalName.getCatalog();
final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog(); final Identifier catalogPhysicalName = context.catalogWithDefault( physicalName.getCatalog() );
if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) ) { if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) ) {
final String catalogName = catalogPhysicalName.render( dialect ); final String catalogName = catalogPhysicalName.render( dialect );
applySqlStrings( dialect.getDropCatalogCommand( catalogName ), formatter, options, targets ); applySqlStrings( dialect.getDropCatalogCommand( catalogName ), formatter, options, targets );