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

allow use of pro DatabaseObject's for filtering with generateChangeLog (DAT-9542) #3068

Merged
merged 1 commit into from Jul 20, 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
@@ -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