Skip to content

Commit

Permalink
include pro DatabaseObject's in the permitted list for filtering with…
Browse files Browse the repository at this point in the history
… includeObjects/excludeObjects in generateChangeLog
  • Loading branch information
StevenMassaro committed Jul 15, 2022
1 parent 83faa21 commit d653f84
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
@@ -1,14 +1,17 @@
package liquibase.diff.output;

import liquibase.Scope;
import liquibase.database.Database;
import liquibase.diff.ObjectDifferences;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.servicelocator.ServiceLocator;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.*;
import liquibase.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

/**
Expand All @@ -24,13 +27,18 @@
*/
public class StandardObjectChangeFilter implements ObjectChangeFilter {

private FilterType filterType;
private final FilterType filterType;

private List<Filter> filters = new ArrayList<>();
private final List<Filter> filters = new ArrayList<>();
private final static List<DatabaseObject> databaseObjects = new ArrayList<>();
private boolean catalogOrSchemaFilter;

public StandardObjectChangeFilter(FilterType type, String filter) {
this.filterType = type;
if (databaseObjects.isEmpty()) {
ServiceLocator serviceLocator = Scope.getCurrentScope().getServiceLocator();
databaseObjects.addAll(serviceLocator.findInstances(DatabaseObject.class));
}
parseFilter(filter);
}

Expand All @@ -47,15 +55,14 @@ protected void parseFilter(String filter) {
if (split.length == 1) {
filters.add(new Filter(null, Pattern.compile(split[0])));
} else {
String className = StringUtil.upperCaseFirst(split[0]);
className = "liquibase.structure.core."+className;
try {
Class<DatabaseObject> clazz = (Class<DatabaseObject>) Class.forName(className);
filters.add(new Filter(clazz, Pattern.compile(split[1])));
catalogOrSchemaFilter |= "Catalog".equals(className) || "Schema".equals(className);
} catch (ClassNotFoundException e) {
throw new UnexpectedLiquibaseException(e);
String className = split[0];
Optional<DatabaseObject> databaseObject = databaseObjects.stream().filter(instance -> instance.getClass().getSimpleName().equalsIgnoreCase(className)).findAny();
if (databaseObject.isPresent()) {
filters.add(new Filter((Class<DatabaseObject>) databaseObject.get().getClass(), Pattern.compile(split[1])));
} else {
throw new UnexpectedLiquibaseException(className + " not found");
}
catalogOrSchemaFilter |= "Catalog".equals(className) || "Schema".equals(className);
}
}
}
Expand Down
Expand Up @@ -89,6 +89,51 @@ Optional Args:
]
}

run "Filtering with includeObjects", {
arguments = [
url : { it.url },
username: { it.username },
password: { it.password },
changelogFile: "target/test-classes/changelog-test.xml",
includeObjects: "table:FIRSTTABLE"
]
setup {
cleanResources(SetupCleanResources.CleanupMode.CLEAN_ON_SETUP, "changelog-test.xml")
database = [
new CreateTableChange(
tableName: "FirstTable",
columns: [
ColumnConfig.fromName("FirstColumn")
.setType("VARCHAR(255)")
]
),
new CreateTableChange(
tableName: "SecondTable",
columns: [
ColumnConfig.fromName("SecondColumn")
.setType("VARCHAR(255)")
]
),
new TagDatabaseChange(
tag: "version_2.0"
),
new CreateTableChange(
tableName: "liquibaseRunInfo",
columns: [
ColumnConfig.fromName("timesRan")
.setType("INT")
]
),
]
}
expectedFileContent = [
"target/test-classes/changelog-test.xml" : [CommandTests.assertContains("<changeSet ", 1)]
]
expectedResults = [
statusCode : 0
]
}

run "Run without changelogFile throws exception", {
arguments = [
changelogFile: ""
Expand Down

0 comments on commit d653f84

Please sign in to comment.