Running Liquibase from Java on a selected Postgres schema
Posted 10 Jun 2020 by Erik Lumme
Running Liquibase from Java is useful to ensure a schema is up to date on startup, or to update a schema as part of an integration test.
All code in this post can be found on GitHub.
The first step is to add the Liquibase core dependency from Maven central. Also ensure that your database driver of choice is on the classpath.
I created a class LiquibaseRunner with a constructor that accepts the database connection properties, as well as the names of two database schemas: the schema where the database change log tables are located, and the schema to update.
Liquibase requires a database connection. How this is obtained depends on your setup, one example is through the
In a Hibernate
Session, which you can get from an
entityManager.unwrap(Session.class), there is a method
doWork that provides a
With the connection, we can create a Liquibase
Here the schemas are set. With
SET search_path TO <schema>, we set the schema for that connection, which is the one Liquibase will update.
We can then create the
Database object using the
DatabaseFactory. We set the default schema name, which will be the schema where the
databasechangeloglock tables reside.
When using Postgres, if the default schema is not set in Liquibase, it will be the result of the
SELECT current_schema() query. In our case, that is the schema set as the
With these pieces in place, we can construct a
CHANGELOG_PATH is the path to the Liquibase change log. If it is on the classpath, the
ClassLoaderResourceAccessor can be used. If it is not, another accessor should be used, such as the
As the name suggests, the
ClassLoader#getResource, as opposed to
Class#getResource. This means that the path should not have a leading slash.
Now the updates can be run. We use Java’s try-with-resources to ensure the Liquibase instance is closed afterwards, which also closes the database connection.
LabelExpression can be used to only run certain change log entries, see Contexts vs. Labels.