Using Liquibase with DB2 LUW
The purpose of this document is to guide you through the process of creating a new Liquibase project with DB2 LUW. In this tutorial, you will learn how to install the required database drivers and configure the liquibase.properties
file to establish a database connection to DB2 LUW.
Prerequisites
- Ensure that you have installed the latest version of Liquibase. If not, go to https://www.liquibase.org/download to install it.
- Ensure that the Liquibase executable location is in the PATH environment variable.
- Go to https://www.ibm.com/support/pages/db2-jdbc-driver-versions-and-downloads to download the jdbc jar driver file to connect to DB2 LUW. The IBM Data Server Driver for JDBC and SQLJ package includes two JDBC drivers—
db2jcc.jar
anddb2jcc4.jar
. Thedb2jcc.jar
driver is deprecated. Use thedb2jcc4.jar
file. - Go to https://github.com/liquibase/liquibase-db2i/releases to download the Liquibase DB2 extension for iSeries support.
Note: Place your jdbc jar driver file in the Liquibase/lib
install directory or in any other known directory so you can locate it easily. Place the extension jar file in the Liquibase/lib
install directory.
Tutorial
To create a Liquibase project with DB2 LUW, perform the following steps:
- Create a new project folder and name it
LiquibaseDB2LUW
. - In your
LiquibaseDB2LUW
folder, create a new text file and name itliquibase.properties
.
Note: For more information about the liquibase.properties
file, see Creating and configuring a liquibase.properties file.
- Edit the
liquibase.properties
file to add the following properties:
changeLogFile: dbchangelog.xml
url: jdbc:db2://192.168.1.15:5432/MYDATABASE
username: user
password: password
driver: com.ibm.db2.jcc.DB2Driver
classpath: ../../Liquibase_Drivers/db2jcc4.jar
Note: Specifying your password, take into account that Liquibase supports only the following special characters: ~ # $ % * ( ) - _ + [ ] { } . ?
. Unsupported special characters are as follows: @ & / : < > " ' ` | ^ ! = , \ <spaces>
.
Use the following format for the url
property:
url: jdbc:db2://<servername>:<port>/<dbname>
- If you placed your jar file in the
Liquibase/lib
install directory, there is no need to specify theclasspath
property in theliquibase.properties
file. Otherwise, put the path to your driver as it is shown in the preceding example. - If you already have a Liquibase Pro key and want to apply it to your project, add the following property to your
liquibase.properties
file:
liquibaseProLicenseKey: <paste license key>
- Create a changelog file. The changelog files contain a sequence of changesets, each of which makes small changes to the structure of your database. For more information, see changelog. Liquibase provides a changelog template located at
$LIQUIBASE_HOME/examples/xml/blank.changelog.xml
. Copy theblank.changelog.xml
file to yourLiquibaseDB2LUW
folder and rename it todbchangelog.xml
. Each version of Liquibase will have an updated XML schema, so use the one that matches the version of Liquibase you have installed. The contents of the changelog will be similar to the following:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
</databaseChangeLog>
Note: The preceding changelog is XML format. The corresponding SQL changelog statement looks like the following:
--liquibase formatted sql
- Verify the project configuration. Run the
status
command to verify that the configuration is complete. Open a command prompt and navigate to the project folderLiquibaseDB2LUW
. Run the command as follows:
liquibase status
- Verify that the DATABASECHANGELOG and DATABASECHANGELOGLOCK tables were created. From a database UI Tool, check your new Liquibase tables.
DATABASECHANGELOG tracking table. This table keeps a record of all the changesets that were deployed. When you add changesets to your changelog and run the update
command, the changesets in the changelog will be compared with the DATABASECHANGELOG tracking table, and only the new changesets that were not found in the DATABASECHANGELOG will be deployed.
DATABASECHANGELOGLOCK tracking table. This table is used internally by Liquibase to manage access to the changelog table during deployment.
Congratulations!
You have successfully configured your project and can begin creating changesets to migrate changes to your database. For more information on how to create changesets, see changeset.