use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class DiffToReport method printSetComparison.
protected void printSetComparison(String title, Set<? extends DatabaseObject> objects, PrintStream out) {
out.print(title + ": ");
Schema lastSchema = null;
if (objects.isEmpty()) {
out.println("NONE");
} else {
out.println();
for (DatabaseObject object : objects) {
if (!diffResult.getReferenceSnapshot().getSnapshotControl().shouldInclude(object)) {
continue;
}
if (getIncludeSchema() && (object.getSchema() != null) && ((lastSchema == null) || !lastSchema.equals(object.getSchema()))) {
lastSchema = object.getSchema();
String schemaName = object.getSchema().getName();
if (schemaName == null) {
schemaName = object.getSchema().getCatalogName();
}
schemaName = includeSchemaComparison(schemaName);
out.println(" SCHEMA: " + schemaName);
}
out.println(" " + object);
}
}
}
use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class StringSnapshotSerializerReadable method serialize.
private String serialize(final DatabaseObject databaseObject, final DatabaseObject parentObject) {
StringBuilder buffer = new StringBuilder();
final List<String> attributes = sort(databaseObject.getAttributes());
for (String attribute : attributes) {
if ("name".equals(attribute)) {
continue;
}
if ("schema".equals(attribute)) {
continue;
}
if ("catalog".equals(attribute)) {
continue;
}
Object value = databaseObject.getAttribute(attribute, Object.class);
if (value instanceof Schema) {
continue;
}
if (value instanceof DatabaseObject) {
if ((parentObject != null) && ((DatabaseObject) value).getSnapshotId() != null && ((DatabaseObject) value).getSnapshotId().equals(parentObject.getSnapshotId())) {
continue;
}
boolean expandContainedObjects = shouldExpandNestedObject(value, databaseObject);
if (expandContainedObjects) {
value = ((DatabaseObject) value).getName() + "\n" + StringUtil.indent(serialize((DatabaseObject) value, databaseObject), INDENT_LENGTH);
} else {
value = databaseObject.getSerializableFieldValue(attribute);
}
} else if (value instanceof Collection) {
if (((Collection) value).isEmpty()) {
value = null;
} else {
if (((Collection) value).iterator().next() instanceof DatabaseObject) {
value = StringUtil.join(new TreeSet<>((Collection<DatabaseObject>) value), "\n", new StringUtil.StringUtilFormatter() {
@Override
public String toString(Object obj) {
if (obj instanceof DatabaseObject) {
if (shouldExpandNestedObject(obj, databaseObject)) {
return ((DatabaseObject) obj).getName() + "\n" + StringUtil.indent(serialize(((DatabaseObject) obj), databaseObject), INDENT_LENGTH);
} else {
return ((DatabaseObject) obj).getName();
}
} else {
return obj.toString();
}
}
});
value = "\n" + StringUtil.indent((String) value, INDENT_LENGTH);
} else {
value = databaseObject.getSerializableFieldValue(attribute);
}
}
} else {
value = databaseObject.getSerializableFieldValue(attribute);
}
if (value != null) {
buffer.append(attribute).append(": ").append(value).append("\n");
}
}
return buffer.toString().replaceFirst("\n$", "");
}
use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class StringSnapshotSerializerReadable method serialize.
@Override
public String serialize(LiquibaseSerializable object, boolean pretty) {
try {
StringBuilder buffer = new StringBuilder();
DatabaseSnapshot snapshot = ((DatabaseSnapshot) object);
Database database = snapshot.getDatabase();
buffer.append("Database snapshot for ").append(database.getConnection().getURL()).append("\n");
addDivider(buffer);
buffer.append("Database type: ").append(database.getDatabaseProductName()).append("\n");
buffer.append("Database version: ").append(database.getDatabaseProductVersion()).append("\n");
buffer.append("Database user: ").append(database.getConnection().getConnectionUserName()).append("\n");
SnapshotControl snapshotControl = snapshot.getSnapshotControl();
List<Class> includedTypes = sort(snapshotControl.getTypesToInclude());
buffer.append("Included types:\n").append(StringUtil.indent(StringUtil.join(includedTypes, "\n", new StringUtil.StringUtilFormatter<Class>() {
@Override
public String toString(Class obj) {
return obj.getName();
}
}))).append("\n");
List<Schema> schemas = sort(snapshot.get(Schema.class), new Comparator<Schema>() {
@Override
public int compare(Schema o1, Schema o2) {
return o1.toString().compareTo(o2.toString());
}
});
for (Schema schema : schemas) {
if (database.supportsSchemas()) {
buffer.append("\nCatalog & Schema: ").append(schema.getCatalogName()).append(" / ").append(schema.getName()).append("\n");
} else {
buffer.append("\nCatalog: ").append(schema.getCatalogName()).append("\n");
}
StringBuilder catalogBuffer = new StringBuilder();
for (Class type : includedTypes) {
if (type.equals(Schema.class) || type.equals(Catalog.class) || type.equals(Column.class)) {
continue;
}
List<DatabaseObject> objects = new ArrayList<DatabaseObject>(snapshot.get(type));
ListIterator<DatabaseObject> iterator = objects.listIterator();
while (iterator.hasNext()) {
DatabaseObject next = iterator.next();
if (next instanceof DatabaseLevelObject) {
continue;
}
Schema objectSchema = next.getSchema();
if (objectSchema == null) {
if (!(next instanceof CatalogLevelObject) || !((CatalogLevelObject) next).getCatalog().equals(schema.getCatalog())) {
iterator.remove();
}
} else if (!objectSchema.equals(schema)) {
iterator.remove();
}
}
outputObjects(objects, type, catalogBuffer);
}
buffer.append(StringUtil.indent(catalogBuffer.toString(), INDENT_LENGTH));
}
// standardize all newline chars
return buffer.toString().replace("\r\n", "\n").replace("\r", "\n");
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
}
use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class DatabaseSnapshot method includeNestedObjects.
private void includeNestedObjects(DatabaseObject object) throws DatabaseException, InvalidExampleException, ReflectiveOperationException {
for (String field : new HashSet<>(object.getAttributes())) {
Object fieldValue = object.getAttribute(field, Object.class);
if ("columns".equals(field) && ((object.getClass() == PrimaryKey.class) || (object.getClass() == Index.class) || (object.getClass() == UniqueConstraint.class))) {
if ((fieldValue != null) && !((Collection) fieldValue).isEmpty()) {
Column column = (Column) ((Collection) fieldValue).iterator().next();
String columnName = column.getName().toLowerCase();
if (BooleanUtil.isTrue(column.getDescending()) || columnName.endsWith(" asc") || columnName.endsWith(" desc") || columnName.endsWith(" random")) {
List<Column> columns = (List<Column>) fieldValue;
for (Column col : columns) {
if (col.getSnapshotId() != null) {
continue;
}
col.setSnapshotId(SnapshotIdService.getInstance().generateId());
includeNestedObjects(col);
referencedObjects.add(col);
}
continue;
}
}
}
Object newFieldValue = replaceObject(fieldValue);
if (newFieldValue == null) {
// sometimes an object references a non-snapshotted object. Leave it with the unsnapshotted example
if (((object instanceof UniqueConstraint) || (object instanceof PrimaryKey) || (object instanceof ForeignKey)) && "backingIndex".equals(field)) {
// unless it is the backing index, that is handled a bit strange and we need to handle the case where there is no backing index (disabled PK on oracle)
object.setAttribute(field, null);
}
} else if (fieldValue != newFieldValue) {
object.setAttribute(field, newFieldValue);
}
}
}
use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class DatabaseSnapshot method loadObjects.
protected void loadObjects(Map<String, DatabaseObject> objectMap, Map<String, DatabaseObject> allObjects, ParsedNode node, ResourceAccessor resourceAccessor) throws ReflectiveOperationException, ParsedNodeException {
if (node == null) {
return;
}
for (ParsedNode typeNode : node.getChildren()) {
Class<? extends DatabaseObject> objectType = (Class<? extends DatabaseObject>) Class.forName(typeNode.getName());
for (ParsedNode objectNode : typeNode.getChildren()) {
DatabaseObject databaseObject = objectType.getConstructor().newInstance();
databaseObject.load(objectNode, resourceAccessor);
String key = objectType.getName() + "#" + databaseObject.getSnapshotId();
objectMap.put(key, databaseObject);
allObjects.put(key, databaseObject);
}
}
}
Aggregations