Skip to content

Commit

Permalink
fix SQLFileChange.generateCheckSum() to calculate checksum without pr…
Browse files Browse the repository at this point in the history
…operty substitution (liquibase#5389)
  • Loading branch information
jglass524 committed Dec 20, 2023
1 parent 672b35c commit c4748fe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,18 @@ public String getConfirmationMessage() {
@Override
@DatabaseChangeProperty(isChangeProperty = false)
public String getSql() {
return getSql(true);
}

public String getSql(boolean doExpandExpressions) {
String sql = super.getSql();
if (sql == null) {
try (InputStream sqlStream = openSqlStream()) {
if (sqlStream == null) {
return null;
}
String content = StreamUtil.readStreamAsString(sqlStream, getEncoding());
if (getChangeSet() != null) {
if (doExpandExpressions && getChangeSet() != null) {
ChangeLogParameters parameters = getChangeSet().getChangeLogParameters();
if (parameters != null) {
content = parameters.expandExpressions(content, getChangeSet().getChangeLog());
Expand Down Expand Up @@ -198,7 +202,7 @@ public CheckSum generateCheckSum() {
}
InputStream stream = null;
try {
String sqlContent = getSql();
String sqlContent = getSql(false);
Charset encoding = GlobalConfiguration.FILE_ENCODING.getCurrentValue();
stream = new ByteArrayInputStream(sqlContent.getBytes(encoding));
return CheckSum.compute(new AbstractSQLChange.NormalizingStream(stream), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,39 @@ class SQLFileChangeTest extends StandardChangeTest {
assertEquals("create prfx_customer (nofx INTEGER NOT NULL, PRIMARY KEY (nofx));", change.getSql());
}

def noReplacementOfPropertiesInChecksum() throws Exception {
when:
def changelog = new DatabaseChangeLog("com/example/changelog.xml")
def change1 = new SQLFileChange()
change1.path = "com/example-2/fileWithSchemaNameProperty.sql"
change1.relativeToChangelogFile = false
def changeLogParameters1 = new ChangeLogParameters()
changeLogParameters1.set("database.liquibaseSchemaName", "schema1")
def changeSet1 = new ChangeSet("x", "y", false, false, null, null, null, changelog)
changeSet1.setChangeLogParameters(changeLogParameters1)
change1.setChangeSet(changeSet1)

def sqlForSchema1 = change1.getSql().trim()
def checksum1 = change1.generateCheckSum()

def change2 = new SQLFileChange()
change2.path = "com/example-2/fileWithSchemaNameProperty.sql"
change2.relativeToChangelogFile = false
def changeLogParameters2 = new ChangeLogParameters()
changeLogParameters2.set("database.liquibaseSchemaName", "schema2")
def changeSet2 = new ChangeSet("x", "y", false, false, null, null, null, changelog)
changeSet2.setChangeLogParameters(changeLogParameters2)
change2.setChangeSet(changeSet2)

def sqlForSchema2 = change2.getSql().trim()
def checksum2 = change2.generateCheckSum()

then:
assertEquals("select * from schema1.customer;", sqlForSchema1)
assertEquals("select * from schema2.customer;", sqlForSchema2)
assertEquals(checksum1, checksum2)
}

def "checkStatus"() {
when:
def database = new MockDatabase()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from ${database.liquibaseSchemaName}.customer;

0 comments on commit c4748fe

Please sign in to comment.