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

Add CustomChangeChecksum interface #5649

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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();

}
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
@@ -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()
}
}
@@ -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);
}
}