Search in sources :

Example 26 with DatabaseObject

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);
        }
    }
}
Also used : Schema(liquibase.structure.core.Schema) DatabaseObject(liquibase.structure.DatabaseObject)

Example 27 with DatabaseObject

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$", "");
}
Also used : DatabaseObject(liquibase.structure.DatabaseObject) DatabaseLevelObject(liquibase.structure.DatabaseLevelObject) DatabaseObject(liquibase.structure.DatabaseObject) CatalogLevelObject(liquibase.structure.CatalogLevelObject) StringUtil(liquibase.util.StringUtil)

Example 28 with DatabaseObject

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);
    }
}
Also used : CatalogLevelObject(liquibase.structure.CatalogLevelObject) IOException(java.io.IOException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseLevelObject(liquibase.structure.DatabaseLevelObject) Database(liquibase.database.Database) DatabaseObject(liquibase.structure.DatabaseObject) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 29 with DatabaseObject

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);
        }
    }
}
Also used : DatabaseObjectCollection(liquibase.structure.DatabaseObjectCollection) DatabaseObject(liquibase.structure.DatabaseObject) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 30 with DatabaseObject

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);
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) DatabaseObject(liquibase.structure.DatabaseObject)

Aggregations

DatabaseObject (liquibase.structure.DatabaseObject)47 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)14 CompareControl (liquibase.diff.compare.CompareControl)10 Change (liquibase.change.Change)9 Database (liquibase.database.Database)9 CatalogAndSchema (liquibase.CatalogAndSchema)7 DiffResult (liquibase.diff.DiffResult)6 ObjectDifferences (liquibase.diff.ObjectDifferences)6 ArrayList (java.util.ArrayList)5 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)5 Column (liquibase.structure.core.Column)5 Table (liquibase.structure.core.Table)5 HashSet (java.util.HashSet)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 DiffOutputControl (liquibase.diff.output.DiffOutputControl)4 DatabaseException (liquibase.exception.DatabaseException)4 InvalidExampleException (liquibase.snapshot.InvalidExampleException)4 SnapshotControl (liquibase.snapshot.SnapshotControl)4 DatabaseObjectCollection (liquibase.structure.DatabaseObjectCollection)4 List (java.util.List)3