Mig is a lightweight, flexible database migration tool for PostgreSQL, designed to help you manage your database schema changes with ease.
- Simple Configuration: YAML-based setup for quick deployment
- Transactional Migrations: Safe execution with transaction support (and optional opt-out)
- Flexible CLI: Intuitive commands for creating and managing migrations
- Docker Support: Ready-to-use with Docker Compose for local development
- Migration History: Tracks all executed migrations with timestamps
- Organized Structure: Clear file-naming convention for easy management
- Go 1.24 or later (for development)
- PostgreSQL database
- Docker (optional, for containerized development)
# Clone the repository
git clone https://github.com/arthurdotwork/mig.git
cd mig
# Build the binary
go build -o mig ./cmd/mig# Start PostgreSQL with Docker Compose
docker-compose up -dCreate a new migrations environment in your project:
./mig initThis will create:
- A default
mig.yamlconfiguration file - A
migrationsdirectory - An initial sample migration
The default mig.yaml looks like this:
database:
host: localhost
port: 5432
name: postgres
user: postgres
password: postgres
sslmode: disable
migrations:
directory: migrationsYou can override database configuration using environment variables:
DATABASE_HOSTDATABASE_PORTDATABASE_NAMEDATABASE_USERDATABASE_PASSWORDDATABASE_SSLMODE
Create a new migration file:
./mig create add_users_tableThis creates a timestamped SQL file in your migrations directory:
-- Migration: add_users_table
-- Created at: 2025-04-06 14:30:00
--
-- Note:
-- Add "-- disable-tx" anywhere in this file to disable transaction wrapping.
-- Your SQL goes hereApply the next pending migration:
./mig upApply all pending migrations:
./mig up-allCheck migration status:
./mig statusMigration files follow a specific naming convention:
YYYY_MM_DD_HH_MM_SS_name.sql
For example:
2025_04_06_14_30_00_add_users_table.sql
By default, migrations run inside a transaction. If you need to execute statements that can't run in a transaction (like creating an index concurrently), add this comment at the top of your migration file:
-- disable-tx
CREATE INDEX CONCURRENTLY idx_users_email ON users(email);Migrator version 0.1.0
Usage:
mig [options] <command> [arguments]
Options:
-config string
Path to the configuration file (default "mig.yaml")
-log-level string
Log level (debug, info, warn, error, fatal) (default "info")
-version
Show version information
Commands:
init Initialize the migration environment
create Create a new migration
up Apply the next pending migration
up-all Apply all pending migrations
status Show the status of migrations
mig init [-dir migrations]
-dir: Path to the migrations directory (default:migrations)
mig create migration_name
mig status
Shows information about applied and pending migrations.
# Run all tests
go test ./...
# Run tests with code coverage
go test -cover ./...Mig includes GitHub Actions workflows for:
- Linting with golangci-lint
- Running tests against PostgreSQL
- Automatic tag creation based on CHANGELOG updates
Contributions are welcome! Please feel free to submit a Pull Request.
- Update the CHANGELOG.md when making changes
- Add tests for new functionality
- Ensure all tests pass before submitting PRs
This project is licensed under the MIT License - see the LICENSE file for details.