HHH-13788 Schema update try to recreate existing tables

This commit is contained in:
Andrea Boriero 2019-12-17 13:51:02 +00:00 committed by Andrea Boriero
parent dfdc439f66
commit b17e17cdb6
1 changed files with 128 additions and 26 deletions

View File

@ -29,6 +29,8 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters; import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper; import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
@ -59,12 +61,26 @@ public class InformationExtractorJdbcDatabaseMetaDataImpl implements Information
private final ExtractionContext extractionContext; private final ExtractionContext extractionContext;
private final boolean useJdbcMetadataDefaultsSetting;
private Identifier currentCatalog;
private Identifier currentSchema;
private String currentCatalogFilter;
private String currentSchemaFilter;
public InformationExtractorJdbcDatabaseMetaDataImpl(ExtractionContext extractionContext) { public InformationExtractorJdbcDatabaseMetaDataImpl(ExtractionContext extractionContext) {
this.extractionContext = extractionContext; this.extractionContext = extractionContext;
ConfigurationService configService = extractionContext.getServiceRegistry() ConfigurationService configService = extractionContext.getServiceRegistry()
.getService( ConfigurationService.class ); .getService( ConfigurationService.class );
useJdbcMetadataDefaultsSetting = configService.getSetting(
"hibernate.temp.use_jdbc_metadata_defaults",
StandardConverters.BOOLEAN,
Boolean.TRUE
);
final String extraPhysycalTableTypesConfig = configService.getSetting( final String extraPhysycalTableTypesConfig = configService.getSetting(
AvailableSettings.EXTRA_PHYSICAL_TABLE_TYPES, AvailableSettings.EXTRA_PHYSICAL_TABLE_TYPES,
StandardConverters.STRING, StandardConverters.STRING,
@ -229,11 +245,14 @@ public class InformationExtractorJdbcDatabaseMetaDataImpl implements Information
TableInformation tableInfo = null; TableInformation tableInfo = null;
// 1) look in current namespace // 1) look in current namespace
if ( extractionContext.getJdbcEnvironment().getCurrentCatalog() != null final JdbcEnvironment jdbcEnvironment = extractionContext.getJdbcEnvironment();
|| extractionContext.getJdbcEnvironment().getCurrentSchema() != null ) { final Identifier currentSchema = getCurrentSchema( jdbcEnvironment );
final Identifier currentCatalog = getCurrentCatalog( jdbcEnvironment );
if ( currentCatalog != null
|| currentSchema != null ) {
tableInfo = locateTableInNamespace( tableInfo = locateTableInNamespace(
extractionContext.getJdbcEnvironment().getCurrentCatalog(), currentCatalog,
extractionContext.getJdbcEnvironment().getCurrentSchema(), currentSchema,
tableName tableName
); );
@ -288,23 +307,106 @@ public class InformationExtractorJdbcDatabaseMetaDataImpl implements Information
} }
} }
private Identifier getCurrentSchema(JdbcEnvironment jdbcEnvironment) {
if ( currentSchema != null ) {
return currentSchema;
}
final Identifier schema = jdbcEnvironment.getCurrentSchema();
if ( schema != null ) {
currentSchema = schema;
}
if ( !useJdbcMetadataDefaultsSetting ) {
try {
currentSchema = extractionContext.getJdbcEnvironment()
.getIdentifierHelper()
.toIdentifier( extractionContext.getJdbcConnection().getSchema() );
}
catch (SQLException ignore) {
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
}
}
return currentCatalog;
}
private Identifier getCurrentCatalog(JdbcEnvironment jdbcEnvironment) {
if ( currentCatalog != null ) {
return currentCatalog;
}
final Identifier catalog = jdbcEnvironment.getCurrentCatalog();
if ( catalog != null ) {
currentCatalog = catalog;
}
if ( !useJdbcMetadataDefaultsSetting ) {
try {
currentCatalog = extractionContext.getJdbcEnvironment()
.getIdentifierHelper()
.toIdentifier( extractionContext.getJdbcConnection().getCatalog() );
}
catch (SQLException ignore) {
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
}
}
return currentCatalog;
}
private String getCurrentCatalogFilter(JdbcEnvironment jdbcEnvironment) {
if ( currentCatalogFilter != null ) {
return currentCatalogFilter;
}
final Identifier currentCatalog = jdbcEnvironment.getCurrentCatalog();
if ( currentCatalog != null ) {
currentCatalogFilter = toMetaDataObjectName( currentCatalog );
}
if ( !useJdbcMetadataDefaultsSetting ) {
try {
currentCatalogFilter = extractionContext.getJdbcConnection().getCatalog();
}
catch (SQLException ignore) {
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
}
}
return currentCatalogFilter;
}
private String getCurrentSchemaFilter(JdbcEnvironment jdbcEnvironment) {
if ( currentSchemaFilter != null ) {
return currentSchemaFilter;
}
final Identifier currentSchema = jdbcEnvironment.getCurrentSchema();
if ( currentSchema != null ) {
currentSchemaFilter = toMetaDataObjectName( currentSchema );
}
if ( !useJdbcMetadataDefaultsSetting ) {
try {
currentSchemaFilter = extractionContext.getJdbcConnection().getSchema();
}
catch (SQLException ignore) {
log.sqlWarning( ignore.getErrorCode(), ignore.getSQLState() );
}
}
return currentSchemaFilter;
}
public NameSpaceTablesInformation getTables(Identifier catalog, Identifier schema) { public NameSpaceTablesInformation getTables(Identifier catalog, Identifier schema) {
String catalogFilter = null; String catalogFilter = null;
String schemaFilter = null; String schemaFilter = null;
if ( extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsCatalogs() ) { final JdbcEnvironment jdbcEnvironment = extractionContext.getJdbcEnvironment();
final NameQualifierSupport nameQualifierSupport = jdbcEnvironment.getNameQualifierSupport();
if ( nameQualifierSupport.supportsCatalogs() ) {
if ( catalog == null ) { if ( catalog == null ) {
if ( extractionContext.getJdbcEnvironment().getCurrentCatalog() != null ) { // look in the current namespace
// 1) look in current namespace catalogFilter = getCurrentCatalogFilter(jdbcEnvironment);
catalogFilter = toMetaDataObjectName( extractionContext.getJdbcEnvironment().getCurrentCatalog() ); if ( catalogFilter == null ) {
} if ( extractionContext.getDefaultCatalog() != null ) {
else if ( extractionContext.getDefaultCatalog() != null ) { // 2) look in default namespace
// 2) look in default namespace catalogFilter = toMetaDataObjectName( extractionContext.getDefaultCatalog() );
catalogFilter = toMetaDataObjectName( extractionContext.getDefaultCatalog() ); }
} else {
else { catalogFilter = "";
catalogFilter = ""; }
} }
} }
else { else {
@ -312,18 +414,18 @@ public class InformationExtractorJdbcDatabaseMetaDataImpl implements Information
} }
} }
if ( extractionContext.getJdbcEnvironment().getNameQualifierSupport().supportsSchemas() ) { if ( nameQualifierSupport.supportsSchemas() ) {
if ( schema == null ) { if ( schema == null ) {
if ( extractionContext.getJdbcEnvironment().getCurrentSchema() != null ) { // 1) look in current namespace
// 1) look in current namespace schemaFilter = getCurrentSchemaFilter( jdbcEnvironment );
schemaFilter = toMetaDataObjectName( extractionContext.getJdbcEnvironment().getCurrentSchema() ); if ( schemaFilter == null ) {
} if ( extractionContext.getDefaultSchema() != null ) {
else if ( extractionContext.getDefaultSchema() != null ) { // 2) look in default namespace
// 2) look in default namespace schemaFilter = toMetaDataObjectName( extractionContext.getDefaultSchema() );
schemaFilter = toMetaDataObjectName( extractionContext.getDefaultSchema() ); }
} else {
else { schemaFilter = "";
schemaFilter = ""; }
} }
} }
else { else {