Skip to content

Commit

Permalink
Merge pull request #2351 from liquibase/fix_for_GH_2231
Browse files Browse the repository at this point in the history
Fix for Github issue dealing with duplicate property keys
  • Loading branch information
nvoxland committed Feb 17, 2022
2 parents 3632cfa + 16c1ec5 commit e606925
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
Expand Up @@ -124,22 +124,31 @@ public void set(String key, String value, ContextExpression contexts, Labels lab
if (globalParam) {
// if it is global param remove duplicate non-global parameters
ChangeLogParameter param = findParameter(key, null);
if (param != null && ! param.isGlobal()) {
if (param != null && isDuplicate(param) && ! param.isGlobal()) {
changeLogParameters.remove(param);
}
// okay add it
changeLogParameters.add(new ChangeLogParameter(key, value, contexts, labels, databases, globalParam,
changeLog));
} else {
ChangeLogParameter param = findParameter(key, changeLog);
if (param != null && ! param.isGlobal() && param.getChangeLog() == changeLog) {
if (param != null && isDuplicate(param) && ! param.isGlobal() && param.getChangeLog() == changeLog) {
changeLogParameters.remove(param);
}
//this is a non-global param, just add it
changeLogParameters.add(new ChangeLogParameter(key, value, contexts, labels, databases, globalParam, changeLog));
}
}

private boolean isDuplicate(ChangeLogParameter param) {
for (ChangeLogParameter parameter : changeLogParameters) {
if (param != parameter && param.isDuplicate(parameter)) {
return true;
}
}
return false;
}

/**
* Return the value of a parameter
*
Expand Down Expand Up @@ -343,6 +352,19 @@ public boolean isValid() {
return isValid;
}

public boolean isDuplicate(ChangeLogParameter other) {
String contextString = (this.getValidContexts() != null ? this.getValidContexts().toString() : null);
String labelsString = (this.getLabels() != null ? this.getLabels().toString() : null);
String databases = (this.getValidDatabases() != null ? StringUtil.join(this.getValidDatabases(), ",") : null);

String otherContextString = (other.getValidContexts() != null ? other.getValidContexts().toString() : null);
String otherLabelsString = (other.getLabels() != null ? other.getLabels().toString() : null);
String otherDatabases = (other.getValidDatabases() != null ? StringUtil.join(other.getValidDatabases(), ",") : null);
return StringUtil.equalsIgnoreCaseAndEmpty(contextString, otherContextString) &&
StringUtil.equalsIgnoreCaseAndEmpty(labelsString, otherLabelsString) &&
StringUtil.equalsIgnoreCaseAndEmpty(databases, otherDatabases);
}

public boolean isGlobal() {
return global;
}
Expand Down
Expand Up @@ -4,13 +4,15 @@
import liquibase.Contexts;
import liquibase.Labels;
import liquibase.database.core.H2Database;
import liquibase.database.core.MySQLDatabase;
import liquibase.database.core.OracleDatabase;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;


public class ChangeLogParametersTest {
public class ChangeLogParametersTest {

@Test
public void setParameterValue_doubleSet() {
Expand All @@ -22,6 +24,28 @@ public void setParameterValue_doubleSet() {
assertEquals("re-setting a param should not overwrite the value (like how ant works)", "originalValue", changeLogParameters.getValue("doubleSet", null));
}

@Test
public void setParameterValue_doubleSet_withDbms() throws Exception {
final DatabaseChangeLog changelog = new DatabaseChangeLog("com/example/changelog.txt");

ChangeLogParameters h2Params = new ChangeLogParameters(new H2Database());
ChangeLogParameters oracleParams = new ChangeLogParameters(new OracleDatabase());
ChangeLogParameters mysqlParams = new ChangeLogParameters(new MySQLDatabase());

h2Params.set("dbmsProperty", "h2 value", new ContextExpression(), new Labels(), "h2", false, changelog);
h2Params.set("dbmsProperty", "oracle value", new ContextExpression(), new Labels(), "oracle", false, changelog);

oracleParams.set("dbmsProperty", "h2 value", new ContextExpression(), new Labels(), "h2", false, changelog);
oracleParams.set("dbmsProperty", "oracle value", new ContextExpression(), new Labels(), "oracle", false, changelog);

mysqlParams.set("dbmsProperty", "h2 value", new ContextExpression(), new Labels(), "h2", false, changelog);
mysqlParams.set("dbmsProperty", "oracle value", new ContextExpression(), new Labels(), "oracle", false, changelog);

assertEquals("h2 value", h2Params.getValue("dbmsProperty", changelog));
assertEquals("oracle value", oracleParams.getValue("dbmsProperty", changelog));
assertNull(mysqlParams.getValue("dbmsProperty", changelog));
}

@Test
public void getParameterValue_envVariable() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters();
Expand Down Expand Up @@ -64,7 +88,8 @@ public void setParameterValue_rightDBWrongContext() {

assertNull(changeLogParameters.getValue("doubleSet", null));
}
@Test

@Test
public void setParameterValue_rightDBRightContext() {
ChangeLogParameters changeLogParameters = new ChangeLogParameters(new H2Database());
changeLogParameters.setContexts(new Contexts("junit"));
Expand Down

0 comments on commit e606925

Please sign in to comment.