From 5837373f83270780190bd6c1fab00ca1f85888b6 Mon Sep 17 00:00:00 2001 From: filipe Date: Tue, 17 Jan 2023 18:17:55 -0300 Subject: [PATCH 1/7] Allows liquibase default column handle to be executed. From 8c779f2b13fa89deb20dcece0cd82bff3a530c49 Mon Sep 17 00:00:00 2001 From: filipe Date: Tue, 17 Jan 2023 18:12:08 -0300 Subject: [PATCH 2/7] Restore previous behavior and handle diff when the size/length of a field changes. --- .../diff/ChangedColumnChangeGenerator.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java diff --git a/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java b/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java new file mode 100644 index 00000000..83280233 --- /dev/null +++ b/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java @@ -0,0 +1,73 @@ +package liquibase.ext.hibernate.diff; + +import liquibase.change.Change; +import liquibase.database.Database; +import liquibase.diff.Difference; +import liquibase.diff.ObjectDifferences; +import liquibase.diff.output.DiffOutputControl; +import liquibase.ext.hibernate.database.HibernateDatabase; +import liquibase.statement.DatabaseFunction; +import liquibase.structure.DatabaseObject; +import liquibase.structure.core.Column; +import liquibase.structure.core.DataType; + +import java.util.List; + +/** + * Hibernate and database types tend to look different even though they are not. + * There are enough false positives that it works much better to suppress all column changes based on types. + */ +public class ChangedColumnChangeGenerator extends liquibase.diff.output.changelog.core.ChangedColumnChangeGenerator { + + @Override + public int getPriority(Class objectType, Database database) { + if (Column.class.isAssignableFrom(objectType)) { + return PRIORITY_ADDITIONAL; + } + return PRIORITY_NONE; + } + + @Override + protected void handleTypeDifferences(Column column, ObjectDifferences differences, DiffOutputControl control, List changes, Database referenceDatabase, Database comparisonDatabase) { + if (referenceDatabase instanceof HibernateDatabase || comparisonDatabase instanceof HibernateDatabase) { + handleSizeChange(column, differences, control, changes, referenceDatabase, comparisonDatabase); + } else { + super.handleTypeDifferences(column, differences, control, changes, referenceDatabase, comparisonDatabase); + } + } + + private void handleSizeChange(Column column, ObjectDifferences differences, DiffOutputControl control, List changes, Database referenceDatabase, Database comparisonDatabase) { + Difference difference = differences.getDifference("type"); + if (difference != null) { + for (Difference d : differences.getDifferences()) { + if (!(d.getReferenceValue() instanceof DataType)) { + differences.removeDifference(d.getField()); + continue; + } + int originalSize = ((DataType) d.getReferenceValue()).getColumnSize(); + int newSize = ((DataType) d.getComparedValue()).getColumnSize(); + if (newSize == originalSize) { + differences.removeDifference(d.getField()); + } + } + super.handleTypeDifferences(column, differences, control, changes, referenceDatabase, comparisonDatabase); + } + } + + @Override + protected void handleDefaultValueDifferences(Column column, ObjectDifferences differences, DiffOutputControl control, List changes, Database referenceDatabase, Database comparisonDatabase) { + if (referenceDatabase instanceof HibernateDatabase || comparisonDatabase instanceof HibernateDatabase) { + Difference difference = differences.getDifference("defaultValue"); + if (difference != null && difference.getReferenceValue() == null && difference.getComparedValue() instanceof DatabaseFunction) { + //database sometimes adds a function default value, like for timestamp columns + return; + } + difference = differences.getDifference("defaultValue"); + if (difference != null) { + super.handleDefaultValueDifferences(column, differences, control, changes, referenceDatabase, comparisonDatabase); + } + // do nothing, types tend to not match with hibernate + } + super.handleDefaultValueDifferences(column, differences, control, changes, referenceDatabase, comparisonDatabase); + } +} From 7c75120fc9fafd119f24f45083df81dd8a93ea02 Mon Sep 17 00:00:00 2001 From: filipe Date: Mon, 13 Mar 2023 23:20:04 -0300 Subject: [PATCH 3/7] Register class. --- .../services/liquibase.diff.output.changelog.ChangeGenerator | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/resources/META-INF/services/liquibase.diff.output.changelog.ChangeGenerator b/src/main/resources/META-INF/services/liquibase.diff.output.changelog.ChangeGenerator index b5d2eb3d..28466d96 100644 --- a/src/main/resources/META-INF/services/liquibase.diff.output.changelog.ChangeGenerator +++ b/src/main/resources/META-INF/services/liquibase.diff.output.changelog.ChangeGenerator @@ -1,3 +1,4 @@ +liquibase.ext.hibernate.diff.ChangedColumnChangeGenerator liquibase.ext.hibernate.diff.ChangedForeignKeyChangeGenerator liquibase.ext.hibernate.diff.ChangedSequenceChangeGenerator liquibase.ext.hibernate.diff.MissingSequenceChangeGenerator From aa6077e19e6c27366388358280125df8f3a4b3e2 Mon Sep 17 00:00:00 2001 From: filipe Date: Tue, 14 Mar 2023 16:37:59 -0300 Subject: [PATCH 4/7] fix nullpointer. --- .../ext/hibernate/diff/ChangedColumnChangeGenerator.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java b/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java index 83280233..5e0afea9 100644 --- a/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java +++ b/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java @@ -44,9 +44,10 @@ private void handleSizeChange(Column column, ObjectDifferences differences, Diff differences.removeDifference(d.getField()); continue; } - int originalSize = ((DataType) d.getReferenceValue()).getColumnSize(); - int newSize = ((DataType) d.getComparedValue()).getColumnSize(); - if (newSize == originalSize) { + Integer originalSize = ((DataType) d.getReferenceValue()).getColumnSize(); + Integer newSize = ((DataType) d.getComparedValue()).getColumnSize(); + if ((newSize == null && originalSize == null) || + (newSize != null && newSize.equals(originalSize))) { differences.removeDifference(d.getField()); } } From cf4437c54e5836883a56ebbb33beef1887fdfe8f Mon Sep 17 00:00:00 2001 From: filipe Date: Fri, 31 Mar 2023 00:02:14 -0300 Subject: [PATCH 5/7] Allows ignoring specific data types. --- .../ext/hibernate/diff/ChangedColumnChangeGenerator.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java b/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java index 5e0afea9..051dc1a8 100644 --- a/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java +++ b/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java @@ -19,6 +19,8 @@ */ public class ChangedColumnChangeGenerator extends liquibase.diff.output.changelog.core.ChangedColumnChangeGenerator { + private static final List TYPES_TO_IGNORE_SIZE = List.of("TIMESTAMP"); + @Override public int getPriority(Class objectType, Database database) { if (Column.class.isAssignableFrom(objectType)) { @@ -37,6 +39,9 @@ protected void handleTypeDifferences(Column column, ObjectDifferences difference } private void handleSizeChange(Column column, ObjectDifferences differences, DiffOutputControl control, List changes, Database referenceDatabase, Database comparisonDatabase) { + if (TYPES_TO_IGNORE_SIZE.stream().anyMatch(s -> s.equalsIgnoreCase(column.getType().getTypeName()))) { + return; + } Difference difference = differences.getDifference("type"); if (difference != null) { for (Difference d : differences.getDifferences()) { From eaf8ca2b311307421400e96b52e9170e8a035746 Mon Sep 17 00:00:00 2001 From: filipe Date: Fri, 31 Mar 2023 00:09:44 -0300 Subject: [PATCH 6/7] Allows ignoring specific data types. --- .../ext/hibernate/diff/ChangedColumnChangeGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java b/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java index 051dc1a8..38960e0e 100644 --- a/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java +++ b/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java @@ -15,7 +15,7 @@ /** * Hibernate and database types tend to look different even though they are not. - * There are enough false positives that it works much better to suppress all column changes based on types. + * The only change that we are handling it size change, and even for this one there are exceptions. */ public class ChangedColumnChangeGenerator extends liquibase.diff.output.changelog.core.ChangedColumnChangeGenerator { From c4c391c5afe78156f2c5a567dac2e99dfcaac6ea Mon Sep 17 00:00:00 2001 From: filipe Date: Fri, 31 Mar 2023 13:53:32 -0300 Subject: [PATCH 7/7] Fix nullpointers ignoring types having null size. --- .../ext/hibernate/diff/ChangedColumnChangeGenerator.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java b/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java index 38960e0e..dffc5f3c 100644 --- a/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java +++ b/src/main/java/liquibase/ext/hibernate/diff/ChangedColumnChangeGenerator.java @@ -51,8 +51,7 @@ private void handleSizeChange(Column column, ObjectDifferences differences, Diff } Integer originalSize = ((DataType) d.getReferenceValue()).getColumnSize(); Integer newSize = ((DataType) d.getComparedValue()).getColumnSize(); - if ((newSize == null && originalSize == null) || - (newSize != null && newSize.equals(originalSize))) { + if (newSize == null || originalSize == null || newSize.equals(originalSize)) { differences.removeDifference(d.getField()); } }