All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
liquibase.diff.compare.CompareControl Maven / Gradle / Ivy
package liquibase.diff.compare;
import liquibase.CatalogAndSchema;
import liquibase.database.Database;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.DatabaseObjectFactory;
import liquibase.util.StringUtils;
import java.util.*;
public class CompareControl {
public static CompareControl STANDARD = new CompareControl();
private CompareControl.SchemaComparison[] schemaComparisons;
private Set> compareTypes = new HashSet<>();
private Map, Set> suppressedFields = new HashMap<>();
public CompareControl() {
this(null);
}
public CompareControl(Set> compareTypes) {
schemaComparisons = new SchemaComparison[]{new SchemaComparison(new CatalogAndSchema(null, null), new CatalogAndSchema(null, null))};
setTypes(compareTypes);
}
public CompareControl(SchemaComparison[] schemaComparison, Set> compareTypes) {
this.schemaComparisons = schemaComparison;
setTypes(compareTypes);
}
public CompareControl(SchemaComparison[] schemaComparison, String compareTypes) {
if ((schemaComparison != null) && (schemaComparison.length > 0)) {
this.schemaComparisons = schemaComparison;
} else {
this.schemaComparisons = new SchemaComparison[]{new SchemaComparison(new CatalogAndSchema(null, null), new CatalogAndSchema(null, null))};
}
setTypes(DatabaseObjectFactory.getInstance().parseTypes(compareTypes));
}
public CompareControl(String[] referenceVsComparisonSchemas, Set> compareTypes) {
String[] splitReferenceSchemas = referenceVsComparisonSchemas[0].split(",");
String[] splitComparisonSchemas = referenceVsComparisonSchemas[1].split(",");
this.schemaComparisons = new SchemaComparison[splitReferenceSchemas.length];
for (int i = 0; i < splitReferenceSchemas.length; i++) {
String referenceCatalogName = null;
String referenceSchemaName = splitReferenceSchemas[i];
if (referenceSchemaName.contains(".")) {
referenceCatalogName = referenceSchemaName.split(".", 2)[0];
referenceSchemaName = referenceSchemaName.split(".", 2)[1];
}
String comparisonCatalogName = null;
String comparisonSchemaName = splitComparisonSchemas[i];
if (comparisonSchemaName.contains(".")) {
comparisonCatalogName = comparisonSchemaName.split(".", 2)[0];
comparisonSchemaName = comparisonSchemaName.split(".", 2)[1];
}
CatalogAndSchema referenceSchema = new CatalogAndSchema(referenceCatalogName, referenceSchemaName);
CatalogAndSchema comparisonSchema = new CatalogAndSchema(comparisonCatalogName, comparisonSchemaName);
this.schemaComparisons[i] = new SchemaComparison(referenceSchema, comparisonSchema);
setTypes(compareTypes);
}
}
public static ComputedSchemas computeSchemas(String schemaNames, String referenceSchemaNames, String
outputSchemaNames, String defaultCatalogName, String defaultSchemaName, String referenceDefaultCatalogName,
String referenceDefaultSchemaName, Database database) {
//Make sure either both schemaNames and referenceSchemaNames are set or both are null. If only one is set,
// make them equal
if ((schemaNames == null) && (referenceSchemaNames == null)) {
//they will be set to the defaults
} else if ((schemaNames == null) && (referenceSchemaNames != null)) {
schemaNames = referenceSchemaNames;
} else if ((schemaNames != null) && (referenceSchemaNames == null)) {
referenceSchemaNames = schemaNames;
}
if ((schemaNames == null) && (outputSchemaNames != null)) {
if (defaultSchemaName == null) {
schemaNames = database.getDefaultSchemaName();
} else {
schemaNames = defaultSchemaName;
}
referenceSchemaNames = schemaNames;
}
ComputedSchemas returnObj = new ComputedSchemas();
if (referenceSchemaNames == null) {
returnObj.finalSchemaComparisons = new CompareControl.SchemaComparison[]{new CompareControl
.SchemaComparison(
new CatalogAndSchema(referenceDefaultCatalogName, referenceDefaultSchemaName),
new CatalogAndSchema(defaultCatalogName, defaultSchemaName)
)};
returnObj.finalTargetSchemas = new CatalogAndSchema[]{new CatalogAndSchema(defaultCatalogName,
defaultSchemaName)};
} else {
List schemaComparisons = new ArrayList<>();
List referenceSchemas = new ArrayList<>();
List targetSchemas = new ArrayList<>();
List splitReferenceSchemaNames = StringUtils.splitAndTrim(referenceSchemaNames, ",");
List splitSchemaNames = StringUtils.splitAndTrim(schemaNames, ",");
List splitOutputSchemaNames = StringUtils.splitAndTrim(StringUtils.trimToNull(outputSchemaNames),
",");
if (splitReferenceSchemaNames.size() != splitSchemaNames.size()) {
throw new UnexpectedLiquibaseException("You must specify the same number of schemas in --schemas and " +
"--referenceSchemas");
}
if ((splitOutputSchemaNames != null) && (splitOutputSchemaNames.size() != splitSchemaNames.size())) {
throw new UnexpectedLiquibaseException("You must specify the same number of schemas in --schemas and " +
"--outputSchemasAs");
}
for (int i = 0; i < splitReferenceSchemaNames.size(); i++) {
String referenceSchema = splitReferenceSchemaNames.get(i);
String targetSchema = splitSchemaNames.get(i);
String outputSchema = null;
if (splitOutputSchemaNames != null) {
outputSchema = splitOutputSchemaNames.get(i);
}
CatalogAndSchema correctedTargetSchema = new CatalogAndSchema(null, targetSchema).customize(database);
CatalogAndSchema correctedReferenceSchema = new CatalogAndSchema(null, referenceSchema).customize
(database);
SchemaComparison comparison = new SchemaComparison(correctedReferenceSchema, correctedTargetSchema);
comparison.setOutputSchemaAs(outputSchema);
schemaComparisons.add(comparison);
referenceSchemas.add(correctedReferenceSchema);
targetSchemas.add(correctedTargetSchema);
}
returnObj.finalSchemaComparisons = schemaComparisons.toArray(new CompareControl
.SchemaComparison[schemaComparisons.size()]);
returnObj.finalTargetSchemas = targetSchemas.toArray(new CatalogAndSchema[targetSchemas.size()]);
}
return returnObj;
}
protected void setTypes(Set> types) {
if ((types == null) || types.isEmpty()) {
types = DatabaseObjectFactory.getInstance().getStandardTypes();
}
this.compareTypes = types;
}
public Set> getComparedTypes() {
return compareTypes;
}
public CompareControl addSuppressedField(Class extends DatabaseObject> type, String field) {
if (!suppressedFields.containsKey(type)) {
suppressedFields.put(type, new HashSet());
}
suppressedFields.get(type).add(field);
return this;
}
public boolean isSuppressedField(Class extends DatabaseObject> type, String field) {
if (!suppressedFields.containsKey(type)) {
return false;
}
return suppressedFields.get(type).contains(field);
}
public SchemaComparison[] getSchemaComparisons() {
return schemaComparisons;
}
public CatalogAndSchema[] getSchemas(DatabaseRole databaseRole) {
CatalogAndSchema[] schemas = new CatalogAndSchema[schemaComparisons.length];
for (int i=0; i