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

Fix for Github issue dealing with duplicate property keys #2351

Merged
merged 4 commits into from Feb 17, 2022
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test! Thanks for adding it!

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