Make and track changes with Liquibase
Once your changelog structure is set up, you’re ready to start making changes to your database and tracking those changes with Liquibase.
Liquibase uses changesets to define discrete changes to your database schema or data. Each changeset has a unique id and author to track what has been applied.
Make and track changes with Liquibase
Make sure you've installed Liquibase and can run
liquibase
from your command line.Connect your database to Liquibase. You'll need to follow the configuration guide for your specific database. You'll also create your first changelog during this process to test your database connection.
If you have an existing database, you'll also need to generate your changelog from your existing database.
Set up your changelog structure with a root changelog and connect your changelogs.
Procedure
Add changesets to your changelog files.
Changes in Liquibase are defined inside changesets, which are units of change that include one or more database operations. Each changeset has a unique id
and author
attribute to identify it.
Add new changesets to the appropriate nested changelog file based on your project structure. Here’s a simple example of a changeset in XML format that creates a new table:
<changeSet id="1" author="your.name"> <createTable tableName="person"> <column name="id" type="int"> <constraints primaryKey="true"/> </column> <column name="name" type="varchar(50)"> <constraints nullable="false"/> </column> </createTable> </changeSet>
<changeSet id="1" author="your.name">
<createTable tableName="person">
<column name="id" type="int">
<constraints primaryKey="true"/>
</column>
<column name="name" type="varchar(50)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
--liquibase formatted sql
--changeset your.name:1
CREATE TABLE person (id INT PRIMARY KEY, name VARCHAR(50) NOT NULL);
Run Liquibase update to apply changes.
Once you’ve added or updated changesets, apply them to your database using the liquibase update
command, specifying the changelog file you want to run. This will typically be your root changelog
liquibase update --changelog-file=changelog-root.xml
You run your root changelog file because it serves as the central point that organizes and references all other nested changelogs in your project. By running the root changelog, Liquibase processes every included changelog and changeset in the correct order, ensuring that all database changes across your project are applied consistently and completely.
Liquibase also keeps track of which changesets have already been applied to your database using a special table (DATABASECHANGELOG
). This means when you run update
, Liquibase only applies new or pending changesets that have not been run before. This prevents duplicate changes and keeps your database schema in sync with your changelog files.
Using the root changelog simplifies deployment by letting you apply all your changes in a single command, rather than running updates on each individual changelog file.