Search in sources :

Example 1 with CatalogLevelObject

use of liquibase.structure.CatalogLevelObject 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(StringUtils.indent(StringUtils.join(includedTypes, "\n", new StringUtils.StringUtilsFormatter<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(StringUtils.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) 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)

Aggregations

Database (liquibase.database.Database)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)1 SnapshotControl (liquibase.snapshot.SnapshotControl)1 CatalogLevelObject (liquibase.structure.CatalogLevelObject)1 DatabaseLevelObject (liquibase.structure.DatabaseLevelObject)1 DatabaseObject (liquibase.structure.DatabaseObject)1