Skip to content
 
 

Latest commit

 

History

42 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Database Migrator

This project uses golang-migrate to manage database migrations.

Prerequisites

You need the migrate CLI tool installed locally to create and manage migrations.

Installation

macOS (Homebrew):

brew install golang-migrate

Windows (Scoop):

scoop install migrate

Windows (Chocolatey):

choco install golang-migrate

Linux (curl):

curl -L https://github.com/golang-migrate/migrate/releases/download/v4.17.1/migrate.linux-amd64.tar.gz | tar xvz
sudo mv migrate /usr/local/bin/migrate

Go install:

go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest

Creating Migrations

Use the Makefile to create new migrations:

make create-migration name=<migration_name>

Example:

make create-migration name=create_users_table

This creates two files in the migrations/ directory:

  • <timestamp>_create_users_table.up.sql - Applied when migrating up
  • <timestamp>_create_users_table.down.sql - Applied when migrating down (rollback)

Running Migrations

Apply all pending migrations:

migrate -path ./migrations -database "postgres://user:password@localhost:5432/dbname?sslmode=disable" up

Rollback the last migration:

migrate -path ./migrations -database "postgres://user:password@localhost:5432/dbname?sslmode=disable" down 1

Rollback all migrations:

migrate -path ./migrations -database "postgres://user:password@localhost:5432/dbname?sslmode=disable" down

Check current migration version:

migrate -path ./migrations -database "postgres://user:password@localhost:5432/dbname?sslmode=disable" version

Docker

The included Dockerfile builds a minimal image with the migrate tool and your migrations:

docker build -t migrator .
docker run migrator -path=/migrations -database="$DATABASE_URL" up

Project Structure

migrator/
├── Dockerfile        # Container image for running migrations
├── Makefile          # Helper commands
├── migrations/       # SQL migration files
└── README.md

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages