All Downloads are FREE. Search and download functionalities are using the official Maven repository.

liquibase.serializer.core.yaml.YamlChangeLogSerializer Maven / Gradle / Ivy

There is a newer version: 3.6.2.5.inovus
Show newest version
package liquibase.serializer.core.yaml;

import liquibase.changelog.ChangeLogChild;
import liquibase.changelog.ChangeSet;
import liquibase.configuration.GlobalConfiguration;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.serializer.ChangeLogSerializer;
import liquibase.serializer.LiquibaseSerializable;

import java.io.*;
import java.util.*;

public class YamlChangeLogSerializer extends YamlSerializer implements ChangeLogSerializer {

    protected Comparator getComparator(LiquibaseSerializable object) {
        if (object instanceof ChangeSet) {
            return new ChangeSetComparator();
        } else {
            return super.getComparator(object);
        }
    }

    @Override
    public  void write(List children, OutputStream out) throws IOException {
        List maps = new ArrayList<>();
        for (T changeSet : children) {
            maps.add(toMap(changeSet));
        }
        Map containerMap = new HashMap<>();
        containerMap.put("databaseChangeLog", maps);

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()));
        writer.write(yaml.dumpAsMap(containerMap));
        writer.write("\n");
        writer.flush();
    }


    @Override
    public void append(ChangeSet changeSet, File changeLogFile) throws IOException {
        //To change body of implemented methods use File | Settings | File Templates.
    }



    private static class ChangeSetComparator implements Comparator {
        private static final Map order = new HashMap<>();

        static {
            order.put("id", 1);
            order.put("author", 2);
            order.put("changes", Integer.MAX_VALUE);
        }

        @Override
        public int compare(String o1, String o2) {
            Integer o1Order = order.get(o1);
            if (o1Order == null) {
                o1Order = 10;
            }

            Integer o2Order = order.get(o2);
            if (o2Order == null) {
                o2Order = 10;
            }

            int orderCompare = o1Order.compareTo(o2Order);

            if (orderCompare == 0) {
                return o1.compareTo(o2);
            }
            return orderCompare;
        }
    }

    @Override
    public int getPriority() {
        return PRIORITY_DEFAULT;
    }
}