Using Liquibase with Apache Derby
The purpose of this document is to guide you through the process of creating a new Liquibase project with Apache Derby. In this tutorial, you will learn how to generate an example project and follow the instructions to apply concepts associated with creating new Liquibase projects with Apache Derby.
Prerequisites
- Ensure that you have installed the latest version of Liquibase. If not, go to https://www.liquibase.org/download to install it.
- Ensure the
liquibase.bat
file's path is set to a location in the PATH System variable. - Go to https://db.apache.org/derby/derby_downloads.html to download the Derby client jdbc jar driver file.
Note: Apache Derby 10.15 has the driver in the derbytools.jar
file. Place your derbytools.jar
file in the Liquibase/lib
install directory or in any other known directory so you can locate it easily.
Tutorial
To create a Liquibase project with Apache Derby, perform the following steps:
- Create a new project folder and name it
LiquibaseDerby
. - In your
LiquibaseDerby
folder, create a new text file and name itliquibase.properties
. - Edit the
liquibase.properties
file to add the following properties:
changeLogFile: dbchangelog.xml
url: jdbc:derby://localhost:1527/MYDATABASE;create=true
username: APP
password: password
driver: org.apache.derby.jdbc.ClientDriver
classpath: ../../Liquibase_Drivers/derbytools.jar
Note: Specifying your password, take into account that Liquibase supports only the following special characters: ~ # $ % * ( ) - _ + [ ] { } . ?
. Unsupported special characters are as follows: @ & / : < > " ' ` | ^ ! = , \ <spaces>
.
- If you created MYDATABASE, use
create=false
or remove thecreate=true
from URL. - 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>
- In your
LiquibaseDerby
folder, create a new text file and name itdbchangelog.xml
. The changelog files contain a sequence of changesets, each of which makes small changes to the structure of your database.
Note: Instead of creating an empty changelog file, you can use an existing database to generate a changelog. In this tutorial, you will manually add a single change. To add this change, open the XML file and update the changelog file with the following code snippet:
<?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>
- Add a changeset to the changelog. The changesets are uniquely identified by
author
andid
. Liquibase attempts to execute each changeset in a transaction that is committed at the end. In thedbchangelog.xml
file, add a newdepartment
create table changeset as follows:
<?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">
<changeSet id="1" author="bob">
<createTable tableName="department">
<column name="id" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="active" type="boolean" defaultValueBoolean="true"/>
</createTable>
</changeSet>
</databaseChangeLog>
Note: The preceding changeset is XML format. The corresponding SQL statement looks like the following:
--liquibase formatted sql
--changeset bob:1
CREATE TABLE "DEPARTMENT" ("ID" INTEGER, "NAME" STRING, "ACTIVE" BOOLEAN);
--changeset bob:1
CREATE TABLE "DEPARTMENT"
(id INTEGER NOT NULL,
name VARCHAR (50) NOT NULL,
active BOOLEAN DEFAULT TRUE,
CONSTRAINT PK_DEPARTMENT id PRIMARY KEY);
- Open the command prompt, navigate to the
LiquibaseDerby
directory, and then run the following command:
liquibase update
- From a database UI Tool, check your database changes. You will see a new
department
; table added to the database. For example:
SELECT * FROM "APP"."department";
ID | Name | Active |
---|---|---|
NULL |
NULL |
NULL |
Also, you will see two more tables:
DATABASECHANGELOG tracking table. This table keeps a record of all the changesets that were deployed. This way, next time when you deploy again, 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. You will see that a new row was created in that table with the changeset information we have just deployed. For example:
ID | Author | Filename | Dateexecuted | Orderexecuted | Exectype | MDSum |
---|---|---|---|---|---|---|
1 |
bob |
dbchangelog.xml |
date&time |
1 |
EXECUTED |
checksumvalue |
DATABASECHANGELOGLOCK tracking table. This table is used internally by Liquibase to manage access to the changelog table during deployment.
Troubleshooting issues on the Mac OS
If your Derby Server is not running or you are not using the embedded driver, use the following commands on the Mac to start the Derby Server:
export DERBY_HOME=<location_of the unzipped directory_for_derby>
Note: export DERBY_HOME=/Users/myname/Downloads/db-derby-10.15.2.0-bin
export JAVA_HOME=<path_to_your_JRE>
Note: Use the actual installed location of the JRE in place of <path_to_your_JRE>
since Apache Derby will expect a bin directory as a subfolder. For example, export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
java -jar $DERBY_HOME/lib/derbynet.jar start -h 0.0.0.0