Before you begin
Before you begin the tutorial, do the following:- , and . When starting your cluster, make sure that you generate cluster certificates, create the
bankdatabase, and create themaxuser. - Download and install a Java Development Kit. Liquibase supports JDK versions 8+. In this tutorial, we use AdoptOpenJDK 8, but you can follow along with any JDK version 8+.
Step 1. Download and install Liquibase
To install the Liquibase binary on your machine:- Download the latest version of the Liquibase command-line tool. CockroachDB is fully compatible with Liquibase versions 4.2.0 and greater. This tutorial uses the binary download of Liquibase 4.2.0 for macOS.
In this tutorial, we go through a manual installation, using a download of the binary version of the Liquibase command-line tool. If you are new to Liquibase, you can also use the Liquibase Installer to get started. The installer comes with some example properties and changelog files, an example H2 database, and a distribution of AdoptOpenJDK.
-
Make a new directory for your Liquibase installation:
-
Extract the Liquibase download to the new directory:
-
Append the full path of the
liquibasebinary (now located in theliquibase-4.2.0-binfolder) to your machine’sPATHenvironment variable:
If your terminal does not run
.bash_profile at start-up, you can alternatively append the liquibase path to the PATH definition in .bashrc or .profile.-
To verify that the installation was successful, run the following command:
You should get output similar to the following:
Step 2: Download the PostgreSQL JDBC driver
The Liquibase command-line tool uses the PostgreSQL JDBC driver to connect to CockroachDB as a Java application. To install the driver for Liquibase:- Download the latest JDBC driver from the PostgreSQL website.
-
Place the driver in the
libdirectory of the Liquibase binary. For example:
Step 3. Generate TLS certificates for the max user
When you , you should have created a user max. You should have also given this user the , which grants all privileges to all databases on the cluster. In this tutorial, Liquibase runs schema changes as the max user.
To authenticate connection requests to CockroachDB from the Liquibase client, you need to generate some certificates for max. Use to generate the certificates:
client.max.key.pk8.
Step 4: Create a changelog
Liquibase uses changelog files to manage database schema changes. Changelog files include a list of instructions, known as changesets, that are executed against the database in a specified order. Liquibase supports XML, YAML, and SQL formats for changelogs and changesets. Let’s define a changelog with the XML format:-
Create a file named
changelog-main.xml: -
Add the following to the blank
changelog-main.xmlfile:This first changeset uses thesqlFiletag, which tells Liquibase that an external.sqlfile contains some SQL statements to execute.
-
In the same directory, create the SQL file specified by the first changeset:
-
Add the following statement to the
create.sqlfile:When Liquibase runs, the first changeset will execute the statements increate.sql, creating a table namedaccount. -
Now let’s use the XML format to define the second changeset. Directly after the first
changeSetelement inchangelog-main.xml, add the following:This second changeset uses the Liquibase XML syntax to specify a series of sequentialINSERTstatements that initialize theaccounttable with some values.
changeset id values.
Step 5. Configure a Liquibase properties file
Liquibase properties are defined in a file namedliquibase.properties. These properties define the database connection information.
You can also set Liquibase properties with the
liquibase command-line tool.-
In the same directory as
changelog-main.xml, create aliquibase.propertiesfile: -
Add the following property definitions to the file:
For
url, the SSL connection parameters must specify the full paths of the certificates that you generated.Step 6. Run Liquibase
To run Liquibase from the command line, execute the following command from the directory containing yourliquibase.properties and changelog-main.xml files:
databasechangelog in the database where it performs changes. This table’s rows log all completed changesets.
To see the completed changesets, open a new terminal, start the , and query the databasechangelog table:
account table directly to see the latest changes reflected in the table:
Liquibase does not automatically. If a changeset fails at startup, you might need to restart the application manually to complete the changeset.
Step 7. Add additional changesets
Suppose that you want to change the primary key of theaccounts table from a simple, incrementing (in this case, id) to an auto-generated , to follow some . You can make these changes to the schema by creating and executing an additional changeset:
-
Create a SQL file to add a new -typed column to the table:
-
Add the following SQL statement to
add_uuid.sql:This statement adds to theaccountstable, with the default value as . -
In the
changelog-main.xmlfile, add the following after the secondchangeSetelement: -
Now create a SQL file to update the primary key for the table with the new column:
-
Add the following SQL statement to
update_pk.sql:This statement to use theunique_idcolumn. -
In the
changelog-main.xmlfile, add the following after the thirdchangeSetelement: -
To update the table, run
liquibase updateagain:You should see output similar to the following:
databasechangelog table:
account table directly to see the latest changes reflected in the table:
Liquibase and transactions
By default, Liquibase wraps each changeset within a single transaction. If the transaction fails to successfully commit, Liquibase rolls back the transaction. CockroachDB has . If a schema change fails, automatic rollbacks can lead to unexpected results. To avoid running into issues with incomplete transactions, we recommend setting therunInTransaction attribute on each of your changesets to "false", as demonstrated throughout this tutorial.
If
runInTransaction="false" for a changeset, and an error occurs while Liquid is running the changeset, the databasechangelog table might be left in an invalid state and need to be fixed manually.Transaction retries
When multiple, concurrent transactions or statements are issued to a single CockroachDB cluster, can cause schema migrations to fail. In the event of transaction contention, CockroachDB returns a40001 SQLSTATE (i.e., a serialization failure).
Liquibase does not automatically retry transactions. To handle transaction failures, we recommend writing client-side transaction retry logic. For more information about client-side transaction retries in CockroachDB, see .
Liquibase integrations
You can run Liquibase in the context of a Java application framework, like Spring Boot. For documentation on running Liquibase with other tooling, see the Liquibase documentation site.Report Issues with Liquibase and CockroachDB
If you run into problems, please file an issue on the Liquibase issue tracker, including the following details about the environment where you encountered the issue:- CockroachDB version ()
- Liquibase version
- Operating system
- Steps to reproduce the behavior

