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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Liquibase: Database Change Management and Automated Deployments
  • Database Migration tools: Flyway vs Liquibase
  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups

Trending

  • Share Delta Lake Tables Between Databricks and Snowflake
  • Designing Frontend Applications for Failure, Not Just Success
  • Agentic Test Creation: From Plain-Language Requirements to End-to-End Test Cases
  • Architecture Review Boards for Generative AI: A Framework for Governance, Risk Management, and Responsible Adoption

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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.

By 
Shay Shmeltzer
·
Oct. 23, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
75.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Liquibase: Database Change Management and Automated Deployments
  • Database Migration tools: Flyway vs Liquibase
  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook