Saturday, 28 July 2012

Viewing Liquibase's Generated Changelog At Runtime

I know grails 2.x has a migrations plugin, but if you still using / prefer the liquibase one then read on...

I use liquibase in all environments "north" of development to manage the database schema and reference data, but still use create-drop while developing locally. It can be a pain to construct changesets by hand, but it's also awkward to run grails generate-changelog (especially if development normally uses an in memory database).  Much better if you could see the generated changelog while the application is running. Here's how...

import liquibase.database.*
import liquibase.diff.*
import java.sql.Connection

class LiquibaseController {

    def dataSource
    def index() {
        Connection connection = dataSource.connection
        DatabaseFactory factory = DatabaseFactory.getInstance()
        Database database = factory.findCorrectDatabaseImplementation(connection)
        Diff diff = new Diff(database, 'schemaName')
        DiffResult diffResult =
        diffResult.printChangeLog(new PrintStream(response.outputStream), database)
        response.contentType = 'text/xml'