liquibaseliquibase-hibernate

How to generate xml file from DatabaseChangeLog programmatically


I made changes on a DatabaseChangeLog object programmatically. How could I generate the diffChangeLogFile file based on that DatabaseChangeLog from java.

EDIT: here is an example

public DatabaseChangeLog removeDropColumnChangeFromDatabaseChangeLog(
            DatabaseChangeLog databaseChangeLog, String oldName,
            String newName, String tableName) {

        DatabaseChangeLog changeLog = databaseChangeLog;
        Collection<ChangeSet> changeSetsToRemove = new ArrayList<ChangeSet>();

        List<ChangeSet> changeSets = changeLog.getChangeSets();

        // Remove DropColumnChange from ChangeSets using tableName, oldName and
        // newName
        for (ChangeSet cs : changeSets) {
            for (Change change : cs.getChanges()) {
                if (change instanceof DropColumnChange) {
                    DropColumnChange aux2 = (DropColumnChange) change;
                    if (aux2.getTableName().equals(tableName)) {
                        if (aux2.getColumnName().equals(oldName)) {
                            changeSetsToRemove.add(cs);
                        }
                    }
                }
            }
        }
        changeSets.removeAll(changeSetsToRemove);
        DatabaseChangeLog databaseChangeLogWithoutAddColumn = new DatabaseChangeLog();
        for (ChangeSet cs : changeSets) {
            databaseChangeLogWithoutAddColumn.addChangeSet(cs);
        }
        return databaseChangeLogWithoutAddColumn;
    }

this method will have a DatabaseChangeLog object as input and it will delete the dropColumn change from it when the table name is equal to the tableName parameter.

After this modification on DatabaseChangeLog object I want to generate an xml file which contain the changesets relative to the DatabaseChangeLog object (the xml that liquibase generate after a diff in order to make an update).

Something like this:

databaseChange.generateXmlFile(pathToFile);


Solution

  • Finnaly i found into liquibase source code how it converts a list of changeSets into an xml file output.(we can retrieve the list of chnageSets from databaseChangeLog)

        /**
         * Prints changeLog that would bring the target database to be the same as
         * the reference database
         */
        public void print(PrintStream out, ChangeLogSerializer changeLogSerializer) throws ParserConfigurationException, IOException, DatabaseException {
    
            List<ChangeSet> changeSets = generateChangeSets();
    
            changeLogSerializer.write(changeSets, out);
    
            out.flush();
        }
    

    Here is an example how I generated the xml file output after making changes into the databaseChangeLog object

            // TESTING CHANGELOG GENERATION 
            PrintStream printStreamFile =null;
            try {
                printStreamFile = new PrintStream("pathTofile/changeLogAfterChange.xml");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            XMLChangeLogSerializer changeLogSerializer = new XMLChangeLogSerializer();
    
            try {
                changeLogSerializer.write(databaseChangeLog.getChangeSets(), printStreamFile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    

    I hope this will help.