Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introduction to Liquibase and Managing Your Database Source Code

DZone's Guide to

Introduction to Liquibase and Managing Your Database Source Code

Liquibase is an open-source solution for managing revisions of your database schema scripts. Learn how to use it to sync up your database versions with your app version.

· Database Zone
Free Resource

Find out how Database DevOps helps your team deliver value quicker while keeping your data safe and your organization compliant. Align DevOps for your applications with DevOps for your SQL Server databases to discover the advantages of true Database DevOps, brought to you in partnership with Redgate

In previous posts, 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 database 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 needing 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 and 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 runs 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 sync 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 changeset 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 ten-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:
    <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="0">
            <createtable tablename="department">
                <column name="id" type="int">
                    <constraints nullable="false" primarykey="true">
                </constraints></column>
                <column name="name" type="varchar(50)">
                    <constraints nullable="false">
                </constraints></column>
                <column defaultvalueboolean="true" name="active" type="boolean">
            </column></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:
    <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" dbms="oracle" id="createProcedure-example">
        <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. Line 8 has the rollback tag that defines how to do a rollback for this insert:
    <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="20">
            <column name="name" value="Marketing">
        </column></column></insert>
        <rollback>delete from department where id=20</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 the parameter in your build with a $ sign (i.e. $password).
  3. Want to learn more about test automation with ut/PLSQL? Check out this blog entry.

Align DevOps for your applications with DevOps for your SQL Server databases to increase speed of delivery and keep data safe. Discover true Database DevOps, brought to you in partnership with Redgate

Topics:
database ,tutorial ,liquibase ,source code

Published at DZone with permission of Shay Shmeltzer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}