HHH-9966 - Improve schema tooling support for creating catalogs and schemas
This commit is contained in:
parent
e98f220b08
commit
d885f249d9
|
@ -168,7 +168,6 @@ public class SessionFactoryOptionsImpl implements SessionFactoryOptions {
|
||||||
this.autoEvictCollectionCache = state.isAutoEvictCollectionCache();
|
this.autoEvictCollectionCache = state.isAutoEvictCollectionCache();
|
||||||
|
|
||||||
this.schemaAutoTooling = state.getSchemaAutoTooling();
|
this.schemaAutoTooling = state.getSchemaAutoTooling();
|
||||||
|
|
||||||
this.connectionReleaseMode = state.getConnectionReleaseMode();
|
this.connectionReleaseMode = state.getConnectionReleaseMode();
|
||||||
this.getGeneratedKeysEnabled = state.isGetGeneratedKeysEnabled();
|
this.getGeneratedKeysEnabled = state.isGetGeneratedKeysEnabled();
|
||||||
this.jdbcBatchSize = state.getJdbcBatchSize();
|
this.jdbcBatchSize = state.getJdbcBatchSize();
|
||||||
|
|
|
@ -403,6 +403,14 @@ public interface AvailableSettings {
|
||||||
*/
|
*/
|
||||||
String HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR = "hibernate.hbm2ddl.import_files_sql_extractor";
|
String HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR = "hibernate.hbm2ddl.import_files_sql_extractor";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies whether to automatically create also the database schema/catalog.
|
||||||
|
* The default is false.
|
||||||
|
*
|
||||||
|
* @since 5.0
|
||||||
|
*/
|
||||||
|
String HBM2DLL_CREATE_NAMESPACES = "hibernate.hbm2dll.create_namespaces";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link org.hibernate.exception.spi.SQLExceptionConverter} to use for converting SQLExceptions
|
* The {@link org.hibernate.exception.spi.SQLExceptionConverter} to use for converting SQLExceptions
|
||||||
* to Hibernate's JDBCException hierarchy. The default is to use the configured
|
* to Hibernate's JDBCException hierarchy. The default is to use the configured
|
||||||
|
|
|
@ -1979,15 +1979,55 @@ public abstract class Dialect implements ConversionContext {
|
||||||
return auxiliaryObjectExporter;
|
return auxiliaryObjectExporter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does this dialect support catalog creation?
|
||||||
|
*
|
||||||
|
* @return True if the dialect supports catalog creation; false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean canCreateCatalog() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the SQL command used to create the named catalog
|
||||||
|
*
|
||||||
|
* @param catalogName The name of the catalog to be created.
|
||||||
|
*
|
||||||
|
* @return The creation commands
|
||||||
|
*/
|
||||||
|
public String[] getCreateCatalogCommand(String catalogName) {
|
||||||
|
throw new UnsupportedOperationException( "No create catalog syntax supported by " + getClass().getName() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the SQL command used to drop the named catalog
|
||||||
|
*
|
||||||
|
* @param catalogName The name of the catalog to be dropped.
|
||||||
|
*
|
||||||
|
* @return The drop commands
|
||||||
|
*/
|
||||||
|
public String[] getDropCatalogCommand(String catalogName) {
|
||||||
|
throw new UnsupportedOperationException( "No drop catalog syntax supported by " + getClass().getName() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does this dialect support schema creation?
|
||||||
|
*
|
||||||
|
* @return True if the dialect supports schema creation; false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean canCreateSchema() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the SQL command used to create the named schema
|
* Get the SQL command used to create the named schema
|
||||||
*
|
*
|
||||||
* @param schemaName The name of the schema to be created.
|
* @param schemaName The name of the schema to be created.
|
||||||
*
|
*
|
||||||
* @return The creation command
|
* @return The creation commands
|
||||||
*/
|
*/
|
||||||
public String getCreateSchemaCommand(String schemaName) {
|
public String[] getCreateSchemaCommand(String schemaName) {
|
||||||
return "create schema " + schemaName;
|
return new String[] {"create schema " + schemaName};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1995,10 +2035,10 @@ public abstract class Dialect implements ConversionContext {
|
||||||
*
|
*
|
||||||
* @param schemaName The name of the schema to be dropped.
|
* @param schemaName The name of the schema to be dropped.
|
||||||
*
|
*
|
||||||
* @return The drop command
|
* @return The drop commands
|
||||||
*/
|
*/
|
||||||
public String getDropSchemaCommand(String schemaName) {
|
public String[] getDropSchemaCommand(String schemaName) {
|
||||||
return "drop schema " + schemaName;
|
return new String[] {"drop schema " + schemaName};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -112,6 +112,7 @@ import org.hibernate.integrator.spi.Integrator;
|
||||||
import org.hibernate.integrator.spi.IntegratorService;
|
import org.hibernate.integrator.spi.IntegratorService;
|
||||||
import org.hibernate.internal.util.collections.CollectionHelper;
|
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||||
import org.hibernate.internal.util.config.ConfigurationException;
|
import org.hibernate.internal.util.config.ConfigurationException;
|
||||||
|
import org.hibernate.internal.util.config.ConfigurationHelper;
|
||||||
import org.hibernate.mapping.Collection;
|
import org.hibernate.mapping.Collection;
|
||||||
import org.hibernate.mapping.PersistentClass;
|
import org.hibernate.mapping.PersistentClass;
|
||||||
import org.hibernate.mapping.RootClass;
|
import org.hibernate.mapping.RootClass;
|
||||||
|
@ -142,6 +143,7 @@ import org.hibernate.tuple.entity.EntityTuplizer;
|
||||||
import org.hibernate.type.AssociationType;
|
import org.hibernate.type.AssociationType;
|
||||||
import org.hibernate.type.Type;
|
import org.hibernate.type.Type;
|
||||||
import org.hibernate.type.TypeResolver;
|
import org.hibernate.type.TypeResolver;
|
||||||
|
import static org.hibernate.cfg.AvailableSettings.HBM2DLL_CREATE_NAMESPACES;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -467,8 +469,11 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
boolean createDropNamespaces = ConfigurationHelper.getBoolean( HBM2DLL_CREATE_NAMESPACES, properties, false );
|
||||||
|
|
||||||
if ( settings.isAutoCreateSchema() ) {
|
if ( settings.isAutoCreateSchema() ) {
|
||||||
new SchemaExport( serviceRegistry, metadata )
|
|
||||||
|
new SchemaExport( serviceRegistry, metadata, createDropNamespaces )
|
||||||
.setImportSqlCommandExtractor( serviceRegistry.getService( ImportSqlCommandExtractor.class ) )
|
.setImportSqlCommandExtractor( serviceRegistry.getService( ImportSqlCommandExtractor.class ) )
|
||||||
.create( false, true );
|
.create( false, true );
|
||||||
}
|
}
|
||||||
|
@ -479,7 +484,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
|
||||||
new SchemaValidator( serviceRegistry, metadata ).validate();
|
new SchemaValidator( serviceRegistry, metadata ).validate();
|
||||||
}
|
}
|
||||||
if ( settings.isAutoDropSchema() ) {
|
if ( settings.isAutoDropSchema() ) {
|
||||||
schemaExport = new SchemaExport( serviceRegistry, metadata )
|
schemaExport = new SchemaExport( serviceRegistry, metadata, createDropNamespaces )
|
||||||
.setImportSqlCommandExtractor( serviceRegistry.getService( ImportSqlCommandExtractor.class ) );
|
.setImportSqlCommandExtractor( serviceRegistry.getService( ImportSqlCommandExtractor.class ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,8 +107,8 @@ public class SchemaExport {
|
||||||
*
|
*
|
||||||
* @param metadata The metadata object holding the mapping info to be exported
|
* @param metadata The metadata object holding the mapping info to be exported
|
||||||
*/
|
*/
|
||||||
public SchemaExport(MetadataImplementor metadata, boolean exportSchemas) {
|
public SchemaExport(MetadataImplementor metadata, boolean createNamespaces) {
|
||||||
this( metadata.getMetadataBuildingOptions().getServiceRegistry(), metadata, exportSchemas );
|
this( metadata.getMetadataBuildingOptions().getServiceRegistry(), metadata, createNamespaces );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -136,14 +136,14 @@ public class SchemaExport {
|
||||||
* the JdbcServices service.
|
* the JdbcServices service.
|
||||||
* @param metadata The metadata object holding the mapping info to be exported
|
* @param metadata The metadata object holding the mapping info to be exported
|
||||||
*/
|
*/
|
||||||
public SchemaExport(ServiceRegistry serviceRegistry, MetadataImplementor metadata, boolean exportSchemas) {
|
public SchemaExport(ServiceRegistry serviceRegistry, MetadataImplementor metadata, boolean createNamespaces) {
|
||||||
this(
|
this(
|
||||||
new SuppliedConnectionProviderConnectionHelper(
|
new SuppliedConnectionProviderConnectionHelper(
|
||||||
serviceRegistry.getService( ConnectionProvider.class )
|
serviceRegistry.getService( ConnectionProvider.class )
|
||||||
),
|
),
|
||||||
serviceRegistry,
|
serviceRegistry,
|
||||||
metadata,
|
metadata,
|
||||||
exportSchemas
|
createNamespaces
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,7 +151,7 @@ public class SchemaExport {
|
||||||
ConnectionHelper connectionHelper,
|
ConnectionHelper connectionHelper,
|
||||||
ServiceRegistry serviceRegistry,
|
ServiceRegistry serviceRegistry,
|
||||||
MetadataImplementor metadata,
|
MetadataImplementor metadata,
|
||||||
boolean exportSchemas) {
|
boolean createNamespaces) {
|
||||||
this.connectionHelper = connectionHelper;
|
this.connectionHelper = connectionHelper;
|
||||||
this.sqlStatementLogger = serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger();
|
this.sqlStatementLogger = serviceRegistry.getService( JdbcServices.class ).getSqlStatementLogger();
|
||||||
this.formatter = ( sqlStatementLogger.isFormat() ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
|
this.formatter = ( sqlStatementLogger.isFormat() ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
|
||||||
|
@ -193,10 +193,10 @@ public class SchemaExport {
|
||||||
|
|
||||||
final Map settings = serviceRegistry.getService( ConfigurationService.class ).getSettings();
|
final Map settings = serviceRegistry.getService( ConfigurationService.class ).getSettings();
|
||||||
|
|
||||||
schemaManagementTool.getSchemaDropper( settings ).doDrop( metadata, exportSchemas, target );
|
schemaManagementTool.getSchemaDropper( settings ).doDrop( metadata, createNamespaces, target );
|
||||||
this.dropSQL = commands.toArray( new String[commands.size()] );
|
this.dropSQL = commands.toArray( new String[commands.size()] );
|
||||||
|
|
||||||
schemaManagementTool.getSchemaCreator( settings ).doCreation( metadata, exportSchemas, target );
|
schemaManagementTool.getSchemaCreator( settings ).doCreation( metadata, createNamespaces, target );
|
||||||
this.createSQL = commands.toArray( new String[commands.size()] );
|
this.createSQL = commands.toArray( new String[commands.size()] );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,6 +66,11 @@ public class DatabaseInformationImpl implements DatabaseInformation, ExtractionC
|
||||||
return locateSequenceInformation( sequenceName );
|
return locateSequenceInformation( sequenceName );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean catalogExists(Identifier catalog) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// RegisteredObjectAccess implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
// RegisteredObjectAccess implementation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -141,6 +141,11 @@ public class DatabaseInformationImpl implements DatabaseInformation, ExtractionC
|
||||||
return locateSequenceInformation( qualifiedSequenceName );
|
return locateSequenceInformation( qualifiedSequenceName );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean catalogExists(Identifier catalog) {
|
||||||
|
return extractor.catalogExists( catalog );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TableInformation locateTableInformation(QualifiedTableName tableName) {
|
public TableInformation locateTableInformation(QualifiedTableName tableName) {
|
||||||
return getTableInformation( tableName );
|
return getTableInformation( tableName );
|
||||||
|
|
|
@ -92,4 +92,13 @@ public interface DatabaseInformation {
|
||||||
* @return The sequence information. May return {@code null} if not found.
|
* @return The sequence information. May return {@code null} if not found.
|
||||||
*/
|
*/
|
||||||
public SequenceInformation getSequenceInformation(QualifiedSequenceName sequenceName);
|
public SequenceInformation getSequenceInformation(QualifiedSequenceName sequenceName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check to see if the given catalog already exists.
|
||||||
|
*
|
||||||
|
* @param catalog The catalog name
|
||||||
|
*
|
||||||
|
* @return {@code true} indicates a catalog with the given name already exists
|
||||||
|
*/
|
||||||
|
boolean catalogExists(Identifier catalog);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.hibernate.boot.Metadata;
|
import org.hibernate.boot.Metadata;
|
||||||
|
import org.hibernate.boot.model.naming.Identifier;
|
||||||
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
|
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
|
||||||
import org.hibernate.boot.model.relational.Database;
|
import org.hibernate.boot.model.relational.Database;
|
||||||
import org.hibernate.boot.model.relational.Exportable;
|
import org.hibernate.boot.model.relational.Exportable;
|
||||||
|
@ -38,13 +39,13 @@ import org.hibernate.tool.schema.spi.Target;
|
||||||
public class SchemaCreatorImpl implements SchemaCreator {
|
public class SchemaCreatorImpl implements SchemaCreator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doCreation(Metadata metadata, boolean createSchemas, List<Target> targets) throws SchemaManagementException {
|
public void doCreation(Metadata metadata, boolean createNamespaces, List<Target> targets) throws SchemaManagementException {
|
||||||
doCreation( metadata, createSchemas, targets.toArray( new Target[ targets.size() ] ) );
|
doCreation( metadata, createNamespaces, targets.toArray( new Target[ targets.size() ] ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doCreation(Metadata metadata, boolean createSchemas, Dialect dialect, List<Target> targets) throws SchemaManagementException {
|
public void doCreation(Metadata metadata, boolean createNamespaces, Dialect dialect, List<Target> targets) throws SchemaManagementException {
|
||||||
doCreation( metadata, createSchemas, dialect, targets.toArray( new Target[ targets.size() ] ) );
|
doCreation( metadata, createNamespaces, dialect, targets.toArray( new Target[ targets.size() ] ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,11 +55,11 @@ public class SchemaCreatorImpl implements SchemaCreator {
|
||||||
*
|
*
|
||||||
* @return The generation commands
|
* @return The generation commands
|
||||||
*/
|
*/
|
||||||
public List<String> generateCreationCommands(Metadata metadata, boolean createSchemas) {
|
public List<String> generateCreationCommands(Metadata metadata, boolean createNamespaces) {
|
||||||
final ArrayList<String> commands = new ArrayList<String>();
|
final ArrayList<String> commands = new ArrayList<String>();
|
||||||
doCreation(
|
doCreation(
|
||||||
metadata,
|
metadata,
|
||||||
createSchemas,
|
createNamespaces,
|
||||||
new Target() {
|
new Target() {
|
||||||
@Override
|
@Override
|
||||||
public boolean acceptsImportScriptActions() {
|
public boolean acceptsImportScriptActions() {
|
||||||
|
@ -86,16 +87,16 @@ public class SchemaCreatorImpl implements SchemaCreator {
|
||||||
* For temporary use from JPA schema generation
|
* For temporary use from JPA schema generation
|
||||||
*
|
*
|
||||||
* @param metadata The metadata for which to generate the creation commands.
|
* @param metadata The metadata for which to generate the creation commands.
|
||||||
* @param createSchemas Should the schema(s) actually be created as well ({@code CREATE SCHEMA})?
|
* @param createNamespaces Should the schema(s)/catalog(s) actually be created as well ({@code CREATE SCHEMA})?
|
||||||
* @param dialect Allow explicitly passing the Dialect to use.
|
* @param dialect Allow explicitly passing the Dialect to use.
|
||||||
*
|
*
|
||||||
* @return The generation commands
|
* @return The generation commands
|
||||||
*/
|
*/
|
||||||
public List<String> generateCreationCommands(Metadata metadata, boolean createSchemas, Dialect dialect) {
|
public List<String> generateCreationCommands(Metadata metadata, boolean createNamespaces, Dialect dialect) {
|
||||||
final ArrayList<String> commands = new ArrayList<String>();
|
final ArrayList<String> commands = new ArrayList<String>();
|
||||||
doCreation(
|
doCreation(
|
||||||
metadata,
|
metadata,
|
||||||
createSchemas,
|
createNamespaces,
|
||||||
dialect,
|
dialect,
|
||||||
new Target() {
|
new Target() {
|
||||||
|
|
||||||
|
@ -122,19 +123,30 @@ public class SchemaCreatorImpl implements SchemaCreator {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doCreation(Metadata metadata, boolean createSchemas, Target... targets)
|
public void doCreation(Metadata metadata, boolean createNamespaces, Target... targets)
|
||||||
throws SchemaManagementException {
|
throws SchemaManagementException {
|
||||||
doCreation(
|
doCreation(
|
||||||
metadata,
|
metadata,
|
||||||
createSchemas,
|
createNamespaces,
|
||||||
metadata.getDatabase().getJdbcEnvironment().getDialect(),
|
metadata.getDatabase().getJdbcEnvironment().getDialect(),
|
||||||
targets
|
targets
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doCreation(Metadata metadata, boolean createSchemas, Dialect dialect, Target... targets)
|
public void doCreation(Metadata metadata, boolean createNamespaces, Dialect dialect, Target... targets)
|
||||||
throws SchemaManagementException {
|
throws SchemaManagementException {
|
||||||
|
boolean tryToCreateCatalogs = false;
|
||||||
|
boolean tryToCreateSchemas = false;
|
||||||
|
if ( createNamespaces ) {
|
||||||
|
if ( dialect.canCreateSchema() ) {
|
||||||
|
tryToCreateSchemas = true;
|
||||||
|
}
|
||||||
|
if ( dialect.canCreateCatalog() ) {
|
||||||
|
tryToCreateCatalogs = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final Database database = metadata.getDatabase();
|
final Database database = metadata.getDatabase();
|
||||||
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
|
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
|
||||||
|
|
||||||
|
@ -144,13 +156,32 @@ public class SchemaCreatorImpl implements SchemaCreator {
|
||||||
|
|
||||||
final Set<String> exportIdentifiers = new HashSet<String>( 50 );
|
final Set<String> exportIdentifiers = new HashSet<String>( 50 );
|
||||||
|
|
||||||
// first, create each schema
|
// first, create each catalog/schema
|
||||||
for ( Namespace namespace : database.getNamespaces() ) {
|
if ( tryToCreateCatalogs || tryToCreateSchemas ) {
|
||||||
if ( createSchemas ) {
|
Set<Identifier> exportedCatalogs = new HashSet<Identifier>();
|
||||||
if ( namespace.getName().getSchema() == null ) {
|
for ( Namespace namespace : database.getNamespaces() ) {
|
||||||
continue;
|
|
||||||
|
if ( tryToCreateCatalogs ) {
|
||||||
|
final Identifier catalogLogicalName = namespace.getName().getCatalog();
|
||||||
|
final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();
|
||||||
|
|
||||||
|
if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) ) {
|
||||||
|
applySqlStrings(
|
||||||
|
targets,
|
||||||
|
dialect.getCreateCatalogCommand( catalogPhysicalName.render( dialect ) )
|
||||||
|
);
|
||||||
|
exportedCatalogs.add( catalogLogicalName );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( tryToCreateSchemas && namespace.getPhysicalName().getSchema() != null ) {
|
||||||
|
applySqlStrings(
|
||||||
|
targets,
|
||||||
|
dialect.getCreateSchemaCommand(
|
||||||
|
namespace.getPhysicalName().getSchema().render( dialect )
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
applySqlStrings( targets, dialect.getCreateSchemaCommand( namespace.getName().getSchema().render( dialect ) ) );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.hibernate.boot.Metadata;
|
import org.hibernate.boot.Metadata;
|
||||||
|
import org.hibernate.boot.model.naming.Identifier;
|
||||||
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
|
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
|
||||||
import org.hibernate.boot.model.relational.Database;
|
import org.hibernate.boot.model.relational.Database;
|
||||||
import org.hibernate.boot.model.relational.Exportable;
|
import org.hibernate.boot.model.relational.Exportable;
|
||||||
|
@ -39,16 +40,19 @@ public class SchemaDropperImpl implements SchemaDropper {
|
||||||
* Intended for use from JPA schema export code.
|
* Intended for use from JPA schema export code.
|
||||||
*
|
*
|
||||||
* @param metadata The metadata for which to generate drop commands
|
* @param metadata The metadata for which to generate drop commands
|
||||||
* @param dropSchemas Should {@code DROP SCHEMA} command be generated?
|
* @param dropNamespaces Should drop schema/catalog command be generated?
|
||||||
* @param dialect Allow explicitly specifying the dialect.
|
* @param dialect Allow explicitly specifying the dialect.
|
||||||
*
|
*
|
||||||
* @return The commands
|
* @return The commands
|
||||||
*/
|
*/
|
||||||
public Iterable<String> generateDropCommands(MetadataImplementor metadata, boolean dropSchemas, Dialect dialect) {
|
public Iterable<String> generateDropCommands(
|
||||||
|
MetadataImplementor metadata,
|
||||||
|
boolean dropNamespaces,
|
||||||
|
Dialect dialect) {
|
||||||
final ArrayList<String> commands = new ArrayList<String>();
|
final ArrayList<String> commands = new ArrayList<String>();
|
||||||
doDrop(
|
doDrop(
|
||||||
metadata,
|
metadata,
|
||||||
dropSchemas,
|
dropNamespaces,
|
||||||
dialect,
|
dialect,
|
||||||
new Target() {
|
new Target() {
|
||||||
|
|
||||||
|
@ -75,20 +79,22 @@ public class SchemaDropperImpl implements SchemaDropper {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doDrop(Metadata metadata, boolean dropSchemas, List<Target> targets) throws SchemaManagementException {
|
public void doDrop(Metadata metadata, boolean dropNamespaces, List<Target> targets)
|
||||||
doDrop( metadata, dropSchemas, targets.toArray( new Target[ targets.size() ] ) );
|
throws SchemaManagementException {
|
||||||
|
doDrop( metadata, dropNamespaces, targets.toArray( new Target[targets.size()] ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doDrop(Metadata metadata, boolean dropSchemas, Dialect dialect, List<Target> targets) throws SchemaManagementException {
|
public void doDrop(Metadata metadata, boolean dropNamespaces, Dialect dialect, List<Target> targets)
|
||||||
doDrop( metadata, dropSchemas, dialect, targets.toArray( new Target[ targets.size() ] ) );
|
throws SchemaManagementException {
|
||||||
|
doDrop( metadata, dropNamespaces, dialect, targets.toArray( new Target[targets.size()] ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doDrop(Metadata metadata, boolean dropSchemas, Target... targets) throws SchemaManagementException {
|
public void doDrop(Metadata metadata, boolean dropNamespaces, Target... targets) throws SchemaManagementException {
|
||||||
doDrop(
|
doDrop(
|
||||||
metadata,
|
metadata,
|
||||||
dropSchemas,
|
dropNamespaces,
|
||||||
metadata.getDatabase().getJdbcEnvironment().getDialect(),
|
metadata.getDatabase().getJdbcEnvironment().getDialect(),
|
||||||
targets
|
targets
|
||||||
);
|
);
|
||||||
|
@ -96,10 +102,22 @@ public class SchemaDropperImpl implements SchemaDropper {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doDrop(Metadata metadata, boolean dropSchemas, Dialect dialect, Target... targets) throws SchemaManagementException {
|
public void doDrop(Metadata metadata, boolean dropNamespaces, Dialect dialect, Target... targets)
|
||||||
|
throws SchemaManagementException {
|
||||||
final Database database = metadata.getDatabase();
|
final Database database = metadata.getDatabase();
|
||||||
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
|
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
|
||||||
|
|
||||||
|
boolean tryToDropCatalogs = false;
|
||||||
|
boolean tryToDropSchemas = false;
|
||||||
|
if ( dropNamespaces ) {
|
||||||
|
if ( dialect.canCreateSchema() ) {
|
||||||
|
tryToDropSchemas = true;
|
||||||
|
}
|
||||||
|
if ( dialect.canCreateCatalog() ) {
|
||||||
|
tryToDropCatalogs = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for ( Target target : targets ) {
|
for ( Target target : targets ) {
|
||||||
target.prepare();
|
target.prepare();
|
||||||
}
|
}
|
||||||
|
@ -155,12 +173,31 @@ public class SchemaDropperImpl implements SchemaDropper {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( Namespace namespace : database.getNamespaces() ) {
|
if ( tryToDropCatalogs || tryToDropSchemas ) {
|
||||||
if ( dropSchemas ) {
|
Set<Identifier> exportedCatalogs = new HashSet<Identifier>();
|
||||||
if ( namespace.getName().getSchema() == null ) {
|
|
||||||
continue;
|
for ( Namespace namespace : database.getNamespaces() ) {
|
||||||
|
if ( tryToDropSchemas && namespace.getPhysicalName().getSchema() != null ) {
|
||||||
|
applySqlStrings(
|
||||||
|
targets, dialect.getDropSchemaCommand(
|
||||||
|
namespace.getPhysicalName().getSchema().render( dialect )
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if ( tryToDropCatalogs ) {
|
||||||
|
final Identifier catalogLogicalName = namespace.getName().getCatalog();
|
||||||
|
final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();
|
||||||
|
|
||||||
|
if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) ) {
|
||||||
|
applySqlStrings(
|
||||||
|
targets,
|
||||||
|
dialect.getDropCatalogCommand(
|
||||||
|
catalogPhysicalName.render( dialect )
|
||||||
|
)
|
||||||
|
);
|
||||||
|
exportedCatalogs.add( catalogLogicalName );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
applySqlStrings( targets, dialect.getDropSchemaCommand( namespace.getName().getSchema().render( dialect ) ) );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,14 +49,14 @@ public class SchemaMigratorImpl implements SchemaMigrator {
|
||||||
public void doMigration(
|
public void doMigration(
|
||||||
Metadata metadata,
|
Metadata metadata,
|
||||||
DatabaseInformation existingDatabase,
|
DatabaseInformation existingDatabase,
|
||||||
boolean createSchemas,
|
boolean createNamespaces,
|
||||||
List<Target> targets) throws SchemaManagementException {
|
List<Target> targets) throws SchemaManagementException {
|
||||||
|
|
||||||
for ( Target target : targets ) {
|
for ( Target target : targets ) {
|
||||||
target.prepare();
|
target.prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
doMigrationToTargets( metadata, existingDatabase, createSchemas, targets );
|
doMigrationToTargets( metadata, existingDatabase, createNamespaces, targets );
|
||||||
|
|
||||||
for ( Target target : targets ) {
|
for ( Target target : targets ) {
|
||||||
target.release();
|
target.release();
|
||||||
|
@ -67,25 +67,58 @@ public class SchemaMigratorImpl implements SchemaMigrator {
|
||||||
protected void doMigrationToTargets(
|
protected void doMigrationToTargets(
|
||||||
Metadata metadata,
|
Metadata metadata,
|
||||||
DatabaseInformation existingDatabase,
|
DatabaseInformation existingDatabase,
|
||||||
boolean createSchemas,
|
boolean createNamespaces,
|
||||||
List<Target> targets) {
|
List<Target> targets) {
|
||||||
final Set<String> exportIdentifiers = new HashSet<String>( 50 );
|
final Set<String> exportIdentifiers = new HashSet<String>( 50 );
|
||||||
|
|
||||||
final Database database = metadata.getDatabase();
|
|
||||||
|
|
||||||
|
final Database database = metadata.getDatabase();
|
||||||
|
boolean tryToCreateCatalogs = false;
|
||||||
|
boolean tryToCreateSchemas = false;
|
||||||
|
|
||||||
|
if ( createNamespaces ) {
|
||||||
|
if ( database.getJdbcEnvironment().getDialect().canCreateSchema() ) {
|
||||||
|
tryToCreateSchemas = true;
|
||||||
|
}
|
||||||
|
if ( database.getJdbcEnvironment().getDialect().canCreateCatalog() ) {
|
||||||
|
tryToCreateCatalogs = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Identifier> exportedCatalogs = new HashSet<Identifier>();
|
||||||
for ( Namespace namespace : database.getNamespaces() ) {
|
for ( Namespace namespace : database.getNamespaces() ) {
|
||||||
if ( createSchemas ) {
|
if ( tryToCreateCatalogs || tryToCreateSchemas ) {
|
||||||
if ( namespace.getName().getSchema() != null ) {
|
if ( tryToCreateCatalogs ) {
|
||||||
if ( !existingDatabase.schemaExists( namespace.getName() ) ) {
|
final Identifier catalogLogicalName = namespace.getName().getCatalog();
|
||||||
applySqlString(
|
final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();
|
||||||
database.getJdbcEnvironment().getDialect().getCreateSchemaCommand(
|
|
||||||
namespace.getName().getSchema().render( database.getJdbcEnvironment().getDialect() )
|
if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName ) && !existingDatabase
|
||||||
|
.catalogExists( catalogLogicalName ) ) {
|
||||||
|
applySqlStrings(
|
||||||
|
database.getJdbcEnvironment().getDialect().getCreateCatalogCommand(
|
||||||
|
catalogPhysicalName.render(
|
||||||
|
database.getJdbcEnvironment().getDialect()
|
||||||
|
)
|
||||||
),
|
),
|
||||||
targets,
|
targets,
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
exportedCatalogs.add( catalogLogicalName );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( tryToCreateSchemas
|
||||||
|
&& namespace.getPhysicalName().getSchema() != null
|
||||||
|
&& !existingDatabase.schemaExists( namespace.getName() ) ) {
|
||||||
|
applySqlStrings(
|
||||||
|
database.getJdbcEnvironment().getDialect().getCreateSchemaCommand(
|
||||||
|
namespace.getPhysicalName()
|
||||||
|
.getSchema()
|
||||||
|
.render( database.getJdbcEnvironment().getDialect() )
|
||||||
|
),
|
||||||
|
targets,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( Table table : namespace.getTables() ) {
|
for ( Table table : namespace.getTables() ) {
|
||||||
|
@ -240,7 +273,7 @@ public class SchemaMigratorImpl implements SchemaMigrator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private UniqueConstraintSchemaUpdateStrategy determineUniqueConstraintSchemaUpdateStrategy(Metadata metadata) {
|
private UniqueConstraintSchemaUpdateStrategy determineUniqueConstraintSchemaUpdateStrategy(Metadata metadata) {
|
||||||
final ConfigurationService cfgService = ( (MetadataImplementor) metadata ).getMetadataBuildingOptions()
|
final ConfigurationService cfgService = ((MetadataImplementor) metadata).getMetadataBuildingOptions()
|
||||||
.getServiceRegistry()
|
.getServiceRegistry()
|
||||||
.getService( ConfigurationService.class );
|
.getService( ConfigurationService.class );
|
||||||
|
|
||||||
|
|
|
@ -21,21 +21,21 @@ public interface SchemaCreator {
|
||||||
* Perform the creation to the specified targets
|
* Perform the creation to the specified targets
|
||||||
*
|
*
|
||||||
* @param metadata The "compiled" mapping metadata.
|
* @param metadata The "compiled" mapping metadata.
|
||||||
* @param createSchemas Should the schema(s) actually be created as well ({@code CREATE SCHEMA})?
|
* @param createNamespaces Should the schema(s)/catalog(s) actually be created as well ({@code CREATE SCHEMA})?
|
||||||
* @param targets The targets for creation
|
* @param targets The targets for creation
|
||||||
*
|
*
|
||||||
* @throws SchemaManagementException Indicates a problem processing the creation
|
* @throws SchemaManagementException Indicates a problem processing the creation
|
||||||
*/
|
*/
|
||||||
public void doCreation(
|
public void doCreation(
|
||||||
Metadata metadata,
|
Metadata metadata,
|
||||||
boolean createSchemas,
|
boolean createNamespaces,
|
||||||
Target... targets) throws SchemaManagementException;
|
Target... targets) throws SchemaManagementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform the creation to the specified targets
|
* Perform the creation to the specified targets
|
||||||
*
|
*
|
||||||
* @param metadata The "compiled" mapping metadata.
|
* @param metadata The "compiled" mapping metadata.
|
||||||
* @param createSchemas Should the schema(s) actually be created as well ({@code CREATE SCHEMA})?
|
* @param createNamespaces Should the schema(s)/catalog(s) actually be created as well ({@code CREATE SCHEMA})?
|
||||||
* @param dialect Allow explicitly passing the Dialect to use.
|
* @param dialect Allow explicitly passing the Dialect to use.
|
||||||
* @param targets The targets for creation
|
* @param targets The targets for creation
|
||||||
*
|
*
|
||||||
|
@ -43,7 +43,7 @@ public interface SchemaCreator {
|
||||||
*/
|
*/
|
||||||
public void doCreation(
|
public void doCreation(
|
||||||
Metadata metadata,
|
Metadata metadata,
|
||||||
boolean createSchemas,
|
boolean createNamespaces,
|
||||||
Dialect dialect,
|
Dialect dialect,
|
||||||
Target... targets) throws SchemaManagementException;
|
Target... targets) throws SchemaManagementException;
|
||||||
|
|
||||||
|
@ -51,21 +51,21 @@ public interface SchemaCreator {
|
||||||
* Perform the creation to the specified targets
|
* Perform the creation to the specified targets
|
||||||
*
|
*
|
||||||
* @param metadata The "compiled" mapping metadata.
|
* @param metadata The "compiled" mapping metadata.
|
||||||
* @param createSchemas Should the schema(s) actually be created as well ({@code CREATE SCHEMA})?
|
* @param createNamespaces Should the schema(s) actually be created as well ({@code CREATE SCHEMA})?
|
||||||
* @param targets The targets for creation
|
* @param targets The targets for creation
|
||||||
*
|
*
|
||||||
* @throws SchemaManagementException Indicates a problem processing the creation
|
* @throws SchemaManagementException Indicates a problem processing the creation
|
||||||
*/
|
*/
|
||||||
public void doCreation(
|
public void doCreation(
|
||||||
Metadata metadata,
|
Metadata metadata,
|
||||||
boolean createSchemas,
|
boolean createNamespaces,
|
||||||
List<Target> targets) throws SchemaManagementException;
|
List<Target> targets) throws SchemaManagementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform the creation to the specified targets
|
* Perform the creation to the specified targets
|
||||||
*
|
*
|
||||||
* @param metadata The "compiled" mapping metadata.
|
* @param metadata The "compiled" mapping metadata.
|
||||||
* @param createSchemas Should the schema(s) actually be created as well ({@code CREATE SCHEMA})?
|
* @param createNamespaces Should the schema(s)/catalog(s) actually be created as well ({@code CREATE SCHEMA})?
|
||||||
* @param dialect Allow explicitly passing the Dialect to use.
|
* @param dialect Allow explicitly passing the Dialect to use.
|
||||||
* @param targets The targets for creation
|
* @param targets The targets for creation
|
||||||
*
|
*
|
||||||
|
@ -73,7 +73,7 @@ public interface SchemaCreator {
|
||||||
*/
|
*/
|
||||||
public void doCreation(
|
public void doCreation(
|
||||||
Metadata metadata,
|
Metadata metadata,
|
||||||
boolean createSchemas,
|
boolean createNamespaces,
|
||||||
Dialect dialect,
|
Dialect dialect,
|
||||||
List<Target> targets) throws SchemaManagementException;
|
List<Target> targets) throws SchemaManagementException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,42 +21,42 @@ public interface SchemaDropper {
|
||||||
* Perform the drop to the specified targets
|
* Perform the drop to the specified targets
|
||||||
*
|
*
|
||||||
* @param metadata The "compiled" mapping metadata.
|
* @param metadata The "compiled" mapping metadata.
|
||||||
* @param dropSchemas Should the schema(s) actually be dropped also ({@code DROP SCHEMA})?
|
* @param dropNamespaces Should the schema(s)/catalog(s) actually be dropped also ({@code DROP SCHEMA})?
|
||||||
* @param targets The targets for drop
|
* @param targets The targets for drop
|
||||||
*
|
*
|
||||||
* @throws SchemaManagementException Indicates a problem processing the creation
|
* @throws SchemaManagementException Indicates a problem processing the creation
|
||||||
*/
|
*/
|
||||||
public void doDrop(Metadata metadata, boolean dropSchemas, Target... targets) throws SchemaManagementException;
|
public void doDrop(Metadata metadata, boolean dropNamespaces, Target... targets) throws SchemaManagementException;
|
||||||
/**
|
/**
|
||||||
* Perform the drop to the specified targets
|
* Perform the drop to the specified targets
|
||||||
*
|
*
|
||||||
* @param metadata The "compiled" mapping metadata.
|
* @param metadata The "compiled" mapping metadata.
|
||||||
* @param dropSchemas Should the schema(s) actually be dropped also ({@code DROP SCHEMA})?
|
* @param dropNamespaces Should the schema(s)/catalog(s) actually be dropped also ({@code DROP SCHEMA})?
|
||||||
* @param targets The targets for drop
|
* @param targets The targets for drop
|
||||||
*
|
*
|
||||||
* @throws SchemaManagementException Indicates a problem processing the creation
|
* @throws SchemaManagementException Indicates a problem processing the creation
|
||||||
*/
|
*/
|
||||||
public void doDrop(Metadata metadata, boolean dropSchemas, Dialect dialect, Target... targets) throws SchemaManagementException;
|
public void doDrop(Metadata metadata, boolean dropNamespaces, Dialect dialect, Target... targets) throws SchemaManagementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform the drop to the specified targets
|
* Perform the drop to the specified targets
|
||||||
*
|
*
|
||||||
* @param metadata The "compiled" mapping metadata.
|
* @param metadata The "compiled" mapping metadata.
|
||||||
* @param dropSchemas Should the schema(s) actually be dropped also ({@code DROP SCHEMA})?
|
* @param dropNamespaces Should the schema(s)/catalog(s) actually be dropped also ({@code DROP SCHEMA})?
|
||||||
* @param targets The targets for drop
|
* @param targets The targets for drop
|
||||||
*
|
*
|
||||||
* @throws SchemaManagementException Indicates a problem processing the creation
|
* @throws SchemaManagementException Indicates a problem processing the creation
|
||||||
*/
|
*/
|
||||||
public void doDrop(Metadata metadata, boolean dropSchemas, List<Target> targets) throws SchemaManagementException;
|
public void doDrop(Metadata metadata, boolean dropNamespaces, List<Target> targets) throws SchemaManagementException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform the drop to the specified targets
|
* Perform the drop to the specified targets
|
||||||
*
|
*
|
||||||
* @param metadata The "compiled" mapping metadata.
|
* @param metadata The "compiled" mapping metadata.
|
||||||
* @param dropSchemas Should the schema(s) actually be dropped also ({@code DROP SCHEMA})?
|
* @param dropNamespaces Should the schema(s)/catalog(s) actually be dropped also ({@code DROP SCHEMA})?
|
||||||
* @param targets The targets for drop
|
* @param targets The targets for drop
|
||||||
*
|
*
|
||||||
* @throws SchemaManagementException Indicates a problem processing the creation
|
* @throws SchemaManagementException Indicates a problem processing the creation
|
||||||
*/
|
*/
|
||||||
public void doDrop(Metadata metadata, boolean dropSchemas, Dialect dialect, List<Target> targets) throws SchemaManagementException;
|
public void doDrop(Metadata metadata, boolean dropNamespaces, Dialect dialect, List<Target> targets) throws SchemaManagementException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ public interface SchemaMigrator {
|
||||||
*
|
*
|
||||||
* @param metadata The "compiled" mapping metadata.
|
* @param metadata The "compiled" mapping metadata.
|
||||||
* @param existingDatabase Access to the information about the existing database.
|
* @param existingDatabase Access to the information about the existing database.
|
||||||
* @param createSchemas Should the schema(s) actually be created as well ({@code CREATE SCHEMA})?
|
* @param createNamespaces Should the schema(s)/catalog(s) actually be created?
|
||||||
* @param targets The migration targets
|
* @param targets The migration targets
|
||||||
*
|
*
|
||||||
* @throws SchemaManagementException
|
* @throws SchemaManagementException
|
||||||
|
@ -30,6 +30,6 @@ public interface SchemaMigrator {
|
||||||
public void doMigration(
|
public void doMigration(
|
||||||
Metadata metadata,
|
Metadata metadata,
|
||||||
DatabaseInformation existingDatabase,
|
DatabaseInformation existingDatabase,
|
||||||
boolean createSchemas,
|
boolean createNamespaces,
|
||||||
List<Target> targets) throws SchemaManagementException;
|
List<Target> targets) throws SchemaManagementException;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue