Large Blog image

Database Migrator

Introducing My Golang Database Migrator: The Swiss Army Knife of Migrations

Listen to the Blog and Follow Along with the Transcript

Managing database migrations shouldn't feel like assembling IKEA furniture without instructions. If you've ever wished for a database migration tool that is lightweight, easy to set up, and yet packed with features, you're in luck! Introducing godbmigrator—my Golang database migration package that keeps things simple while offering powerful functionality.

Why Another Migration Tool?

Great question! I built godbmigrator because I wanted something that:

  • Is easy to install – No need to bring in a million dependencies. Just pass your *sql.DB, and you're good to go.
  • Is lightweight – No unnecessary drivers bloating your project.
  • Offers advanced features – File change verification, audit logs, baselines, and more.
  • Works everywhere – Embed it in your application or use it as a command-line tool.
  • Is about to get even better – Soon, it will be Dockerized and able to listen for HTTP requests, making it perfect for CI/CD and local development, without installing Go (Yes, I'm thinking of your DevOps team!).

Features That Make Your Life Easier

Basic Features:

  • Migrate – Apply pending migrations.
  • Rollback – Undo recent migrations.
  • Add new migration file – Create migration files with ease.

Advanced Features:

  • File Change Verification – Ensures SQL files haven't changed since they were applied (unless you force it, you rebel!).
  • Audit Logs – Every migration attempt is recorded, including successes and failures. (Great for when "I didn't touch the database!" excuses start flying.)
  • Baseline Support (Beta) – Save and restore database structure snapshots.
  • Supports SQLite, MySQL, PostgreSQL, and FirebirdSQL (Sorry, Oracle fans, maybe in version 2.0?).

How to Use It

Embed in Your Golang Project

import migrator "github.com/olbrichattila/godbmigrator"

db, _ := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/dbname")
err := migrator.Migrate(db, "", "./migrations", 0)
if err != nil {
 panic("Migration failed: " + err.Error())
}

Use as a CLI Tool

# Install it
 go install github.com/olbrichattila/godbmigrator_cmd/cmd/migrator@latest

# Migrate
migrator migrate

# Rollback
migrator rollback

# Create a migration file
migrator add "add_users_table"

Migration File Naming

The naming convention follows this format:

[date-time]-migrate-[description].sql
[date-time]-rollback-[description].sql

Example:

2024-05-27_19_49_38-migrate.sql
2024-05-27_19_49_38-rollback.sql

Files are executed in ascending order, so timestamps ensure the correct sequence.

What’s Coming Next?

  • Dockerized version – Run it as a service.
  • HTTP API – Trigger migrations remotely.
  • More database support – Firebird baseline support and beyond!

Ready to Migrate Smarter?

Check out the full documentation and source code here: Base Package
CLI Wrapper

Try it out, and let me know how it goes! If it breaks something, well… you should’ve run it in a test environment first. 😉