Using Liquibase with Redshift
The purpose of this document is to guide you through the process of creating a new Liquibase project with Redshift. In this tutorial, you will generate an example project and follow the instructions to apply and learn concepts associated with creating new Liquibase projects with Redshift.
Prerequisites
- If you have not installed the latest version of Liquibase, navigate to https://www.liquibase.org/download to install the latest Liquibase version.
- Ensure the Liquibase install directory path is set to a location in the PATH System variable.
- Navigate to Redshift Driver download the latest Redshift jdbc jar driver file. Unzip the downloaded jar folder.
- Navigate to liquibase-redshift-releases and download the latest released Liquibase extension jar file.
- Place the two jar files in the
liquibase/lib
install directory.
Tutorial
To create a Liquibase project with Redshift on your machine, begin with the following steps:
- Create a new project folder and name it
LiquibaseProj
. - In your
LiquibaseProj
folder, create a new text file and name itdbchangelog.sql
. - Open the
dbchangelog.sql
file and update the changelog file with the following code snippet:
--liquibase formatted sql
- In your
LiquibaseProj
folder, create a new text file and name itliquibase.properties
. - Edit the
liquibase.properties
file to add the following properties:
changeLogFile: dbchangelog.sql
url: jdbc:redshift://<cluster-identifier>.us-east-1.redshift.amazonaws.com:5439/databasename
username: XXXX
password: XXXX
Note: If you already have a Liquibase Pro key and want to apply it to your project, add the following property to your liquibase.properties
. Ex: liquibaseProLicenseKey: <paste code here>
.
- Add a changeset to the changelog – changeset are uniquely identified by
author
andid
attributes. Liquibase attempts to execute each changeset in a transaction that is committed at the end. In thedbchangelog.sql
file line 3 add a new “department” create table change set as follows: - Open the command prompt. Navigate to the
LiquibaseProj
directory.
Run the following command:liquibase update
- From any SQL tool User Interface, check your database changes under “public schema”. You should see a new department table added to the database. For example:
--liquibase formatted sql
--changeset bob:1
CREATE TABLE DEPARTMENT ("ID" INTEGER, "NAME" VARCHAR, "ACTIVE" BOOLEAN);
SELECT * FROM "public"."DEPARTMENT";
ID | NAME | ACTIVE |
---|---|---|
NULL | NULL | NULL |
Also, you should 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 notice that a new row was created in that table with the changeset information we have just deployed. For this example:
ID | AUTHOR | FILENAME | DATEEXECUTED | ORDEREXECUTED | EXECTYPE | MDSUM | … |
---|---|---|---|---|---|---|---|
1 | bob | dbchangelog.sql | date&time
|
1 | EXECUTED | checksumvalue
|
… |
- DATABASECHANGELOGLOCK – This table is used internally by Liquibase to manage access to the changelog table during deployment.