Using Liquibase: track database changes and apply them
Liquibase records database changes in a file called a changelog, then applies only the changesets that have not yet been run on the target database. Keeping that file under version control with application code helps development, test, and production environments follow the same change sequence. The basic workflow is to record a change, inspect it with `validate` and `status`, review the SQL that would be generated, and then run `update`.
Using Liquibase: track database changes and apply them
Liquibase records database changes in a file called a changelog, then applies only the changesets that have not yet been run on the target database. Keeping that file under version control with application code helps development, test, and production environments follow the same change sequence. The basic workflow is to record a change, inspect it with validate and status, review the SQL that would be generated, and then run update.
This guide covers the general Liquibase CLI workflow for relational database schemas. Options and supported change types depend on Liquibase and database versions. The YAML and commands below are illustrative; they were not run against a database.
What do changelogs and changesets do?
A changelog is a text-based record of database changes. It can use XML, YAML, JSON, or formatted SQL and can be reviewed in source control alongside application changes. A changeset is one small unit in that record, such as creating a table, adding a column, or creating an index.
Liquibase identifies a changeset by combining its id, author, and changelog file path. It stores execution records in the database's DATABASECHANGELOG table. On later runs, it skips changes already recorded and applies newly discovered changes in sequence. It also uses DATABASECHANGELOGLOCK to prevent concurrent runs from changing the same database at once. Liquibase: What is a changelog? Liquibase: update
Choose a changelog format to suit team skills and portability needs. Formatted SQL makes database-specific syntax explicit. XML, YAML, or JSON can express intent through Liquibase change types, which helps Liquibase generate database-specific SQL. This does not mean every statement behaves identically on every database. Review generated SQL for vendor-specific syntax and data transformations. Liquibase: Changelog formats
A safe workflow for a new change
Suppose an application needs an optional preferred_language column in its customer table. Add a changeset to db/changelog/002-add-customer-language.yaml, for example:
databaseChangeLog:
- changeSet:
id: 002-add-customer-language
author: team
changes:
- addColumn:
tableName: customer
columns:
- column:
name: preferred_language
type: varchar(20)This is an illustration. Choose the real table, type, and constraints for the data model and target database. Adding a required column to a table with existing rows may need a default or a staged data backfill. For a large table, consider locks and deployment time, and whether the application can work with both old and new schemas during a staged rollout.
Connection details and the root changelog are commonly configured in a file such as liquibase.properties. Do not put passwords or tokens in version control; supply them through environment variables, a secrets manager, or another approved method. Confirm the target database and changelog path, then run:
liquibase validate --changelog-file=db/changelog/db.changelog.yaml
liquibase status --verbose --changelog-file=db/changelog/db.changelog.yaml
liquibase update-sql --changelog-file=db/changelog/db.changelog.yaml
liquibase update --changelog-file=db/changelog/db.changelog.yaml
liquibase status --verbose --changelog-file=db/changelog/db.changelog.yamlvalidate checks Liquibase-level issues such as changelog structure, referenced files, duplicate identifiers, and checksums. It does not prove that SQL is valid for the target database or that the change will succeed with its constraints and data. status shows changes that have not run. update-sql prints the SQL that would be applied so you can review it; it does not replace applying the change, and output can vary with connection, options, and Liquibase version. update applies the actual changes. Check status again afterward to confirm the expected changeset was recorded. Liquibase: validate Liquibase: update
Before running commands, confirm the connection URL points to the intended environment, the account has appropriate privileges, and you have a backup and deployment plan. Review generated SQL and data impact for production changes. Liquibase does not create the database or schema itself; prepare the target database and required schema first. Liquibase: generate-changelog
How do you start with an existing database?
Running a new changelog against a database that already contains objects can fail when Liquibase tries to create an object that was created manually. First represent the existing structure in a changelog and check that the changelog matches the actual target.
Connect to the correct database and schema and use `generate-changelog` to create a draft describing its current structure.
Review the result and account for environment differences and objects outside the intended scope. Generated output is not automatically a perfect expression of the team's intent or operating standards.
After confirming that the target structure matches the changelog, run `changelog-sync` to record the existing changesets as already executed.
Record future changes in new changesets and manage them with the normal `update` workflow.
changelog-sync is a baseline operation: it marks undeployed changesets as executed in DATABASECHANGELOG. It does not run their SQL to make the current database match. If the target differs from the baseline, changes can be skipped or later deployments can conflict. Verify the target and compare its schema before using this command. Liquibase: generate-changelog Liquibase: changelog-sync
Can you edit a changeset that has already been deployed?
As a rule, do not edit a changeset after deploying it to a permanent environment. Liquibase records its checksum and compares it on later runs, so changing its content can cause a validation error. Add a follow-up changeset instead; this preserves the original intent and history. runOnChange can be useful for definitions such as views or stored procedures that are meant to be reapplied when their definition changes. Use it selectively and ensure the SQL is safe to run again. Liquibase: runOnChange
Rollback is not always automatically safe. Liquibase can generate rollback SQL for some change types, but data deletion, external effects, and database-specific behavior may require a separate recovery plan. Before deployment, review the SQL and data impact. If a problem occurs, stop and compare both the DATABASECHANGELOG record and actual database state. Blindly clearing checksums or editing the history table can make the recorded history diverge from the schema; identify the cause before choosing a recovery method.
What should you remember?
Liquibase is reliable when its changelog agrees with the actual database. Record changes in small units, treat deployed changesets as immutable history, and use validate, status, and update-sql to inspect the planned change. For an existing database, generate and review its structure first, and use changelog-sync only after confirming that the target really matches the baseline. This helps track changes while reducing differences across environments.