Liquibase Template
Purpose and Scope
This project provides a standardized template and a set of tools for managing and applying schema changes in PostgreSQL databases using Liquibase. It is designed to facilitate database versioning and controlled migration execution in both local environments and CI/CD pipelines.
Main Features:
- Definition of database changes (DDL and DML) through changelog files in YAML format.
- Execution of migrations in local development environments against a PostgreSQL instance.
- Integration with CI/CD pipelines (Jenkins and GitLab CI) to automate migration application.
- Deployment of migrations in Docker containers, facilitating execution in environments such as Google Kubernetes Engine (GKE) and Google Cloud Run.
- Secure connection to Google Cloud SQL instances using Cloud SQL Proxy.
The scope covers everything from creating and modifying tables to loading initial or configuration data.
Architecture
The workflow and main components of this Liquibase template are:
- Change Development: Developers define schema or data changes in
*.yamlfiles following Liquibase syntax. These files are organized in a directory structure (database/changelog/tables/,database/changelog/data/). - Local Testing: Changes can be tested locally by running the Liquibase CLI against a PostgreSQL instance configured for development.
- Version Control: Changelog files are versioned in a Git repository.
- Docker Image Build: A
Dockerfileis provided to build an image that contains:- Liquibase.
- The project's changelog scripts.
- Cloud SQL Proxy (to connect to Google Cloud SQL instances).
- Continuous Integration / Continuous Deployment (CI/CD):
- Jenkins: A
Jenkinsfiledefines a pipeline (liquibase-migration) that presumably builds the Docker image and runs it to apply migrations in the target environment.
- Jenkins: A
- GitLab CI: A
.gitlab-ci.ymlfile integrates with LATAM pipelines to deploy changes, specifically for Cloud Run ($LATAM_PIPELINE_CLOUD_RUN_DEPLOY).
- Target Environment Execution (GKE/Cloud Run):
- The Docker image runs as a Pod in GKE (defined in
deploy/gke/deployment.yaml.erb) or as a service in Cloud Run (defined indeploy/run/*.yaml.erb). - Inside the container, Cloud SQL Proxy establishes a secure connection to the database.
- Liquibase runs the
validate,releaseLocks, andupdatecommands against the target database, applying pending changes.
- The Docker image runs as a Pod in GKE (defined in
This approach ensures that database changes are consistent, versioned, and applied in an automated and secure manner across different environments.
Technologies and Dependencies
- Liquibase: Main tool for database migration management.
- PostgreSQL: Relational database management system for which the changelogs are designed.
- Docker: For containerizing the Liquibase application and its dependencies.
- YAML: Format used to write Liquibase changelogs.
- Google Cloud SQL Proxy: For secure connections to PostgreSQL instances in Google Cloud.
- Jenkins: CI/CD system used to orchestrate deployments (via
Jenkinsfile). - GitLab CI: CI/CD system used to orchestrate deployments to Cloud Run (via
.gitlab-ci.yml). - Google Kubernetes Engine (GKE): Container orchestration platform, one of the possible deployment environments.
- Google Cloud Run: Serverless platform for running containers, another possible deployment environment.
- Bash/Shell: Used for execution scripts in the
Dockerfileand in local instructions. - Red Hat Universal Base Image (UBI) 8: Base image for the Dockerfile (
redhat_ubi_8_liquidbase_4).
Local Setup
Follow these steps to run Liquibase migrations in your local environment.
Prerequisites
- Liquibase CLI: Must be installed and accessible in your PATH. You can find installation instructions here.
- PostgreSQL JDBC Driver: Download the JDBC driver for PostgreSQL (a
.jarfile) and place it in a location accessible to Liquibase (e.g., in alib/folder within the project, or in thelibdirectory of your Liquibase installation). The originalreadme.mdsuggests a path such as../lib/postgresql.jarrelative to thedatabasefolder. - PostgreSQL Instance: You need a running and accessible PostgreSQL instance.
Setup Steps
- Create project:
- Go to: https://app.getport.io/self-serve
- Select Liquibase scaffolding
- Select GKE deployment
- Select Sample_data
- If you want to push the project to GitLab, select Push to GitLab and register the GitLab group id
🔖 Note
The component name MUST ALWAYS start with liquibase, for example: liquibase-migration-my-component-name
- Local execution
Install Liquibase
Mac
brew install liquibase
Linux
Installation instructions here
Windows
Installation instructions here
Configure database
#Create a role
CREATE ROLE newuser WITH LOGIN PASSWORD 'password';
ALTER ROLE newuser CREATEDB;
#with the newuser create a new database
create database template;
#Connect to the database
\connect template
#Create the schmea
CREATE SCHEMA "template" AUTHORIZATION newuser;
#Set default schemma to template
SET search_path TO template;
SHOW search_path;
#list tables
\dt
Create table
#Export variables
export DATABASE_USERNAME=newuser
export DATABASE_PASSWORD=password
export DATABASE_URL=jdbc:postgresql://127.0.0.1:5432/template?currentSchema=template
Run Liquibase
cd liquibase-migration/database
liquibase --classpath=../lib/postgresql.jar --changeLogFile=changelog-master.yaml --url=${DATABASE_URL} \
--username=${DATABASE_USERNAME} --password=${DATABASE_PASSWORD} --logLevel=${LOG_LEVEL:-info} update
Unlock Database
liquibase --classpath=../lib/postgresql.jar --changeLogFile=changelog-master.yaml --url=${DATABASE_URL} \
--username=${DATABASE_USERNAME} --password=${DATABASE_PASSWORD} --logLevel=${LOG_LEVEL:-info} releaseLocks