Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix diff changelog generating undesired changes #679

Merged
merged 10 commits into from
Apr 24, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package liquibase.ext.hibernate.diff;

import liquibase.change.Change;
import liquibase.database.Database;
import liquibase.diff.ObjectDifferences;
import liquibase.diff.output.DiffOutputControl;
import liquibase.diff.output.changelog.ChangeGeneratorChain;
import liquibase.ext.hibernate.database.HibernateDatabase;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.PrimaryKey;

/**
* Hibernate doesn't know about all the variations that occur with primary keys, especially backing index stuff.
* To prevent changing customized primary keys, we suppress this kind of changes from hibernate side.
*/
public class ChangedPrimaryKeyChangeGenerator extends liquibase.diff.output.changelog.core.ChangedPrimaryKeyChangeGenerator {
Dismissed Show dismissed Hide dismissed

@Override
public int getPriority(Class<? extends DatabaseObject> objectType, Database database) {
if (PrimaryKey.class.isAssignableFrom(objectType)) {
return PRIORITY_ADDITIONAL;
}
return PRIORITY_NONE;
}

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
if (referenceDatabase instanceof HibernateDatabase || comparisonDatabase instanceof HibernateDatabase) {
differences.removeDifference("unique");
if (!differences.hasDifferences()) {
return null;
}
}

return super.fixChanged(changedObject, differences, control, referenceDatabase, comparisonDatabase, chain);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package liquibase.ext.hibernate.diff;

import liquibase.change.Change;
import liquibase.database.Database;
import liquibase.diff.ObjectDifferences;
import liquibase.diff.output.DiffOutputControl;
import liquibase.diff.output.changelog.ChangeGeneratorChain;
import liquibase.ext.hibernate.database.HibernateDatabase;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.UniqueConstraint;

/**
* Unique attribute for unique constraints backing index can have different values dependending on the database implementation,
* so we suppress all unique constraint changes based on unique constraints.

*/
public class ChangedUniqueConstraintChangeGenerator extends liquibase.diff.output.changelog.core.ChangedUniqueConstraintChangeGenerator {
filipelautert marked this conversation as resolved.
Dismissed
Show resolved Hide resolved

@Override
public int getPriority(Class<? extends DatabaseObject> objectType, Database database) {
if (UniqueConstraint.class.isAssignableFrom(objectType)) {
return PRIORITY_ADDITIONAL;
}
return PRIORITY_NONE;
}

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
if (referenceDatabase instanceof HibernateDatabase || comparisonDatabase instanceof HibernateDatabase) {
differences.removeDifference("unique");
if (!differences.hasDifferences()) {
return null;
}
}
return super.fixChanged(changedObject, differences, control, referenceDatabase, comparisonDatabase, chain);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;

public class UniqueConstraintSnapshotGenerator extends HibernateSnapshotGenerator {

Expand Down Expand Up @@ -54,7 +53,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
Index index = getBackingIndex(uniqueConstraint, hibernateTable, snapshot);
uniqueConstraint.setBackingIndex(index);

Scope.getCurrentScope().getLog(getClass()).info("Found unique constraint " + uniqueConstraint.toString());
Scope.getCurrentScope().getLog(getClass()).info("Found unique constraint " + uniqueConstraint);
table.getUniqueConstraints().add(uniqueConstraint);
}
for (var column : hibernateTable.getColumns()) {
Expand All @@ -68,7 +67,7 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
}
uniqueConstraint.addColumn(0, new Column(column.getName()).setRelation(table));
uniqueConstraint.setName(name);
Scope.getCurrentScope().getLog(getClass()).info("Found unique constraint " + uniqueConstraint.toString());
Scope.getCurrentScope().getLog(getClass()).info("Found unique constraint " + uniqueConstraint);
table.getUniqueConstraints().add(uniqueConstraint);

Index index = getBackingIndex(uniqueConstraint, hibernateTable, snapshot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
liquibase.ext.hibernate.diff.ChangedColumnChangeGenerator
liquibase.ext.hibernate.diff.ChangedForeignKeyChangeGenerator
liquibase.ext.hibernate.diff.ChangedPrimaryKeyChangeGenerator
liquibase.ext.hibernate.diff.ChangedSequenceChangeGenerator
liquibase.ext.hibernate.diff.ChangedUniqueConstraintChangeGenerator
liquibase.ext.hibernate.diff.MissingSequenceChangeGenerator
liquibase.ext.hibernate.diff.UnexpectedIndexChangeGenerator
Loading