This page describes a number of best practices that you can apply on your project.
The most common way to organize your changelogs is by major release. Choose a package in your classpath to store the changelogs, preferably near your database access classes. In this example, we will use com/example/db/changelog
com
example
db
changelog
db.changelog-master.xml
db.changelog-1.0.xml
db.changelog-1.1.xml
db.changelog-2.0.xml
DatabasePool.java
AbstractDAO.java
The master.xml includes the changelog for the releases in the correct order. In the example above it could look like this:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<include file="com/example/db/changelog/db.changelog-1.0.xml"/>
<include file="com/example/db/changelog/db.changelog-1.1.xml"/>
<include file="com/example/db/changelog/db.changelog-2.0.xml"/>
</databaseChangeLog>
Each of the included XML files needs to be in the same format as a standard XML database change log, something like this:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<changeSet author="authorName" id="changelog-1.0">
<createTable tableName="TablesAndTables">
<column name="COLUMN1" type="TEXT">
<constraints nullable="true" primaryKey="false" unique="false"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
The db.changelog-master.xml is the changelog you pass to all Liquibase calls.
Try to maintain separate changelog for Stored Procedures and use runOnChange=”true”. This flag forces LiquiBase to check if the changeset was modified. If so, liquibase executes the change again.
As far as possible, Avoid multiple changes per changeset to avoid failed autocommit statements that can leave the database in an unexpected state.
Choose what works for you. Some use a sequence number starting from 1 and unique within the changelog, some choose a descriptive name (e.g. ‘new-address-table’).
Use <comments>
in the change sets. They say “A stitch in time saves nine!”
Try to write changesets in a way that they can be rolled back. e.g. use relevant change clause instead of using custom <sql>
tag.
Include a <rollback>
clause whenever a change doesn’t support out of box rollback. (e.g. <sql>
, <insert>
, etc)
Leverage Liquibase to manage your Reference Data. Environment separation (DEV, QA, PROD) can be achieved using “context”.
Liquibase Pro offers an extra layer of support so you don’t have to go it alone. While Liquibase is a great starting point, teams that want best practices on how to make the most of Liquibase may find Liquibase Pro better suited to their needs. Liquibase Pro also adds functions for procedural database code.
Datical is a commercial product which builds on the core Liquibase functionality. Beyond versioning and managing database changes, Datical bridges the gap between development and operations with capabilities that enable a fully unified and automated path for database code.
For more information on Datical, visit https://www.datical.com/Liquibase