Introduction to Liquibase and Managing Your Database Source Code

Shay Shmeltzer
Oracle Developers
Published in
4 min readOct 16, 2017

In previous blogs I showed how you can manage SQL scripts lifecycle with the help of Oracle Developer Cloud Service (DevCS) as part of an overall Oracle DB DevOps solution. I wanted to add one more utility that might act as an alternative or addition to the SQL script managing — Liquibase.

Liquibase is an open source solution for managing revisions of your databse schema scripts. It works across various types of databases, and supports various file formats for defining the DB structure. The feature that is probably most attractive in Liquibase is its ability to roll changes back and forward from a specific point — saving you from the need to know what was the last change/script you ran on a specific DB instance.

Liquibase uses scripts — referred to as “changesets” — to manage the changes you do to your DB. The changesets files can be in various formats including XML, JSON, YAML, and SQL. In the examples below I’m using the XML format.

As you continue to change an enhance your DB structure through the development lifecycle you’ll add more changesets. A master file lists all the changeset files (or the directories where they are). In parallel Liquibase tracks in your database which changesets have already run.

When you issue a liquibase update command, liquibase looks at the current state of your DB, and identifies which changes have already happened. Then it run the rest of the changes — getting you to the latest revision of the structure you are defining.

By integrating Liquibase into your overall code version management system and continuous integration platform you can synch up your database versions with your app version. In my case this would of course mean integration with Oracle Developer Cloud Service (DevCS) — which you get for free with the Oracle Database Cloud Service. In the video below I show a flow that covers:

  • Tracking my DBA tasks in the issue system
  • Modifying a local MySQL DB with Liquibase (doing forward and backward rolls)
  • Adding a change set defining a new table
  • Committing to Git
  • Automatic build implementing the changes in Oracle Database Cloud Service
  • Automatic testing with UT/PLSQL

Here is a quick 10 minute demo:

For those who want to try and replicate this, here are some resources.

A changeset that creates a “department” table with three columns:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" 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.1.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet id="0" author="shay">
<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>

A changeset that creates PL/SQL function, package and procedure. Note that in line 3 the dbms=”oracle” means this script will only run when we are connected to an Oracle DB:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" 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.1.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet author="shay" id="createProcedure-example" dbms="oracle">
<createProcedure>create or replace function betwnstr( a_string varchar2, a_start_pos integer, a_end_pos integer ) return varchar2
is
begin
return substr( a_string, a_start_pos, a_end_pos - a_start_pos+1 );
end;</createProcedure>
<createProcedure>create or replace package test_betwnstr as
-- %suite(Between string function)
-- %test(Returns substring from start position to end position)
procedure basic_usage;
end;</createProcedure>
<createProcedure>create or replace package body test_betwnstr as
procedure basic_usage is
begin
ut.expect( betwnstr( '1234567', 2, 5 ) ).to_equal('2345');
end;
end;</createProcedure>
</changeSet>
</databaseChangeLog>

A changeset that adds a record to a table. <rollback> Line has the rollback tag that defines how to do a rollback for this insert:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" 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.1.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet author="shay" id="4">
<insert tableName="department">
<column name="id" value="10" />
<column name="name" value="Sales" />
</insert>
<rollback>delete from department where id=10</rollback>
</changeSet>
</databaseChangeLog>

A few tips about my DevCS project and build setup.

  1. For the sake of simplicity, I loaded the liquibase and JDBC jar files into my git repository — this makes it easy for my build steps to find the files and execute them. I’m guessing you could also use Maven to host those.
  2. I use a password parameter for my build so I don’t need to hardcode the password adding a bit of security to my build. Reference teh parameter in your build with a $ sign — $password
  3. Want to learn more about test automation with ut/PLSQL — check out this blog entry.

Originally published at blogs.oracle.com on October 16, 2017.

--

--

Shay Shmeltzer
Oracle Developers

Director of Product Management — Oracle Cloud Development Tools