2

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);

Cœur
  • 37,241
  • 25
  • 195
  • 267
larnouch
  • 189
  • 1
  • 15
  • Can you explain what "...changes on a DatabaseChangeLog object programmaticly..." means? I don't understand what you did. – Jens Mar 22 '17 at 12:04
  • @Jens i added an example above – larnouch Mar 22 '17 at 12:50
  • Maybe you can use the main [Liquibase](http://www.liquibase.org/javadoc/liquibase/Liquibase.html) class and call one of its methods to do this. I have not used liquibase by calling it directly from Java nor have I written any liquibase plugin. So maybe it is because of my lack of knowledge here, but how do you run the code (resp. how do you run liquibase?) – Jens Mar 22 '17 at 17:28
  • i am runing liquibase from java – larnouch Mar 23 '17 at 09:07

1 Answers1

3

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.

larnouch
  • 189
  • 1
  • 15