Search in sources :

Example 1 with DatabaseObjectCollection

use of liquibase.structure.DatabaseObjectCollection in project liquibase by liquibase.

the class YamlSnapshotSerializer method toMap.

//    @Override
//    public String serialize(LiquibaseSerializable object, boolean pretty) {
//        if (object instanceof DatabaseObject) {
//            if (alreadySerializingObject) {
//                return ((DatabaseObject) object).getObjectTypeName()+"#"+((DatabaseObject) object).getSnapshotId();
//            } else {
//                alreadySerializingObject = true;
//                String string = super.serialize(object, pretty);
//                alreadySerializingObject = false;
//                return string;
//            }
//        }
//        return super.serialize(object, pretty);
//    }
@Override
protected Object toMap(final LiquibaseSerializable object) {
    if (object instanceof DatabaseObject) {
        if (alreadySerializingObject) {
            String snapshotId = ((DatabaseObject) object).getSnapshotId();
            if (snapshotId == null) {
                String name = ((DatabaseObject) object).getName();
                Object table = ((DatabaseObject) object).getAttribute("table", Object.class);
                if (table == null) {
                    table = ((DatabaseObject) object).getAttribute("relation", Object.class);
                }
                if (table != null) {
                    name = table.toString() + "." + name;
                }
                if (((DatabaseObject) object).getSchema() != null) {
                    name = ((DatabaseObject) object).getSchema().toString() + "." + name;
                }
                throw new UnexpectedLiquibaseException("Found a null snapshotId for " + StringUtils.lowerCaseFirst(object.getClass().getSimpleName()) + " " + name);
            }
            return ((DatabaseObject) object).getClass().getName() + "#" + snapshotId;
        } else {
            alreadySerializingObject = true;
            Object map = super.toMap(object);
            alreadySerializingObject = false;
            return map;
        }
    }
    if (object instanceof DatabaseObjectCollection) {
        SortedMap<String, Object> returnMap = new TreeMap<String, Object>();
        for (Map.Entry<Class<? extends DatabaseObject>, Set<? extends DatabaseObject>> entry : ((DatabaseObjectCollection) object).toMap().entrySet()) {
            ArrayList value = new ArrayList(entry.getValue());
            Collections.sort(value, new DatabaseObjectComparator());
            returnMap.put(entry.getKey().getName(), value);
        }
        return returnMap;
    }
    return super.toMap(object);
}
Also used : DatabaseObjectCollection(liquibase.structure.DatabaseObjectCollection) DatabaseObject(liquibase.structure.DatabaseObject) DatabaseObjectComparator(liquibase.structure.DatabaseObjectComparator) DatabaseObject(liquibase.structure.DatabaseObject) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 DatabaseObject (liquibase.structure.DatabaseObject)1 DatabaseObjectCollection (liquibase.structure.DatabaseObjectCollection)1 DatabaseObjectComparator (liquibase.structure.DatabaseObjectComparator)1