21
21
import liquibase .change .ColumnConfig ;
22
22
import liquibase .change .ConstraintsConfig ;
23
23
import liquibase .change .core .AddColumnChange ;
24
+ import liquibase .change .core .AddForeignKeyConstraintChange ;
24
25
import liquibase .change .core .CreateTableChange ;
25
26
import liquibase .change .core .DropColumnChange ;
27
+ import liquibase .change .core .DropForeignKeyConstraintChange ;
26
28
import liquibase .change .core .DropTableChange ;
27
29
import liquibase .changelog .ChangeLogChild ;
28
30
import liquibase .changelog .ChangeLogParameters ;
52
54
import java .util .Set ;
53
55
import java .util .function .BiPredicate ;
54
56
import java .util .function .Predicate ;
57
+ import java .util .stream .Collectors ;
55
58
56
59
import org .springframework .core .io .Resource ;
57
60
import org .springframework .data .mapping .context .MappingContext ;
@@ -321,15 +324,15 @@ private ChangeSet createChangeSet(ChangeSetMetadata metadata, SchemaDiff differe
321
324
private SchemaDiff initial () {
322
325
323
326
Tables mappedEntities = Tables .from (mappingContext .getPersistentEntities ().stream ().filter (schemaFilter ),
324
- sqlTypeMapping , null );
327
+ sqlTypeMapping , null , mappingContext );
325
328
return SchemaDiff .diff (mappedEntities , Tables .empty (), nameComparator );
326
329
}
327
330
328
331
private SchemaDiff differenceOf (Database database ) throws LiquibaseException {
329
332
330
333
Tables existingTables = getLiquibaseModel (database );
331
334
Tables mappedEntities = Tables .from (mappingContext .getPersistentEntities ().stream ().filter (schemaFilter ),
332
- sqlTypeMapping , database .getDefaultCatalogName ());
335
+ sqlTypeMapping , database .getDefaultCatalogName (), mappingContext );
333
336
334
337
return SchemaDiff .diff (mappedEntities , existingTables , nameComparator );
335
338
}
@@ -362,6 +365,13 @@ private DatabaseChangeLog getDatabaseChangeLog(File changeLogFile, @Nullable Dat
362
365
363
366
private void generateTableAdditionsDeletions (ChangeSet changeSet , SchemaDiff difference ) {
364
367
368
+ for (Table table : difference .tableDeletions ()) {
369
+ for (ForeignKey foreignKey : table .foreignKeys ()) {
370
+ DropForeignKeyConstraintChange dropForeignKey = dropForeignKey (foreignKey );
371
+ changeSet .addChange (dropForeignKey );
372
+ }
373
+ }
374
+
365
375
for (Table table : difference .tableAdditions ()) {
366
376
CreateTableChange newTable = changeTable (table );
367
377
changeSet .addChange (newTable );
@@ -373,12 +383,24 @@ private void generateTableAdditionsDeletions(ChangeSet changeSet, SchemaDiff dif
373
383
changeSet .addChange (dropTable (table ));
374
384
}
375
385
}
386
+
387
+ for (Table table : difference .tableAdditions ()) {
388
+ for (ForeignKey foreignKey : table .foreignKeys ()) {
389
+ AddForeignKeyConstraintChange addForeignKey = addForeignKey (foreignKey );
390
+ changeSet .addChange (addForeignKey );
391
+ }
392
+ }
376
393
}
377
394
378
395
private void generateTableModifications (ChangeSet changeSet , SchemaDiff difference ) {
379
396
380
397
for (TableDiff table : difference .tableDiffs ()) {
381
398
399
+ for (ForeignKey foreignKey : table .fkToDrop ()) {
400
+ DropForeignKeyConstraintChange dropForeignKey = dropForeignKey (foreignKey );
401
+ changeSet .addChange (dropForeignKey );
402
+ }
403
+
382
404
if (!table .columnsToAdd ().isEmpty ()) {
383
405
changeSet .addChange (addColumns (table ));
384
406
}
@@ -388,6 +410,11 @@ private void generateTableModifications(ChangeSet changeSet, SchemaDiff differen
388
410
if (!deletedColumns .isEmpty ()) {
389
411
changeSet .addChange (dropColumns (table , deletedColumns ));
390
412
}
413
+
414
+ for (ForeignKey foreignKey : table .fkToAdd ()) {
415
+ AddForeignKeyConstraintChange addForeignKey = addForeignKey (foreignKey );
416
+ changeSet .addChange (addForeignKey );
417
+ }
391
418
}
392
419
}
393
420
@@ -444,12 +471,27 @@ private Tables getLiquibaseModel(Database targetDatabase) throws LiquibaseExcept
444
471
tableModel .columns ().add (columnModel );
445
472
}
446
473
474
+ tableModel .foreignKeys ().addAll (extractForeignKeys (table ));
475
+
447
476
existingTables .add (tableModel );
448
477
}
449
478
450
479
return new Tables (existingTables );
451
480
}
452
481
482
+ private static List <ForeignKey > extractForeignKeys (liquibase .structure .core .Table table ) {
483
+
484
+ return table .getOutgoingForeignKeys ().stream ().map (foreignKey -> {
485
+ String tableName = foreignKey .getForeignKeyTable ().getName ();
486
+ String columnName = foreignKey .getForeignKeyColumns ().stream ().findFirst ()
487
+ .map (liquibase .structure .core .Column ::getName ).get ();
488
+ String referencedTableName = foreignKey .getPrimaryKeyTable ().getName ();
489
+ String referencedColumnName = foreignKey .getPrimaryKeyColumns ().stream ().findFirst ()
490
+ .map (liquibase .structure .core .Column ::getName ).get ();
491
+ return new ForeignKey (foreignKey .getName (), tableName , columnName , referencedTableName , referencedColumnName );
492
+ }).collect (Collectors .toList ());
493
+ }
494
+
453
495
private static AddColumnChange addColumns (TableDiff table ) {
454
496
455
497
AddColumnChange addColumnChange = new AddColumnChange ();
@@ -532,6 +574,25 @@ private static DropTableChange dropTable(Table table) {
532
574
return change ;
533
575
}
534
576
577
+ private static AddForeignKeyConstraintChange addForeignKey (ForeignKey foreignKey ) {
578
+
579
+ AddForeignKeyConstraintChange change = new AddForeignKeyConstraintChange ();
580
+ change .setConstraintName (foreignKey .name ());
581
+ change .setBaseTableName (foreignKey .tableName ());
582
+ change .setBaseColumnNames (foreignKey .columnName ());
583
+ change .setReferencedTableName (foreignKey .referencedTableName ());
584
+ change .setReferencedColumnNames (foreignKey .referencedColumnName ());
585
+ return change ;
586
+ }
587
+
588
+ private static DropForeignKeyConstraintChange dropForeignKey (ForeignKey foreignKey ) {
589
+
590
+ DropForeignKeyConstraintChange change = new DropForeignKeyConstraintChange ();
591
+ change .setConstraintName (foreignKey .name ());
592
+ change .setBaseTableName (foreignKey .tableName ());
593
+ return change ;
594
+ }
595
+
535
596
/**
536
597
* Metadata for a ChangeSet.
537
598
*/
0 commit comments