API Reference

golang-migrate

Database migration CLI and Go library supporting many databases with versioned up and down migration files.

Official Documentation ↗
CompatibilityThis documentation covers version v4. Check the official docs for the latest API surface.

Installation

gogo install github.com/golang-migrate/migrate@latest

Methods

Overview

golang-migrate is a database migration CLI and Go library supporting many databases with versioned up and down migration files. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • migrate up — Applies all pending migrations, or the given number of steps.
  • migrate.New — Library constructor that wires a migration source to a database driver.
  • migrate create — Creates paired up and down migration files with sequential version numbers.

Example

migrate -path ./migrations -database "$DATABASE_URL" up
migrate -path ./migrations -database "$DATABASE_URL" version

Notes

A failed migration marks the database dirty, blocking further runs until resolved with force. Always ship a matching down file.

Verification

Run up, confirm the version reported by migrate matches the newest file, then run down one step and confirm the version decreases.

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://cdn.jsdelivr.net/gh/golang-migrate/migrate@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:56.192Z.

FAQ

How do I fix a dirty migration state?
Investigate the partial change, then run migrate force with the last good version to reset tracking before retrying.
Where is the schema version stored?
In a schema_migrations table created by the database driver, holding the current version and a dirty flag.
What are the up and down files for?
Each version has an up file to apply and a down file to revert, allowing controlled rollbacks.