Liquibase Database change tracking

Version 5.0.4 · source

Start here

liquibase init project
Scaffold a project
liquibase validate
Check the changelog
liquibase status
What is pending?
liquibase update-sql
Preview the SQL
liquibase update
Deploy changesets
liquibase history
What was deployed?
liquibase tag --tag=<name>
Mark a rollback point
liquibase rollback --tag=<name>
Undo back to a tag

Concepts

  1. Changelog changesets in a file
  2. Liquibase compares ids + checksums
  3. Database your schema
  4. DATABASECHANGELOG what already ran

Project and connection

liquibase --version
Show the installed version
liquibase <command> --help
Show every flag of a command
--url=<jdbc-url>
JDBC connection URL
--username=<user> --password=<pw>
Database credentials
--changelog-file=<path>
Root changelog (xml, yaml, json or sql)
--defaults-file=<file>
Read settings from another properties file
--log-level=<level>
Set verbosity (global flag, goes first)

Changelog formats

-- liquibase formatted sql
First line of a SQL changelog
-- changeset <author>:<id>
Start a changeset in a SQL changelog
-- rollback <sql>
Rollback statement for the changeset above
<include file="<path>"/>
Pull another changelog into the root file
-- changeset <a>:<id> runOnChange:true
Re-run when its SQL changes
<tagDatabase tag="<name>"/>
Tag the database from inside a changelog

Update

liquibase update
Deploy all pending changesets
liquibase update-sql
Print the SQL update would run
liquibase update-count --count=<n>
Deploy only the next n changesets
liquibase update-testing-rollback
Deploy, roll back, redeploy to test rollbacks
--context-filter=<ctx>
Run only changesets in these contexts
Called --contexts in 4.23.0 and older.

Rollback

liquibase rollback --tag=<name>
Roll back to a tag
liquibase rollback-count --count=<n>
Undo the last n changesets
liquibase rollback-to-date --date=<date>
Undo everything after a date
liquibase rollback-sql --tag=<name>
Preview the SQL of a tag rollback
liquibase rollback-count-sql --count=<n>
Preview the SQL of a count rollback

Inspect and track

liquibase status
List changesets not yet deployed
liquibase status --verbose
Include file, author and id of each one
liquibase history
List changesets already deployed
liquibase tag --tag=<name>
Mark the current database state
liquibase diff --reference-url=<url>
Compare two databases
liquibase diff-changelog --reference-url=<url>
Write a changelog that closes the diff

Generate and sync

liquibase generate-changelog --changelog-file=<file>
Reverse-engineer an existing database
--schemas=<a,b>
Limit generate or diff to some schemas
liquibase changelog-sync
Mark all pending changesets as run, without running

Maintenance

liquibase validate
Find changelog errors before deploying
liquibase release-locks
Clear a lock left by a crashed run
liquibase clear-checksums
Reset stored checksums after an intended edit
liquibase drop-all
Drop every object in the target schema
Destructive. Only point it at a throwaway database.

Configuration

url: <jdbc-url>
Connection URL in liquibase.properties
changelogFile: <path>
Root changelog in liquibase.properties

Recipes

Safe deploy
liquibase validateliquibase status --verboseliquibase update-sqlliquibase tag --tag=before-releaseliquibase update
Undo a release
liquibase rollback-sql --tag=before-releaseliquibase rollback --tag=before-release
Adopt an existing database
liquibase generate-changelog --changelog-file=changelog.xmlliquibase changelog-syncliquibase status