<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amilafonseka on Medium]]></title>
        <description><![CDATA[Stories by Amilafonseka on Medium]]></description>
        <link>https://medium.com/@amilafonseka?source=rss-4628ae2bf80------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*JC16g7lsYBkHN6mS.jpg</url>
            <title>Stories by Amilafonseka on Medium</title>
            <link>https://medium.com/@amilafonseka?source=rss-4628ae2bf80------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 05:32:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amilafonseka/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How I Automated Database Schema Migrations in a Multi-Tenant Database Model]]></title>
            <link>https://medium.com/better-programming/how-i-automated-database-schema-migrations-in-a-multi-tenant-database-model-using-liquibase-f75f9683ee06?source=rss-4628ae2bf80------2</link>
            <guid isPermaLink="false">https://medium.com/p/f75f9683ee06</guid>
            <category><![CDATA[db-migration]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[jenkins]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Amilafonseka]]></dc:creator>
            <pubDate>Sat, 10 Oct 2020 00:43:15 GMT</pubDate>
            <atom:updated>2020-10-16T15:56:55.859Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>How I used Liquibase, Docker, Gradle, Jenkins, and Shell scripts to automate database schema migrations in a PostgreSQL database</em></h4><figure><img alt="computer screen showing lines of code" src="https://cdn-images-1.medium.com/max/1024/0*IVnh54J60OozcAGs" /><figcaption>Photo by <a href="https://unsplash.com/@_imkiran?utm_source=medium&amp;utm_medium=referral">Sai Kiran Anagani</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In one of the applications that I’m working on at the moment, there was a requirement to automate the DB migrations into the pipeline. Our database model is based on a multi-tenant database design. That means in the same database, you can find many schemas. The database structure will look something like the following.</p><figure><img alt="diagram of multi-tenant model" src="https://cdn-images-1.medium.com/max/781/1*DlKpMFWVp395-Yif_69DwQ.png" /><figcaption><strong>Multi-tenant DB model</strong></figcaption></figure><p>When there’s a need to do a DB migration, we used to run our migration script against every schema, which was a tedious task.</p><p>For example, let’s say that we want to add a new column,<strong> </strong>column_A to table_1. We should write the following SQL statement to do that.</p><pre>Alter table table_1 Add Column column_A varchar(255)</pre><p>Since we use a multi-tenant database design, this same change needs to be done to every schema in the database because table_1 is there in every schema.</p><p>We then started using a PL/PGSQL script like the following to do that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/104037664af6fc43fb8fa2c0d18406d2/href">https://medium.com/media/104037664af6fc43fb8fa2c0d18406d2/href</a></iframe><p>This updates our DB change against every schema in the database. However, this has to be done manually. It would save so much time if we could integrate this with our build pipeline.</p><p>First I thought of implementing a solution manually. But I searched online for a solution that already does the job for me. Then I ended up with something called <a href="https://www.liquibase.org/">Liquibase</a>.</p><h3>Why Use Liquibase Instead of a Manual Solution</h3><p>The main benefit of using Liquibase instead of a manual solution is the support available. Since the Liquibase library has been downloaded over 30 million times all over the world, when an issue arises, a solution can be easily found.</p><p>And Liquibase Community version (free version) supports almost all our needs. As it stands now, there’s no need to use the pro version. One major benefit of the pro version is direct support from Liquibase itself. But since Liquibase is popular among the developer community, most of the time a solution can be found just by looking online.</p><h4>How Liquibase works</h4><p>Liquibase manages something called a changelog file. It looks like the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5693a0cb4dea8e357e1f75d1d8f2e110/href">https://medium.com/media/5693a0cb4dea8e357e1f75d1d8f2e110/href</a></iframe><p>You can add any number of changesets you want to this changelog file as in the above code snippet.</p><p>When you run the update command from Liquibase, it will update the particular DB schema/schemas in your DB. (How we can run the update will be discussed later).</p><p>In the changelog file, the --changeset author.name::tracking_number is important. By this SQL comment, Liquibase identifies that it’s a new changeset. And Liquibase tracks these changesets in a table called databasechangelog. You can find more about the databasechangelog table and how Liquibase works by going to the below link.</p><p><a href="https://www.liquibase.org/get-started/how-liquibase-works">Get Started | How Liquibase Works | Liquibase.org</a></p><p>By comparing with the above table, Liquibase can identify the new changesets that should be run and the ones that already have been executed.</p><p>Apart from the databasechangelog table, there’s another table Liquibase maintains in the DB schema for synchronization purposes. That’s called databasechangeloglock. This table comes in handy if you run migration scripts in parallel against your schemas.</p><p>Liquibase is just a Java library. You can add Liquibase dependency in your build.gradle file if you use <a href="https://gradle.org/">Gradle</a> as your build tool, or in your pom.xml file if you use <a href="http://maven.apache.org/">Maven</a>. I’ll explain how this should be integrated into a Gradle task since we use Gradle as our build tool. Liquibase official documention describes how to integrate Liquibase in Maven if you use Maven as your build tool.</p><p><a href="https://docs.liquibase.com/tools-integrations/maven/home.html">Maven</a></p><h3>How to Run a Liquibase Update as a Gradle Task</h3><p>The following code snippets show how to use Liquibase in a build.gradle file.</p><ol><li>Add Liquibase as a dependency and apply that as a plugin.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83aa246611edcd02b5eaf2a1bee967e6/href">https://medium.com/media/83aa246611edcd02b5eaf2a1bee967e6/href</a></iframe><p>2. Define a Gradle task to run Liquibase update as a part of the build.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56caa849ca106fe1468d8ff65bc2c2ea/href">https://medium.com/media/56caa849ca106fe1468d8ff65bc2c2ea/href</a></iframe><p>You can add any number of DB schemas inside the activities block to run the changesets. Here I have added two schemas. You have to provide the changelog file path, DB schema URL, DB username, and DB password as Liquibase properties. (It’s not a good practice to store the username and password out in the open like I have done here. You should store those credentials somewhere like the AWS Parameter Store. How it’s done will be discussed later as well. )</p><p>When you build the project, the dev task will run, and since the build dependsOn both the dev task and the update tasks, the Liquibase update command will also run as a part of the Gradle task.</p><p>Then by looking at the databasechangelock table in the DB schemas, you can make sure that the changesets have been run. A new row should have been added to the databasechangelock table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zf6rmxS1iGDtOrC9xKcHxg.png" /><figcaption><strong>The Gradle task has been run successfully</strong></figcaption></figure><p>After this is done, the requirements are changed. The requirement insisted this process should be included in the Jenkins pipeline. So I removed the task from the Gradle build and put it in the Jenkinsfile.</p><p>If you don’t already know what a Jenkinsfile is, the following tutorial will give you a good overview.</p><p><a href="https://www.jenkins.io/doc/book/pipeline/jenkinsfile/">Using a Jenkinsfile</a></p><p>Jenkinsfile uses <a href="https://groovy-lang.org/">Groovy</a> to define what should be done in the pipeline. You can define stages in the Jenkinsfile to run-order different processes. For example, you can define one stage to build your code. Then you can define another stage to deploy your code to a server. You can find how to do this by going to the Jenkins official website which I have mentioned above.</p><p>So my first approach was to install Liquibase in the Linux EC2 instance where Jenkins runs and write a shell script to update DB schemas. But it couldn&#39;t be done because this Jenkins setup was created by a third-party vendor, and they were hesitant to give me the pem file to access the particular EC2 instance. Without accessing via ssh to the EC2 machine, I had to find another way to run Liquibase from the Jenkinsfile without installing Liquibase in the machine.</p><p>Then I found out that Liquibase has its own Docker image. You can run any Liquibase command using its Docker image. You should provide the changelog file and the credentials to access the database. It can be done for a <a href="https://www.postgresql.org/">PostgreSQL</a> DB as follows.</p><pre>docker run --rm -v &lt;PATH TO CHANGELOG DIR&gt;:/liquibase/changelog liquibase/liquibase --url=&quot;jdbc:postgresql://&lt;IP OR HOSTNAME&gt;:5432/&lt;DATABASE&gt;?currentSchema=&lt;SCHEMA NAME&gt;&quot; --changeLogFile=/liquibase/changelog/&lt;CHANGELOG NAME ie: &quot;changelog.xml&quot;&gt; --username=&lt;USERNAME&gt; --password=&lt;PASSWORD&gt; update</pre><p>What the above command does is it mounts the &lt;PATH TO CHANGELOG DIR&gt; to the /liquibase/changelog directory inside the Docker container and runs the command from there.</p><p>Now, this updates the changes for one schema. But since my DB model is multi-tenant, this should be run for every schema. I should loop through every schema of the DB. The issue is from where to get the schemas and credentials. One very easy approach is to define the schemas and DB credentials in environment variables in the Jenkinsfile itself. However, keeping DB credentials on the open is not a good practice at all. As I have mentioned earlier, I can easily store those details in the AWS Parameter Store.</p><p>What you have to do is retrieve those details from the AWS Parameter Store using the AWS CLI and loop through the schemas and run the DB migrations using the Liquibase Docker image for every schema. It can be done as follows by using a Shell script.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb52f565f954752df9d01e69ef41a7db/href">https://medium.com/media/cb52f565f954752df9d01e69ef41a7db/href</a></iframe><p>The above code snippet is a part of the actual Jenkinsfile. The first stage pauses the pipeline and asks the user whether they want to run the migrations before running the unit tests. If they confirm that they want to run the migrations, then only the second stage runs. Otherwise, the second stage will be skipped.</p><p>The second stage checks whether the user wants to run the DB migrations and runs the Shell script accordingly. In this, what the withAWS attribute does is it connects to the particular AWS account using the credentials. Then using the AWS CLI, it retrieves the schema list, username, and password from the AWS Parameter Store.</p><p>Then it loops through the schema list and executes the liquibase update command against every schema.</p><p>You can verify whether the updates have been properly run by looking at the databasechangelog after the process completes.</p><p>This is one way you can automate DB migrations in your multi-tenant DB model. Maybe there are other, more efficient ways of doing this. Maybe Liquibase is not a good solution for this. Please let me know your suggestions and thoughts on this in the Comments section. I think this might help someone in some way.</p><p>Finally, since this is my first article on Medium, there can be many things that I should still learn. I hope to write more articles like this in the future, and your thoughts on this would be invaluable.</p><p><em>Thanks for reading!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f75f9683ee06" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/how-i-automated-database-schema-migrations-in-a-multi-tenant-database-model-using-liquibase-f75f9683ee06">How I Automated Database Schema Migrations in a Multi-Tenant Database Model</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>