This project uses golang-migrate to manage database migrations.
You need the migrate CLI tool installed locally to create and manage migrations.
macOS (Homebrew):
brew install golang-migrateWindows (Scoop):
scoop install migrateWindows (Chocolatey):
choco install golang-migrateLinux (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/migrateGo install:
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latestUse the Makefile to create new migrations:
make create-migration name=<migration_name>Example:
make create-migration name=create_users_tableThis 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)
Apply all pending migrations:
migrate -path ./migrations -database "postgres://user:password@localhost:5432/dbname?sslmode=disable" upRollback the last migration:
migrate -path ./migrations -database "postgres://user:password@localhost:5432/dbname?sslmode=disable" down 1Rollback all migrations:
migrate -path ./migrations -database "postgres://user:password@localhost:5432/dbname?sslmode=disable" downCheck current migration version:
migrate -path ./migrations -database "postgres://user:password@localhost:5432/dbname?sslmode=disable" versionThe 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" upmigrator/
├── Dockerfile # Container image for running migrations
├── Makefile # Helper commands
├── migrations/ # SQL migration files
└── README.md