Before performing any of these steps, you must be able to connect to a local or remote database, accessible via command line or IDE/GUI.
For this example, we are going to use an H2 database. Please review this H2 Tutorial to get started.
View the Supported Databases topic for more information on which databases we support. You can also view the Database Tutorials topic for more information on running each database with Liquibase.
To complete your first migration, you must have a database changelog file. The Changelog file is where all your database changes are defined. Using Liquibase Functions allows you to define these changes with XML, JSON, or YAML. For this walkthrough, we will use XML examples.
Creating Changelog Files Manually
myChangeLog.xml
myChangeLog.xml
file, then save your file:Generating Changelog Files
If you have an existing database, you can generate a changelog file that reflects the current state of your database. For more information on how to generate a changelog, visit the Liquibase Commands: generateChangelog topic, and read the article on adding Liquibase on an existing project.
ChangeSets are (units of change) that Liquibase can execute on a database. When adding a changeSet, your change must be defined by both an “id” attribute and an “author” attribute. Using only an “id” attribute can cause accidental duplications when dealing with multiple developers and code branches. It is best practice to only include one change in each changeset.
View the changeSet tag topic for more information.
To add a changeSet:
myChangeLog.xml
file.myChangeLog.xml
file, then save your file:When you add a changeSet, Liquibase reads your list of changeSets in order, then checks the DATABASECHANGELOG
table for anything that was previously run.
To run the changeset:
LB_HOME/liquibase update
LB_HOME\liquibase.bat update
Note: In place of LB_HOME use the folder name where you extracted liquibase.
Your database now contains a table called department.
To check your database:
Note: Where (driver-version.jar) is listed, enter your driver name and version number. Example:
java -jar h2-1.4.199.jar
.
If you used a liquibase.properties
file, enter the JDBC URL, User Name, and Password. Notice that two tables were created:
The DATABASECHANGELOG table contains a list of all the changes that have been run against the database. The DATABASECHANGELOGLOCK table is used to make sure two machines don’t attempt to modify the database at the same time.
View DATABASECHANGELOG Table and DATABASECHANGELOGLOCK Table topics for more information.
You can also learn how to create your First Migrations with SQL.
In this tutorial we covered: