12
12
13
13
import org .hibernate .boot .Metadata ;
14
14
import org .hibernate .boot .model .naming .Identifier ;
15
- import org .hibernate .boot .model .relational .AuxiliaryDatabaseObject ;
16
- import org .hibernate .boot .model .relational .Database ;
17
15
import org .hibernate .boot .model .relational .Exportable ;
18
16
import org .hibernate .boot .model .relational .Namespace ;
19
17
import org .hibernate .boot .model .relational .Sequence ;
24
22
import org .hibernate .engine .jdbc .env .spi .JdbcEnvironment ;
25
23
import org .hibernate .engine .jdbc .internal .FormatStyle ;
26
24
import org .hibernate .engine .jdbc .internal .Formatter ;
27
- import org .hibernate .internal .util .StringHelper ;
28
- import org .hibernate .internal .util .collections .CollectionHelper ;
29
25
import org .hibernate .mapping .ForeignKey ;
30
26
import org .hibernate .mapping .Index ;
31
27
import org .hibernate .mapping .Table ;
32
- import org .hibernate .mapping .UniqueKey ;
33
- import org .hibernate .resource .transaction .spi .DdlTransactionIsolator ;
34
28
import org .hibernate .tool .schema .UniqueConstraintSchemaUpdateStrategy ;
35
- import org .hibernate .tool .schema .extract .spi .ColumnInformation ;
36
29
import org .hibernate .tool .schema .extract .spi .DatabaseInformation ;
37
30
import org .hibernate .tool .schema .extract .spi .IndexInformation ;
38
31
import org .hibernate .tool .schema .extract .spi .NameSpaceTablesInformation ;
39
- import org .hibernate .tool .schema .extract .spi .SequenceInformation ;
40
32
import org .hibernate .tool .schema .extract .spi .TableInformation ;
41
33
import org .hibernate .tool .schema .spi .GenerationTarget ;
42
- import org .hibernate .tool .schema .internal .exec .JdbcContext ;
43
34
import org .hibernate .tool .schema .spi .CommandAcceptanceException ;
44
35
import org .hibernate .tool .schema .spi .ContributableMatcher ;
45
36
import org .hibernate .tool .schema .spi .ExecutionOptions ;
46
- import org .hibernate .tool .schema .spi .Exporter ;
47
37
import org .hibernate .tool .schema .spi .SchemaFilter ;
48
38
import org .hibernate .tool .schema .spi .SchemaManagementException ;
49
39
import org .hibernate .tool .schema .spi .SchemaMigrator ;
54
44
import static org .hibernate .cfg .SchemaToolingSettings .UNIQUE_CONSTRAINT_SCHEMA_UPDATE_STRATEGY ;
55
45
import static org .hibernate .engine .config .spi .StandardConverters .STRING ;
56
46
import 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 ;
57
49
import static org .hibernate .tool .schema .UniqueConstraintSchemaUpdateStrategy .DROP_RECREATE_QUIETLY ;
58
50
import 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 ;
59
53
import static org .hibernate .tool .schema .internal .SchemaCreatorImpl .createUserDefinedTypes ;
60
54
import static org .hibernate .tool .schema .internal .SchemaDropperImpl .dropUserDefinedTypes ;
61
55
@@ -82,25 +76,20 @@ public void doMigration(
82
76
ExecutionOptions options ,
83
77
ContributableMatcher contributableInclusionFilter ,
84
78
TargetDescriptor targetDescriptor ) {
85
- final SqlStringGenerationContext sqlGenerationContext = sqlGenerationContext ( metadata , options );
79
+ final var sqlGenerationContext = sqlGenerationContext ( metadata , options );
86
80
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 (
97
86
targetDescriptor ,
98
- ddlTransactionIsolator ,
87
+ isolator ,
99
88
options .getConfigurationValues ()
100
89
);
101
90
102
91
try {
103
- for ( GenerationTarget target : targets ) {
92
+ for ( var target : targets ) {
104
93
target .prepare ();
105
94
}
106
95
@@ -116,7 +105,7 @@ public void doMigration(
116
105
);
117
106
}
118
107
finally {
119
- for ( GenerationTarget target : targets ) {
108
+ for ( var target : targets ) {
120
109
try {
121
110
target .release ();
122
111
}
@@ -169,16 +158,16 @@ private void performMigration(
169
158
Dialect dialect ,
170
159
SqlStringGenerationContext sqlGenerationContext ,
171
160
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 ();
174
163
175
- final Set <String > exportIdentifiers = CollectionHelper . setOfSize ( 50 );
164
+ final Set <String > exportIdentifiers = setOfSize ( 50 );
176
165
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 ();
179
168
180
169
// Drop all AuxiliaryDatabaseObjects
181
- for ( AuxiliaryDatabaseObject auxiliaryDatabaseObject : database .getAuxiliaryDatabaseObjects () ) {
170
+ for ( var auxiliaryDatabaseObject : database .getAuxiliaryDatabaseObjects () ) {
182
171
if ( auxiliaryDatabaseObject .appliesToDialect ( dialect ) ) {
183
172
applySqlStrings (
184
173
true ,
@@ -194,7 +183,7 @@ private void performMigration(
194
183
dropUserDefinedTypes ( metadata , options , schemaFilter , dialect , formatter , sqlGenerationContext , targets );
195
184
196
185
// Create before-table AuxiliaryDatabaseObjects
197
- for ( AuxiliaryDatabaseObject auxiliaryDatabaseObject : database .getAuxiliaryDatabaseObjects () ) {
186
+ for ( var auxiliaryDatabaseObject : database .getAuxiliaryDatabaseObjects () ) {
198
187
if ( auxiliaryDatabaseObject .beforeTablesOnCreation ()
199
188
&& auxiliaryDatabaseObject .appliesToDialect ( dialect ) ) {
200
189
applySqlStrings (
@@ -222,8 +211,8 @@ private void performMigration(
222
211
}
223
212
final Map <Namespace , NameSpaceTablesInformation > tablesInformation = new HashMap <>();
224
213
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 (
227
216
metadata ,
228
217
existingDatabase ,
229
218
options ,
@@ -239,10 +228,10 @@ private void performMigration(
239
228
);
240
229
tablesInformation .put ( namespace , nameSpaceTablesInformation );
241
230
if ( schemaFilter .includeNamespace ( namespace ) ) {
242
- for ( Sequence sequence : namespace .getSequences () ) {
231
+ for ( var sequence : namespace .getSequences () ) {
243
232
if ( contributableInclusionFilter .matches ( sequence ) ) {
244
233
checkExportIdentifier ( sequence , exportIdentifiers );
245
- final SequenceInformation sequenceInformation = existingDatabase .getSequenceInformation ( sequence .getName () );
234
+ final var sequenceInformation = existingDatabase .getSequenceInformation ( sequence .getName () );
246
235
if ( sequenceInformation == null ) {
247
236
applySequence ( sequence , dialect , metadata , formatter , options , sqlGenerationContext , targets );
248
237
}
@@ -252,12 +241,12 @@ private void performMigration(
252
241
}
253
242
254
243
//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 () ) {
256
245
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 () ) {
259
248
if ( schemaFilter .includeTable ( table ) && contributableInclusionFilter .matches ( table ) ) {
260
- final TableInformation tableInformation = nameSpaceTablesInformation .getTableInformation ( table );
249
+ final var tableInformation = nameSpaceTablesInformation .getTableInformation ( table );
261
250
if ( tableInformation == null || tableInformation .isPhysicalTable () ) {
262
251
applyForeignKeys ( table , tableInformation , dialect , metadata , formatter , options ,
263
252
sqlGenerationContext , targets );
@@ -268,8 +257,9 @@ private void performMigration(
268
257
}
269
258
270
259
// 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 ) ) {
273
263
applySqlStrings (
274
264
true ,
275
265
auxiliaryExporter .getSqlCreateStrings ( auxiliaryDatabaseObject , metadata , sqlGenerationContext ),
@@ -291,7 +281,8 @@ private static void applySequence(
291
281
GenerationTarget ... targets ) {
292
282
applySqlStrings (
293
283
false ,
294
- dialect .getSequenceExporter ().getSqlCreateStrings ( sequence , metadata , sqlGenerationContext ),
284
+ dialect .getSequenceExporter ()
285
+ .getSqlCreateStrings ( sequence , metadata , sqlGenerationContext ),
295
286
formatter ,
296
287
options ,
297
288
targets
@@ -308,7 +299,8 @@ protected void createTable(
308
299
GenerationTarget ... targets ) {
309
300
applySqlStrings (
310
301
false ,
311
- dialect .getTableExporter ().getSqlCreateStrings ( table , metadata , sqlGenerationContext ),
302
+ dialect .getTableExporter ()
303
+ .getSqlCreateStrings ( table , metadata , sqlGenerationContext ),
312
304
formatter ,
313
305
options ,
314
306
targets
@@ -343,13 +335,13 @@ protected void applyIndexes(
343
335
ExecutionOptions options ,
344
336
SqlStringGenerationContext sqlGenerationContext ,
345
337
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 () ) {
348
340
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 ;
353
345
if ( existingIndex == null ) {
354
346
applySqlStrings (
355
347
false ,
@@ -381,15 +373,15 @@ protected void applyUniqueKeys(
381
373
}
382
374
383
375
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 () ) {
386
378
// Skip if index already exists. Most of the time, this
387
379
// won't work since most Dialects use Constraints. However,
388
380
// 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 ;
393
385
if ( indexInfo == null ) {
394
386
if ( uniqueConstraintStrategy == DROP_RECREATE_QUIETLY ) {
395
387
applySqlStrings (
@@ -430,8 +422,8 @@ protected void applyForeignKeys(
430
422
SqlStringGenerationContext sqlGenerationContext ,
431
423
GenerationTarget ... targets ) {
432
424
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 () ) {
435
427
if ( foreignKey .isPhysicalConstraint ()
436
428
&& foreignKey .isCreationEnabled ()
437
429
&& ( tableInformation == null || !checkForExistingForeignKey ( foreignKey , tableInformation ) ) ) {
@@ -478,8 +470,8 @@ boolean equivalentForeignKeyExistsInDatabase(TableInformation tableInformation,
478
470
return StreamSupport .stream ( tableInformation .getForeignKeys ().spliterator (), false )
479
471
.flatMap ( foreignKeyInformation -> StreamSupport .stream ( foreignKeyInformation .getColumnReferenceMappings ().spliterator (), false ) )
480
472
.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 ();
483
475
final String existingReferencingColumn = referencingColumnMetadata .getColumnIdentifier ().getText ();
484
476
final String existingReferencedTable =
485
477
referencedColumnMetadata .getContainingTableInformation ().getName ().getTableName ().getCanonicalName ();
@@ -564,7 +556,7 @@ private static void applySqlString(
564
556
GenerationTarget ... targets ) {
565
557
if ( !isEmpty ( sql ) ) {
566
558
final String formattedSql = formatter .format ( sql );
567
- for ( GenerationTarget target : targets ) {
559
+ for ( var target : targets ) {
568
560
try {
569
561
target .accept ( formattedSql );
570
562
}
0 commit comments