Skip to content

Commit

Permalink
Add CustomChangeChecksum interface (#5649)
Browse files Browse the repository at this point in the history
CustomChangeChecksum interface

Adds a new optional interface: CustomChangeChecksum, that you can implement on any CustomChange.

Will let you configure how the checksum for your specific change will be calculated, if you're not happy with the default.

Closes #5478
  • Loading branch information
robinjhector committed Mar 21, 2024
1 parent 6f50035 commit 14288cf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package liquibase.change.custom;

import liquibase.change.AbstractChange;
import liquibase.change.CheckSum;

/**
* Interface to implement that allows a custom change to generate its own checksum.
*
* @see liquibase.change.custom.CustomChange
* @see AbstractChange#generateCheckSum()
*/
public interface CustomChangeChecksum {

/**
* Generates a checksum for the current state of the change.
*
* @return the generated checksum
*/
CheckSum generateChecksum();

}
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,21 @@ public SqlStatement[] generateRollbackStatements(Database database) throws Rollb
statements = SqlStatement.EMPTY_SQL_STATEMENT;
}
return statements;

}

@Override
public CheckSum generateCheckSum() {
try {
configureCustomChange();
if (customChange instanceof CustomChangeChecksum) {
return ((CustomChangeChecksum) customChange).generateChecksum();
} else {
return super.generateCheckSum();
}
} catch (CustomChangeException e) {
throw new UnexpectedLiquibaseException(e);
}
}

/**
* Returns true if the customChange supports rolling back.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package liquibase.change.custom

import liquibase.change.CheckSum
import liquibase.database.Database
import liquibase.exception.CustomChangeException
import liquibase.exception.RollbackImpossibleException
Expand Down Expand Up @@ -323,4 +324,21 @@ class CustomChangeWrapperTest extends Specification {
change.getParamValue("columnName") == "my_col"
change.getParamValue("unusedParam") == null
}

def "custom checksum is used"() {
when:
def change1 = new CustomChangeWrapper()
change1.setClass(ExampleCustomSqlChangeWithChecksum.class.getName())
change1.setParam("tableName", "my_table")
def change2 = new CustomChangeWrapper()
change2.setClass(ExampleCustomSqlChangeWithChecksum.class.getName())
change2.setParam("tableName", "my_other_table_name")
def change3 = new CustomChangeWrapper()
change3.setClass(ExampleCustomSqlChange.class.getName())
change3.setParam("tableName", "my_table")

then: "The checksum not affected by parameters set"
change1.generateCheckSum() == change2.generateCheckSum()
change1.generateCheckSum() != change3.generateCheckSum()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package liquibase.change.custom;

import liquibase.change.CheckSum;

public class ExampleCustomSqlChangeWithChecksum extends ExampleCustomSqlChange implements CustomChangeChecksum {

// Some synthetic field to be used in our checksum calculation
private final Integer version = 5;

/**
* Generate a checksum based on the classname and the version number within.
* Does not care about any parameters set
* @return the calculated checksum
*/
@Override
public CheckSum generateChecksum() {
return CheckSum.compute(getClass().getName() + ":" + version);
}
}

0 comments on commit 14288cf

Please sign in to comment.