1212
1313import org .hibernate .boot .Metadata ;
1414import org .hibernate .boot .model .naming .Identifier ;
15- import org .hibernate .boot .model .relational .AuxiliaryDatabaseObject ;
16- import org .hibernate .boot .model .relational .Database ;
1715import org .hibernate .boot .model .relational .Exportable ;
1816import org .hibernate .boot .model .relational .Namespace ;
1917import org .hibernate .boot .model .relational .Sequence ;
2422import org .hibernate .engine .jdbc .env .spi .JdbcEnvironment ;
2523import org .hibernate .engine .jdbc .internal .FormatStyle ;
2624import org .hibernate .engine .jdbc .internal .Formatter ;
27- import org .hibernate .internal .util .StringHelper ;
28- import org .hibernate .internal .util .collections .CollectionHelper ;
2925import org .hibernate .mapping .ForeignKey ;
3026import org .hibernate .mapping .Index ;
3127import org .hibernate .mapping .Table ;
32- import org .hibernate .mapping .UniqueKey ;
33- import org .hibernate .resource .transaction .spi .DdlTransactionIsolator ;
3428import org .hibernate .tool .schema .UniqueConstraintSchemaUpdateStrategy ;
35- import org .hibernate .tool .schema .extract .spi .ColumnInformation ;
3629import org .hibernate .tool .schema .extract .spi .DatabaseInformation ;
3730import org .hibernate .tool .schema .extract .spi .IndexInformation ;
3831import org .hibernate .tool .schema .extract .spi .NameSpaceTablesInformation ;
39- import org .hibernate .tool .schema .extract .spi .SequenceInformation ;
4032import org .hibernate .tool .schema .extract .spi .TableInformation ;
4133import org .hibernate .tool .schema .spi .GenerationTarget ;
42- import org .hibernate .tool .schema .internal .exec .JdbcContext ;
4334import org .hibernate .tool .schema .spi .CommandAcceptanceException ;
4435import org .hibernate .tool .schema .spi .ContributableMatcher ;
4536import org .hibernate .tool .schema .spi .ExecutionOptions ;
46- import org .hibernate .tool .schema .spi .Exporter ;
4737import org .hibernate .tool .schema .spi .SchemaFilter ;
4838import org .hibernate .tool .schema .spi .SchemaManagementException ;
4939import org .hibernate .tool .schema .spi .SchemaMigrator ;
5444import static org .hibernate .cfg .SchemaToolingSettings .UNIQUE_CONSTRAINT_SCHEMA_UPDATE_STRATEGY ;
5545import static org .hibernate .engine .config .spi .StandardConverters .STRING ;
5646import static org .hibernate .internal .util .StringHelper .isEmpty ;
47+ import static org .hibernate .internal .util .StringHelper .isNotEmpty ;
48+ import static org .hibernate .internal .util .collections .CollectionHelper .setOfSize ;
5749import static org .hibernate .tool .schema .UniqueConstraintSchemaUpdateStrategy .DROP_RECREATE_QUIETLY ;
5850import static org .hibernate .tool .schema .UniqueConstraintSchemaUpdateStrategy .SKIP ;
51+ import static org .hibernate .tool .schema .internal .Helper .buildDatabaseInformation ;
52+ import static org .hibernate .tool .schema .internal .Helper .interpretFormattingEnabled ;
5953import static org .hibernate .tool .schema .internal .SchemaCreatorImpl .createUserDefinedTypes ;
6054import static org .hibernate .tool .schema .internal .SchemaDropperImpl .dropUserDefinedTypes ;
6155
@@ -82,25 +76,20 @@ public void doMigration(
8276 ExecutionOptions options ,
8377 ContributableMatcher contributableInclusionFilter ,
8478 TargetDescriptor targetDescriptor ) {
85- final SqlStringGenerationContext sqlGenerationContext = sqlGenerationContext ( metadata , options );
79+ final var sqlGenerationContext = sqlGenerationContext ( metadata , options );
8680 if ( !targetDescriptor .getTargetTypes ().isEmpty () ) {
87- final JdbcContext jdbcContext = tool .resolveJdbcContext ( options .getConfigurationValues () );
88- try ( DdlTransactionIsolator ddlTransactionIsolator = tool .getDdlTransactionIsolator ( jdbcContext ) ) {
89- final DatabaseInformation databaseInformation = Helper .buildDatabaseInformation (
90- tool .getServiceRegistry (),
91- ddlTransactionIsolator ,
92- sqlGenerationContext ,
93- tool
94- );
95-
96- final GenerationTarget [] targets = tool .buildGenerationTargets (
81+ final var jdbcContext = tool .resolveJdbcContext ( options .getConfigurationValues () );
82+ try ( var isolator = tool .getDdlTransactionIsolator ( jdbcContext ) ) {
83+ final var databaseInformation =
84+ buildDatabaseInformation ( isolator , sqlGenerationContext , tool );
85+ final var targets = tool .buildGenerationTargets (
9786 targetDescriptor ,
98- ddlTransactionIsolator ,
87+ isolator ,
9988 options .getConfigurationValues ()
10089 );
10190
10291 try {
103- for ( GenerationTarget target : targets ) {
92+ for ( var target : targets ) {
10493 target .prepare ();
10594 }
10695
@@ -116,7 +105,7 @@ public void doMigration(
116105 );
117106 }
118107 finally {
119- for ( GenerationTarget target : targets ) {
108+ for ( var target : targets ) {
120109 try {
121110 target .release ();
122111 }
@@ -169,16 +158,16 @@ private void performMigration(
169158 Dialect dialect ,
170159 SqlStringGenerationContext sqlGenerationContext ,
171160 GenerationTarget ... targets ) {
172- final boolean format = Helper . interpretFormattingEnabled ( options .getConfigurationValues () );
173- final Formatter formatter = format ? FormatStyle .DDL .getFormatter () : FormatStyle .NONE .getFormatter ();
161+ final boolean format = interpretFormattingEnabled ( options .getConfigurationValues () );
162+ final var formatter = format ? FormatStyle .DDL .getFormatter () : FormatStyle .NONE .getFormatter ();
174163
175- final Set <String > exportIdentifiers = CollectionHelper . setOfSize ( 50 );
164+ final Set <String > exportIdentifiers = setOfSize ( 50 );
176165
177- final Database database = metadata .getDatabase ();
178- final Exporter < AuxiliaryDatabaseObject > auxiliaryExporter = dialect .getAuxiliaryDatabaseObjectExporter ();
166+ final var database = metadata .getDatabase ();
167+ final var auxiliaryExporter = dialect .getAuxiliaryDatabaseObjectExporter ();
179168
180169 // Drop all AuxiliaryDatabaseObjects
181- for ( AuxiliaryDatabaseObject auxiliaryDatabaseObject : database .getAuxiliaryDatabaseObjects () ) {
170+ for ( var auxiliaryDatabaseObject : database .getAuxiliaryDatabaseObjects () ) {
182171 if ( auxiliaryDatabaseObject .appliesToDialect ( dialect ) ) {
183172 applySqlStrings (
184173 true ,
@@ -194,7 +183,7 @@ private void performMigration(
194183 dropUserDefinedTypes ( metadata , options , schemaFilter , dialect , formatter , sqlGenerationContext , targets );
195184
196185 // Create before-table AuxiliaryDatabaseObjects
197- for ( AuxiliaryDatabaseObject auxiliaryDatabaseObject : database .getAuxiliaryDatabaseObjects () ) {
186+ for ( var auxiliaryDatabaseObject : database .getAuxiliaryDatabaseObjects () ) {
198187 if ( auxiliaryDatabaseObject .beforeTablesOnCreation ()
199188 && auxiliaryDatabaseObject .appliesToDialect ( dialect ) ) {
200189 applySqlStrings (
@@ -222,8 +211,8 @@ private void performMigration(
222211 }
223212 final Map <Namespace , NameSpaceTablesInformation > tablesInformation = new HashMap <>();
224213 final Set <Identifier > exportedCatalogs = new HashSet <>();
225- for ( Namespace namespace : database .getNamespaces () ) {
226- final NameSpaceTablesInformation nameSpaceTablesInformation = performTablesMigration (
214+ for ( var namespace : database .getNamespaces () ) {
215+ final var nameSpaceTablesInformation = performTablesMigration (
227216 metadata ,
228217 existingDatabase ,
229218 options ,
@@ -239,10 +228,10 @@ private void performMigration(
239228 );
240229 tablesInformation .put ( namespace , nameSpaceTablesInformation );
241230 if ( schemaFilter .includeNamespace ( namespace ) ) {
242- for ( Sequence sequence : namespace .getSequences () ) {
231+ for ( var sequence : namespace .getSequences () ) {
243232 if ( contributableInclusionFilter .matches ( sequence ) ) {
244233 checkExportIdentifier ( sequence , exportIdentifiers );
245- final SequenceInformation sequenceInformation = existingDatabase .getSequenceInformation ( sequence .getName () );
234+ final var sequenceInformation = existingDatabase .getSequenceInformation ( sequence .getName () );
246235 if ( sequenceInformation == null ) {
247236 applySequence ( sequence , dialect , metadata , formatter , options , sqlGenerationContext , targets );
248237 }
@@ -252,12 +241,12 @@ private void performMigration(
252241 }
253242
254243 //NOTE: Foreign keys must be created *after* all tables of all namespaces for cross-namespace fks. see HHH-10420
255- for ( Namespace namespace : database .getNamespaces () ) {
244+ for ( var namespace : database .getNamespaces () ) {
256245 if ( schemaFilter .includeNamespace ( namespace ) ) {
257- final NameSpaceTablesInformation nameSpaceTablesInformation = tablesInformation .get ( namespace );
258- for ( Table table : namespace .getTables () ) {
246+ final var nameSpaceTablesInformation = tablesInformation .get ( namespace );
247+ for ( var table : namespace .getTables () ) {
259248 if ( schemaFilter .includeTable ( table ) && contributableInclusionFilter .matches ( table ) ) {
260- final TableInformation tableInformation = nameSpaceTablesInformation .getTableInformation ( table );
249+ final var tableInformation = nameSpaceTablesInformation .getTableInformation ( table );
261250 if ( tableInformation == null || tableInformation .isPhysicalTable () ) {
262251 applyForeignKeys ( table , tableInformation , dialect , metadata , formatter , options ,
263252 sqlGenerationContext , targets );
@@ -268,8 +257,9 @@ private void performMigration(
268257 }
269258
270259 // Create after-table AuxiliaryDatabaseObjects
271- for ( AuxiliaryDatabaseObject auxiliaryDatabaseObject : database .getAuxiliaryDatabaseObjects () ) {
272- if ( !auxiliaryDatabaseObject .beforeTablesOnCreation () && auxiliaryDatabaseObject .appliesToDialect ( dialect ) ) {
260+ for ( var auxiliaryDatabaseObject : database .getAuxiliaryDatabaseObjects () ) {
261+ if ( !auxiliaryDatabaseObject .beforeTablesOnCreation ()
262+ && auxiliaryDatabaseObject .appliesToDialect ( dialect ) ) {
273263 applySqlStrings (
274264 true ,
275265 auxiliaryExporter .getSqlCreateStrings ( auxiliaryDatabaseObject , metadata , sqlGenerationContext ),
@@ -291,7 +281,8 @@ private static void applySequence(
291281 GenerationTarget ... targets ) {
292282 applySqlStrings (
293283 false ,
294- dialect .getSequenceExporter ().getSqlCreateStrings ( sequence , metadata , sqlGenerationContext ),
284+ dialect .getSequenceExporter ()
285+ .getSqlCreateStrings ( sequence , metadata , sqlGenerationContext ),
295286 formatter ,
296287 options ,
297288 targets
@@ -308,7 +299,8 @@ protected void createTable(
308299 GenerationTarget ... targets ) {
309300 applySqlStrings (
310301 false ,
311- dialect .getTableExporter ().getSqlCreateStrings ( table , metadata , sqlGenerationContext ),
302+ dialect .getTableExporter ()
303+ .getSqlCreateStrings ( table , metadata , sqlGenerationContext ),
312304 formatter ,
313305 options ,
314306 targets
@@ -343,13 +335,13 @@ protected void applyIndexes(
343335 ExecutionOptions options ,
344336 SqlStringGenerationContext sqlGenerationContext ,
345337 GenerationTarget ... targets ) {
346- final Exporter < Index > exporter = dialect .getIndexExporter ();
347- for ( Index index : table .getIndexes ().values () ) {
338+ final var exporter = dialect .getIndexExporter ();
339+ for ( var index : table .getIndexes ().values () ) {
348340 if ( !isEmpty ( index .getName () ) ) {
349- IndexInformation existingIndex = null ;
350- if ( tableInformation != null ) {
351- existingIndex = findMatchingIndex ( index , tableInformation );
352- }
341+ final var existingIndex =
342+ tableInformation != null
343+ ? findMatchingIndex ( index , tableInformation )
344+ : null ;
353345 if ( existingIndex == null ) {
354346 applySqlStrings (
355347 false ,
@@ -381,15 +373,15 @@ protected void applyUniqueKeys(
381373 }
382374
383375 if ( uniqueConstraintStrategy != SKIP ) {
384- final Exporter < UniqueKey > exporter = dialect .getUniqueKeyExporter ();
385- for ( UniqueKey uniqueKey : table .getUniqueKeys ().values () ) {
376+ final var exporter = dialect .getUniqueKeyExporter ();
377+ for ( var uniqueKey : table .getUniqueKeys ().values () ) {
386378 // Skip if index already exists. Most of the time, this
387379 // won't work since most Dialects use Constraints. However,
388380 // keep it for the few that do use Indexes.
389- IndexInformation indexInfo = null ;
390- if ( tableInfo != null && StringHelper . isNotEmpty ( uniqueKey .getName () ) ) {
391- indexInfo = tableInfo .getIndex ( Identifier .toIdentifier ( uniqueKey .getName () ) );
392- }
381+ final var indexInfo =
382+ tableInfo != null && isNotEmpty ( uniqueKey .getName () )
383+ ? tableInfo .getIndex ( Identifier .toIdentifier ( uniqueKey .getName () ) )
384+ : null ;
393385 if ( indexInfo == null ) {
394386 if ( uniqueConstraintStrategy == DROP_RECREATE_QUIETLY ) {
395387 applySqlStrings (
@@ -430,8 +422,8 @@ protected void applyForeignKeys(
430422 SqlStringGenerationContext sqlGenerationContext ,
431423 GenerationTarget ... targets ) {
432424 if ( dialect .hasAlterTable () ) {
433- final Exporter < ForeignKey > exporter = dialect .getForeignKeyExporter ();
434- for ( ForeignKey foreignKey : table .getForeignKeyCollection () ) {
425+ final var exporter = dialect .getForeignKeyExporter ();
426+ for ( var foreignKey : table .getForeignKeyCollection () ) {
435427 if ( foreignKey .isPhysicalConstraint ()
436428 && foreignKey .isCreationEnabled ()
437429 && ( tableInformation == null || !checkForExistingForeignKey ( foreignKey , tableInformation ) ) ) {
@@ -478,8 +470,8 @@ boolean equivalentForeignKeyExistsInDatabase(TableInformation tableInformation,
478470 return StreamSupport .stream ( tableInformation .getForeignKeys ().spliterator (), false )
479471 .flatMap ( foreignKeyInformation -> StreamSupport .stream ( foreignKeyInformation .getColumnReferenceMappings ().spliterator (), false ) )
480472 .anyMatch ( columnReferenceMapping -> {
481- final ColumnInformation referencingColumnMetadata = columnReferenceMapping .getReferencingColumnMetadata ();
482- final ColumnInformation referencedColumnMetadata = columnReferenceMapping .getReferencedColumnMetadata ();
473+ final var referencingColumnMetadata = columnReferenceMapping .getReferencingColumnMetadata ();
474+ final var referencedColumnMetadata = columnReferenceMapping .getReferencedColumnMetadata ();
483475 final String existingReferencingColumn = referencingColumnMetadata .getColumnIdentifier ().getText ();
484476 final String existingReferencedTable =
485477 referencedColumnMetadata .getContainingTableInformation ().getName ().getTableName ().getCanonicalName ();
@@ -564,7 +556,7 @@ private static void applySqlString(
564556 GenerationTarget ... targets ) {
565557 if ( !isEmpty ( sql ) ) {
566558 final String formattedSql = formatter .format ( sql );
567- for ( GenerationTarget target : targets ) {
559+ for ( var target : targets ) {
568560 try {
569561 target .accept ( formattedSql );
570562 }
0 commit comments