Browse Source

DEC-19577 fix: hibernate建表时,调整为只有建表成功才进行添加索引、主键等操作

feature/10.0
lidongy 3 years ago
parent
commit
1f71ce55fa
  1. 24
      fine-hibernate/src/main/java/com/fr/third/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java
  2. 23
      fine-hibernate/src/main/java/com/fr/third/org/hibernate/tool/schema/internal/GroupedSchemaMigratorImpl.java

24
fine-hibernate/src/main/java/com/fr/third/org/hibernate/tool/schema/internal/AbstractSchemaMigrator.java

@ -267,14 +267,14 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
}
}
protected void createTable(
protected boolean createTable(
Table table,
Dialect dialect,
Metadata metadata,
Formatter formatter,
ExecutionOptions options,
GenerationTarget... targets) {
applySqlStrings(
return applySqlStrings(
false,
dialect.getTableExporter().getSqlCreateStrings( table, metadata ),
formatter,
@ -283,7 +283,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
);
}
protected void migrateTable(
protected boolean migrateTable(
Table table,
TableInformation tableInformation,
Dialect dialect,
@ -294,7 +294,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
final Database database = metadata.getDatabase();
//noinspection unchecked
applySqlStrings(
return applySqlStrings(
false,
table.sqlAlterStrings(
dialect,
@ -551,17 +551,19 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
exportIdentifiers.add( exportIdentifier );
}
protected static void applySqlStrings(
protected static boolean applySqlStrings(
boolean quiet,
String[] sqlStrings,
Formatter formatter,
ExecutionOptions options,
GenerationTarget... targets) {
boolean success = true;
if ( sqlStrings != null ) {
for ( String sqlString : sqlStrings ) {
applySqlString( quiet, sqlString, formatter, options, targets );
success &= applySqlString( quiet, sqlString, formatter, options, targets );
}
}
return success;
}
protected void createSchemaAndCatalog(
@ -604,7 +606,7 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
}
}
private static void applySqlString(
private static boolean applySqlString(
boolean quiet,
String sqlString,
Formatter formatter,
@ -619,24 +621,28 @@ public abstract class AbstractSchemaMigrator implements SchemaMigrator {
if ( !quiet ) {
options.getExceptionHandler().handleException( e );
}
return false;
// otherwise ignore the exception
}
}
}
return true;
}
private static void applySqlStrings(
private static boolean applySqlStrings(
boolean quiet,
Iterator<String> sqlStrings,
Formatter formatter,
ExecutionOptions options,
GenerationTarget... targets) {
boolean success = true;
if ( sqlStrings != null ) {
while ( sqlStrings.hasNext() ) {
final String sqlString = sqlStrings.next();
applySqlString( quiet, sqlString, formatter, options, targets );
success &= applySqlString( quiet, sqlString, formatter, options, targets );
}
}
return success;
}
private String getDefaultCatalogName(Database database, Dialect dialect) {

23
fine-hibernate/src/main/java/com/fr/third/org/hibernate/tool/schema/internal/GroupedSchemaMigratorImpl.java

@ -63,28 +63,31 @@ public class GroupedSchemaMigratorImpl extends AbstractSchemaMigrator {
targets
);
final NameSpaceTablesInformation tables = existingDatabase.getTablesInformation( namespace );
boolean tableValid = true;
for ( Table table : namespace.getTables() ) {
if ( schemaFilter.includeTable( table ) && table.isPhysicalTable() ) {
checkExportIdentifier( table, exportIdentifiers );
final TableInformation tableInformation = tables.getTableInformation( table );
if ( tableInformation == null ) {
createTable( table, dialect, metadata, formatter, options, targets );
tableValid &= createTable( table, dialect, metadata, formatter, options, targets );
}
else if ( tableInformation != null && tableInformation.isPhysicalTable() ) {
tablesInformation.addTableInformation( tableInformation );
migrateTable( table, tableInformation, dialect, metadata, formatter, options, targets );
tableValid &= migrateTable( table, tableInformation, dialect, metadata, formatter, options, targets );
}
}
}
for ( Table table : namespace.getTables() ) {
if ( schemaFilter.includeTable( table ) && table.isPhysicalTable() ) {
final TableInformation tableInformation = tablesInformation.getTableInformation( table );
if ( tableInformation == null || ( tableInformation != null && tableInformation.isPhysicalTable() ) ) {
applyIndexes( table, tableInformation, dialect, metadata, formatter, options, targets );
applyUniqueKeys( table, tableInformation, dialect, metadata, formatter, options, targets );
if (tableInformation != null) {
applyPrimaryKey(table, tableInformation, dialect, metadata, formatter, options, targets);
if (tableValid) {
for (Table table : namespace.getTables()) {
if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
final TableInformation tableInformation = tablesInformation.getTableInformation(table);
if (tableInformation == null || (tableInformation != null && tableInformation.isPhysicalTable())) {
applyIndexes(table, tableInformation, dialect, metadata, formatter, options, targets);
applyUniqueKeys(table, tableInformation, dialect, metadata, formatter, options, targets);
if (tableInformation != null) {
applyPrimaryKey(table, tableInformation, dialect, metadata, formatter, options, targets);
}
}
}
}

Loading…
Cancel
Save