DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Introduction to Liquibase and Managing Your Database Source Code

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.

Shay Shmeltzer user avatar by
Shay Shmeltzer
·
Oct. 23, 17 · Tutorial
Like (3)
Save
Tweet
Share
73.06K Views

Join the DZone community and get the full member experience.

Join For Free

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.
Database Liquibase

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything
  • What Is API-First?
  • What Is JavaScript Slice? Practical Examples and Guide
  • Introduction to Container Orchestration

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: