Migrate to Liquibase

As shown in my last blog entry liquibase is very helpful to migrate even ‘living’-databases.

But how to migrate to liquibase? I propose the following steps:

  1. Backup all you data! Really do it! E.g. with xstream. First try the following steps on a test db, of course!
  2. Generate your initial changelog with liquibase:
    liquibase --defaultsFile=src/main/resources/liquibase.properties generateChangeLog

    Check the xml file and if you need additional data into liquibase you could read how to do this here. Or if you like you can do this via:

    <changeSet author="pk" id="1">
            <customChange class="de.InitBean"/>
    </changeSet>

    Then implement CustomSqlChange. Only the generateStatements method is important, where you add your sql statements. (Although it would be cool to use hibernate to create database independent statements. Anyone who did this?)

  3. Now drop all tables with squirell sql (right click all tables -> edit -> tables -> remove) and then execute in sql tab:
    drop sequence HIBERNATE_SEQUENCE;
  4. Now you should be able to update with liquibase. First look at the generated sql statements:
    liquibase --defaultsFile=src/main/resources/liquibase.properties updateSQL

    Then execute:

    liquibase --defaultsFile=src/main/resources/liquibase.properties update

    liquibase should say sth. like: Migration successful

  5. Try executing your program or run tests. And make sure the tests do not overwrite or clear the db schema!
  6. After these steps migrating via liquibase should be possible.

5 thoughts on “Migrate to Liquibase

  1. If you want to backup your database via using dbunit I suggest that you run
    PURGE recyclebin;
    drop sequence HIBERNATE_SEQUENCE;
    drop function SQUIRREL_GET_ERROR_OFFSET;

    if you use oracle as database.
    see:
    http://www.nabble.com/db-export-error-td5856579.html
    http://mojo.codehaus.org/dbunit-maven-plugin/examples/export-full.html
    http://dbunit.wikidot.com/supportedrdbms

    And maybe you want to see the generated sql statements:
    http://ericlefevre.net/wordpress/2007/02/23/p6spy-dbunit-and-maven/

    There was also another attempt to backup the data via hibernate’s ReverseXMLDatabinder
    http://opensource.atlassian.com/projects/hibernate/secure/ViewIssue.jspa?key=HB-493

  2. I am testing the tool and i noticed that it doesnt put into consideration the existing data. The change log doesnt have any existing data. This data is very important to because i am working with a legacy system that store all UI configuration into the database. Am i missing out on something?

    Nice tool though.

    Regards.

    Josh

  3. > The change log doesnt have any existing data.

    Right. I think this is not the intent of liquibase. The intent is saving + updating only the schema! But this updating can be done with existing data.

    To backup your data you can use dbunit or even an object oriented db (e.g. neodatis) or even more unlikely xstream

  4. Just stumbled on this and thought I add a note.

    There is no need to drop all existing tables. After you run generateChangeLog, run changelogSync. This creates the bookkeeping tables in your database and marks everything as already being done. So this in effect looks like you had been using liquibase the whole time.

Comments are closed.