Federico Cargnelutti

Simple is better than complex. Complex is better than complicated. | @fedecarg

Managing and applying database changes with LiquiBase

with 3 comments

LiquiBase is an open source, DBMS-independent library for tracking, managing and applying database changes. It is built on a simple premise: All database changes (structure and data) are stored in an XML-based descriptive manner and checked into source control.

Adding a changeset

<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.6"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.6
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.6.xsd">

    <changeSet id="1" author="bob">
        <createTable tableName="department">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="active" type="boolean" defaultValue="1"/>
        </createTable>
    </changeSet>

</databaseChangeLog>

Running the ChangeSet

There are many ways to execute your change log including via command line, Ant, Maven, Grails, etc.

liquibase --driver=com.mysql.jdbc.Driver \
--classpath=/path/to/classes \
--changeLogFile=com/example/db.changelog.xml \
--url="jdbc:mysql://localhost/example" \
--username=user \
--password=asdf \
migrate

Done!

You will see that your database now contains a table called “department”. Two other tables are created as well: “databasechangelog” and “databasechangeloglock”. The databasechangelog table contains a list of all the statements that have been run against the database. The databasechangeloglock table is used to make sure two machines don’t attempt to modify the database at the same time.

LiquiBase Website

Related Articles


Written by Federico

May 19, 2008 at 6:05 pm

3 Responses

Subscribe to comments with RSS.

  1. What is the advantage over keeping a file with SQL statements that updates the database?

    Karsten Silz

    May 21, 2008 at 1:20 pm

  2. * Increased agility and lower risk to business critical operations
    * Higher transaction throughput and faster batch processing
    * Increased developer productivity
    * Easier database maintenance
    * Simpler system integration
    * More scalable and reliable systems
    * Improved transactional security

    phpimpact

    May 21, 2008 at 1:55 pm

  3. [...] Bundle – May 21 Author : Jonathan Franzone No Comments Managing and applying database changes with LiquiBase Federico Cargnelutti gives a quick demonstration of how to use LiquiBase, an open source, [...]


Leave a Reply