Back to Blog
Automation / AI

Automating PostgreSQL Database Migrations with Yuniql and GitHub Actions

3 min read
Automating PostgreSQL Database Migrations with Yuniql and GitHub Actions

Automated PostgreSQL Migrations with Yuniql and GitHub Actions

Managing database schema changes can be one of the most challenging aspects of software delivery, especially when working with incremental updates across teams and environments.

In this post, I walk through a real example โ€” a publicly available repository that automates PostgreSQL database migrations using Yuniql with GitHub Actions.
This solution applies native SQL migrations as part of a CI/CD pipeline in a production-ready way.


๐Ÿง  What Is Yuniql?

Yuniql is a migration-based database versioning tool that runs plain SQL scripts (no ORM) and keeps track of applied versions.
It acts like DevOps automation for databases: versioning, sequencing, and applying migrations in a consistent order. :contentReference[oaicite:0]{index=0}


๐Ÿ“‚ Project Structure (from the repo)

The repository shows a clear layout for managing migrations and CI/CD:

.github/workflows/ โ””โ”€โ”€ migrate.yml # GitHub Actions workflow db/migrations/ โ”œโ”€โ”€ _init/ # Setup scripts (run once) โ”œโ”€โ”€ _pre/ # Pre-version hooks โ”œโ”€โ”€ _post/ # Post-version hooks โ”œโ”€โ”€ v0.00/ # Version 0.00: base schema โ”œโ”€โ”€ v0.01/ # Version 0.01: new tables โ””โ”€โ”€ v0.02/ # Version 0.02: updates scripts/ โ”œโ”€โ”€ run-local.sh # Local runner โ””โ”€โ”€ run-local.ps1 docker/ โ””โ”€โ”€ Dockerfile.yuniql

This structure makes migrations:

  • Modular
  • Incremental
  • Easily runnable locally or in CI/CD

๐Ÿš€ Quick Start

Locally

  1. Clone the repo:
    git clone https://github.com/vishaljudoka/yuniql-postgres-github-actions
  2. Start PostgreSQL:
    docker run -d --name postgres \ -e POSTGRES_USER=postgres \ -e POSTGRES_PASSWORD=postgres \ -e POSTGRES_DB=sample_db \ -p 5432:5432 \ postgres:latest
  3. Setup environment:
    cp scripts/.env.example scripts/.env
  4. Run migrations:
    ./scripts/run-local.sh

๐Ÿ›  CI/CD with GitHub Actions

The migrate.yml workflow does the following:

  1. Runs on push to main or manually via workflow dispatch
  2. Checks out code
  3. Installs Yuniql CLI
  4. Runs migrations using environment variables stored as GitHub Secrets

Required repo secrets:

DB_HOST DB_USER DB_PASSWORD DB_NAME

GitHub Actions ensures every push triggers database migration automation.


๐Ÿงฉ Migration Best Practices (Used in This Repo)

โœ” Semantic versioning folders (v0.00, v0.01, etc.)
โœ” Use IF NOT EXISTS in SQL for idempotent behavior
โœ” Separate pre and post scripts for hooks
โœ” Scripts only in version folders are executed in order โ€” ensures safe deployment

Example naming convention:

v0.00/001_create_schema_and_initial_tables.sql v0.01/001_add_orders_tables.sql

๐Ÿงช Why This Matters

โœ” Automates repetitive database work
โœ” Makes changes version controlled
โœ” CI/CD integration adds deployment confidence
โœ” Works for local and cloud PostgreSQL environments


โœจ Next Steps

You can extend this workflow by:

  • Adding tests that run post-migration
  • Supporting rollback scripts
  • Integrating notification on migration failures

๐Ÿงพ Summary

This project demonstrates a practical, automated approach to handling database migrations that fits well in modern DevOps pipelines. Using Yuniql with GitHub Actions enables reliable and repeatable updates every time you push code.

If youโ€™d like, I can also tailor this content further for SEO or add screenshots from the workflow.

Tags

#postgresql#yuniql#github-actions#database#ci/cd#migrations