Embedding Liquibase in applications


I would like to embed Liquibase in my application. As a standard practice I want to use in-memory H2 or HSQLDB for development and PostgreSQL/Oracle for deployment. The schema generation (during development and production) should happen through Liquibase. I know I can call the main method of Liquibase to do this (which in turn can read from liquibase.properties) but what I really want to be able to do is to pass a DataSource (or a Connection) instance to Liquibase so that Liquibase can generate the schema.

Embedding Liquibase is important in this case because in-memory databases must be created in the same JVM process to be useful. Is there a set of recommended API that I can use? Thanks in advance for any pointers.


It should be easy to do.  The liquibase.Liquibase class is your main facade class to deal with.  Take a look at liquibase.integaration.spring.SpringLiquibase.afterPropertiesSet() for a relatively simple example.

The main code you would be interesetd in is:

    Liquibase liquibase = null; try {     c = getDataSource().getConnection();     liquibase = createLiquibase(c);     liquibase.update(getContexts()); } catch (SQLException e) {     throw new DatabaseException(e); } finally {     if (liquibase != null) {         liquibase.forceReleaseLocks();     }     if (c != null) {         try {             c.rollback();             c.close();         } catch (SQLException e) {             //nothing to do         }     } }

(note: it may vary slightly between liquibase v1.9 and 2.0)