use of liquibase.structure.core.Column in project liquibase by liquibase.
the class DiffToChangeLog method sortObjects.
private List<DatabaseObject> sortObjects(final String type, Collection<DatabaseObject> objects, Database database) {
if (!objects.isEmpty() && supportsSortingObjects(database) && (database.getConnection() != null) && !(database.getConnection() instanceof OfflineConnection)) {
List<String> schemas = new ArrayList<>();
CompareControl.SchemaComparison[] schemaComparisons = this.diffOutputControl.getSchemaComparisons();
if (schemaComparisons != null) {
for (CompareControl.SchemaComparison comparison : schemaComparisons) {
String schemaName = comparison.getReferenceSchema().getSchemaName();
if (schemaName == null) {
schemaName = database.getDefaultSchemaName();
}
schemas.add(schemaName);
}
}
if (schemas.isEmpty()) {
schemas.add(database.getDefaultSchemaName());
}
try {
final List<String> dependencyOrder = new ArrayList<>();
DependencyUtil.NodeValueListener<String> nameListener = new DependencyUtil.NodeValueListener<String>() {
@Override
public void evaluating(String nodeValue) {
dependencyOrder.add(nodeValue);
}
};
DependencyUtil.DependencyGraph<String> graph = new DependencyUtil.DependencyGraph<String>(nameListener);
addDependencies(graph, schemas, database);
graph.computeDependencies();
if (!dependencyOrder.isEmpty()) {
final List<DatabaseObject> toSort = new ArrayList<>();
final List<DatabaseObject> toNotSort = new ArrayList<>();
for (DatabaseObject obj : objects) {
if (!(obj instanceof Column)) {
String schemaName = null;
if (obj.getSchema() != null) {
schemaName = obj.getSchema().getName();
}
String name = schemaName + "." + obj.getName();
if (dependencyOrder.contains(name)) {
toSort.add(obj);
} else {
toNotSort.add(obj);
}
} else {
toNotSort.add(obj);
}
}
Collections.sort(toSort, new Comparator<DatabaseObject>() {
@Override
public int compare(DatabaseObject o1, DatabaseObject o2) {
String o1Schema = null;
if (o1.getSchema() != null) {
o1Schema = o1.getSchema().getName();
}
String o2Schema = null;
if (o2.getSchema() != null) {
o2Schema = o2.getSchema().getName();
}
Integer o1Order = dependencyOrder.indexOf(o1Schema + "." + o1.getName());
int o2Order = dependencyOrder.indexOf(o2Schema + "." + o2.getName());
int order = o1Order.compareTo(o2Order);
if ("unexpected".equals(type)) {
order = order * -1;
}
return order;
}
});
toSort.addAll(toNotSort);
return toSort;
}
} catch (DatabaseException e) {
Scope.getCurrentScope().getLog(getClass()).fine("Cannot get object dependencies: " + e.getMessage());
}
}
return new ArrayList<>(objects);
}
use of liquibase.structure.core.Column in project liquibase by liquibase.
the class ChangedForeignKeyChangeGenerator method fixChanged.
@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
ForeignKey fk = (ForeignKey) changedObject;
StringUtil.StringUtilFormatter formatter = new StringUtil.StringUtilFormatter<Column>() {
@Override
public String toString(Column obj) {
return obj.toString(false);
}
};
DropForeignKeyConstraintChange dropFkChange = new DropForeignKeyConstraintChange();
dropFkChange.setConstraintName(fk.getName());
dropFkChange.setBaseTableName(fk.getForeignKeyTable().getName());
AddForeignKeyConstraintChange addFkChange = new AddForeignKeyConstraintChange();
addFkChange.setConstraintName(fk.getName());
addFkChange.setBaseTableName(fk.getForeignKeyTable().getName());
addFkChange.setBaseColumnNames(StringUtil.join(fk.getForeignKeyColumns(), ",", formatter));
addFkChange.setReferencedTableName(fk.getPrimaryKeyTable().getName());
addFkChange.setReferencedColumnNames(StringUtil.join(fk.getPrimaryKeyColumns(), ",", formatter));
addFkChange.setOnDelete(fk.getDeleteRule());
addFkChange.setOnUpdate(fk.getUpdateRule());
if (control.getIncludeCatalog()) {
dropFkChange.setBaseTableCatalogName(fk.getForeignKeyTable().getSchema().getCatalogName());
addFkChange.setBaseTableCatalogName(fk.getForeignKeyTable().getSchema().getCatalogName());
addFkChange.setReferencedTableCatalogName(fk.getPrimaryKeyTable().getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
dropFkChange.setBaseTableSchemaName(fk.getForeignKeyTable().getSchema().getName());
addFkChange.setBaseTableSchemaName(fk.getForeignKeyTable().getSchema().getName());
addFkChange.setReferencedTableSchemaName(fk.getPrimaryKeyTable().getSchema().getName());
}
if (fk.getBackingIndex() != null) {
control.setAlreadyHandledChanged(fk.getBackingIndex());
}
return new Change[] { dropFkChange, addFkChange };
}
use of liquibase.structure.core.Column in project liquibase by liquibase.
the class ChangedViewChangeGenerator method fixChanged.
@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
View view = (View) changedObject;
CreateViewChange change = createViewChange();
change.setViewName(view.getName());
change.setReplaceIfExists(true);
if (control.getIncludeCatalog()) {
change.setCatalogName(view.getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(view.getSchema().getName());
}
String selectQuery = view.getDefinition();
boolean fullDefinitionOverridden = false;
if (selectQuery == null) {
selectQuery = "COULD NOT DETERMINE VIEW QUERY";
} else if ((comparisonDatabase instanceof OracleDatabase) && (view.getColumns() != null) && !view.getColumns().isEmpty()) {
String viewName;
if ((change.getCatalogName() == null) && (change.getSchemaName() == null)) {
viewName = comparisonDatabase.escapeObjectName(change.getViewName(), View.class);
} else {
viewName = comparisonDatabase.escapeViewName(change.getCatalogName(), change.getSchemaName(), change.getViewName());
}
selectQuery = "CREATE OR REPLACE FORCE VIEW " + viewName + " (" + StringUtil.join(view.getColumns(), ", ", new StringUtil.StringUtilFormatter() {
@Override
public String toString(Object obj) {
if ((((Column) obj).getComputed() != null) && ((Column) obj).getComputed()) {
return ((Column) obj).getName();
} else {
return comparisonDatabase.escapeColumnName(null, null, null, ((Column) obj).getName(), false);
}
}
}) + ") AS " + selectQuery;
change.setFullDefinition(true);
fullDefinitionOverridden = true;
}
change.setSelectQuery(selectQuery);
if (!fullDefinitionOverridden) {
change.setFullDefinition(view.getContainsFullDefinition());
}
List<Change> changes = new ArrayList<>();
changes.add(change);
Difference changedRemarks = differences.getDifference("remarks");
if (changedRemarks != null) {
SetTableRemarksChange setRemarksChange = new SetTableRemarksChange();
if (control.getIncludeCatalog()) {
setRemarksChange.setCatalogName(view.getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
setRemarksChange.setSchemaName(view.getSchema().getName());
}
setRemarksChange.setTableName(view.getName());
setRemarksChange.setRemarks(view.getRemarks());
changes.add(setRemarksChange);
}
return changes.toArray(new Change[changes.size()]);
}
use of liquibase.structure.core.Column in project liquibase by liquibase.
the class MissingTableChangeGenerator method getSingleColumnUniqueConstraints.
private Map<Column, UniqueConstraint> getSingleColumnUniqueConstraints(Table missingTable) {
Map<Column, UniqueConstraint> map = new HashMap<>();
List<UniqueConstraint> constraints = missingTable.getUniqueConstraints() == null ? null : missingTable.getUniqueConstraints();
for (UniqueConstraint constraint : constraints) {
if (constraint.getColumns().size() == 1) {
map.put(constraint.getColumns().get(0), constraint);
}
}
return map;
}
use of liquibase.structure.core.Column in project liquibase by liquibase.
the class MissingViewChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
View view = (View) missingObject;
CreateViewChange change = createViewChange();
change.setViewName(view.getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(view.getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(view.getSchema().getName());
}
if (view.getRemarks() != null) {
change.setRemarks(view.getRemarks());
}
String selectQuery = view.getDefinition();
boolean fullDefinitionOverridden = false;
if (selectQuery == null) {
selectQuery = "COULD NOT DETERMINE VIEW QUERY";
} else if ((comparisonDatabase instanceof OracleDatabase) && (view.getColumns() != null) && !view.getColumns().isEmpty()) {
String viewName;
if ((change.getCatalogName() == null) && (change.getSchemaName() == null)) {
viewName = comparisonDatabase.escapeObjectName(change.getViewName(), View.class);
} else {
viewName = comparisonDatabase.escapeViewName(change.getCatalogName(), change.getSchemaName(), change.getViewName());
}
selectQuery = "CREATE OR REPLACE FORCE VIEW " + viewName + " (" + StringUtil.join(view.getColumns(), ", ", new StringUtil.StringUtilFormatter() {
@Override
public String toString(Object obj) {
if ((((Column) obj).getComputed() != null) && ((Column) obj).getComputed()) {
return ((Column) obj).getName();
} else {
return comparisonDatabase.escapeColumnName(null, null, null, ((Column) obj).getName(), false);
}
}
}) + ") AS " + selectQuery;
change.setFullDefinition(true);
fullDefinitionOverridden = true;
}
change.setSelectQuery(selectQuery);
if (!fullDefinitionOverridden) {
change.setFullDefinition(view.getContainsFullDefinition());
}
return new Change[] { change };
}
Aggregations