Navigation Menu

Skip to content

Commit

Permalink
Skip change sets that don't match on DBMS during change log parsing
Browse files Browse the repository at this point in the history
Closes: CORE-3180
  • Loading branch information
mches committed Feb 27, 2018
1 parent bd6a62b commit a1ffc67
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
Expand Up @@ -227,13 +227,7 @@ public ChangeSet(String id, String author, boolean alwaysRun, boolean runOnChang
}

protected void setDbms(String dbmsList) {
if (StringUtils.trimToNull(dbmsList) != null) {
String[] strings = dbmsList.toLowerCase().split(",");
dbmsSet = new HashSet<String>();
for (String string : strings) {
dbmsSet.add(string.trim().toLowerCase());
}
}
this.dbmsSet = DatabaseList.toDbmsSet(dbmsList);
}

public String getFilePath() {
Expand Down
Expand Up @@ -9,6 +9,7 @@
import liquibase.changelog.filter.LabelChangeSetFilter;
import liquibase.changelog.visitor.ValidatingVisitor;
import liquibase.database.Database;
import liquibase.database.DatabaseList;
import liquibase.database.ObjectQuotingStrategy;
import liquibase.exception.LiquibaseException;
import liquibase.exception.SetupException;
Expand Down Expand Up @@ -172,11 +173,7 @@ public ChangeSet getChangeSet(String path, String author, String id) {
if (normalizePath(changeSet.getFilePath()).equalsIgnoreCase(normalizePath(path))
&& changeSet.getAuthor().equalsIgnoreCase(author)
&& changeSet.getId().equalsIgnoreCase(id)
&& (changeSet.getDbmsSet() == null
|| changeLogParameters == null
|| changeLogParameters.getValue("database.typeName", this) == null
|| changeSet.getDbmsSet().isEmpty()
|| changeSet.getDbmsSet().contains(changeLogParameters.getValue("database.typeName", this).toString()))) {
&& isDbmsMatch(changeSet.getDbmsSet())) {
return changeSet;
}
}
Expand Down Expand Up @@ -308,7 +305,9 @@ protected void handleChildNode(ParsedNode node, ResourceAccessor resourceAccesso
expandExpressions(node);
String nodeName = node.getName();
if (nodeName.equals("changeSet")) {
this.addChangeSet(createChangeSet(node, resourceAccessor));
if (isDbmsMatch(node.getChildValue(null, "dbms", String.class))) {
this.addChangeSet(createChangeSet(node, resourceAccessor));
}
} else if (nodeName.equals("include")) {
String path = node.getChildValue(null, "file", String.class);
if (path == null) {
Expand Down Expand Up @@ -399,6 +398,18 @@ protected void handleChildNode(ParsedNode node, ResourceAccessor resourceAccesso
}
}

public boolean isDbmsMatch(String dbmsList) {
return isDbmsMatch(DatabaseList.toDbmsSet(dbmsList));
}

public boolean isDbmsMatch(Set<String> dbmsSet) {
return dbmsSet == null
|| changeLogParameters == null
|| changeLogParameters.getValue("database.typeName", this) == null
|| dbmsSet.isEmpty()
|| dbmsSet.contains(changeLogParameters.getValue("database.typeName", this).toString());
}

public void includeAll(String pathName, boolean isRelativeToChangelogFile, IncludeAllFilter resourceFilter,
boolean errorIfMissingOrEmpty,
Comparator<String> resourceComparator, ResourceAccessor resourceAccessor, ContextExpression includeContexts) throws SetupException {
Expand Down
11 changes: 11 additions & 0 deletions liquibase-core/src/main/java/liquibase/database/DatabaseList.java
Expand Up @@ -70,4 +70,15 @@ public static boolean definitionMatches(Collection<String> definition, Database
}
return definitionMatches(definition, shortName, returnValueIfEmptyList);
}

public static Set<String> toDbmsSet(String dbmsList) {
Set<String> dbmsSet = null;
if (StringUtils.trimToNull(dbmsList) != null) {
dbmsSet = new HashSet<String>();
for (String string : dbmsList.toLowerCase().split(",")) {
dbmsSet.add(string.trim());
}
}
return dbmsSet;
}
}
Expand Up @@ -21,6 +21,8 @@ import liquibase.changelog.ChangeSet
import liquibase.changelog.DatabaseChangeLog
import liquibase.configuration.LiquibaseConfiguration
import liquibase.database.ObjectQuotingStrategy
import liquibase.database.core.H2Database
import liquibase.database.core.MSSQLDatabase
import liquibase.sdk.database.MockDatabase
import liquibase.exception.ChangeLogParseException
import liquibase.precondition.CustomPreconditionWrapper
Expand Down Expand Up @@ -693,4 +695,24 @@ public class XMLChangeLogSAXParser_RealFile_Test extends Specification {
cleanup:
ChangeFactory.getInstance().unregister("createTableExample")
}

def "change sets with matching dbms are parsed"() {
when:
def path = "liquibase/parser/core/xml/rollbackWithDbmsChangeLog.xml"
def database = new MSSQLDatabase()
def changeLog = new XMLChangeLogSAXParser().parse(path, new ChangeLogParameters(database), new JUnitResourceAccessor())

then:
changeLog.getChangeSets().size() == 4
}

def "change sets with non-matching dbms are skipped"() {
when:
def path = "liquibase/parser/core/xml/rollbackWithDbmsChangeLog.xml"
def database = new H2Database()
def changeLog = new XMLChangeLogSAXParser().parse(path, new ChangeLogParameters(database), new JUnitResourceAccessor())

then:
changeLog.getChangeSets().size() == 2
}
}
@@ -0,0 +1,28 @@
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">

<changeSet id="1" author="mches" dbms="mssql">
<createTable tableName="a">
<column name="b" type="int"/>
</createTable>
</changeSet>

<changeSet id="2" author="mches" dbms="mssql">
<dropTable tableName="a"/>
<rollback changeSetId="1" changeSetAuthor="mches"/>
</changeSet>

<changeSet id="3" author="mches">
<createTable tableName="c">
<column name="d" type="bigint"/>
</createTable>
</changeSet>

<changeSet id="4" author="mches">
<dropTable tableName="c"/>
<rollback changeSetId="3" changeSetAuthor="mches"/>
</changeSet>

</databaseChangeLog>

0 comments on commit a1ffc67

Please sign in to comment.