Skip to content

Commit

Permalink
fix: primary key backing index issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
filipelautert committed Apr 19, 2024
1 parent b493b5d commit 822ee04
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
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 {

Check notice

Code scanning / CodeQL

Class has same name as super class Note

ChangedPrimaryKeyChangeGenerator has the same name as its supertype
liquibase.diff.output.changelog.core.ChangedPrimaryKeyChangeGenerator
.

@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
Expand Up @@ -9,6 +9,11 @@
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 {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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
Expand Down

0 comments on commit 822ee04

Please sign in to comment.