Migrations
DocumentationMigration Status
Check which migrations have run and their batch numbers using m.Status().
Migration Status
Use m.Status() to inspect the current state of all registered migrations — which ones have run, their batch numbers, and which are still pending.
Using m.Status()
statuses, err := m.Status()
if err != nil {
log.Fatal(err)
}
for _, s := range statuses {
fmt.Printf("Migration: %s | Applied: %v | Batch: %d\n", s.Name, s.Applied, s.Batch)
}m.Status() returns a slice of status entries, one for each registered migration.
Status Output
Each status entry contains:
| Field | Type | Description |
|---|---|---|
Name | string | The migration name as passed to m.Register() |
Applied | bool | Whether the migration has been applied |
Batch | int | The batch number (0 if not yet applied) |
AppliedAt | *time.Time | When the migration was applied (nil if not yet applied) |
Example Output
Given these registered migrations:
m.Register("20240101000001_create_users_table", &migrations.CreateUsersTable{})
m.Register("20240115000001_create_posts_table", &migrations.CreatePostsTable{})
m.Register("20240201000001_add_phone_to_users", &migrations.AddPhoneToUsers{})If the first two have been applied, m.Status() produces:
Migration: 20240101000001_create_users_table | Applied: true | Batch: 1
Migration: 20240115000001_create_posts_table | Applied: true | Batch: 1
Migration: 20240201000001_add_phone_to_users | Applied: false | Batch: 0Complete Example
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq"
"github.com/gopackx/go-migration/pkg/migrator"
"your-project/migrations"
)
func main() {
db, err := sql.Open("postgres", "postgres://user:password@localhost:5432/mydb?sslmode=disable")
if err != nil {
log.Fatal(err)
}
defer db.Close()
m := migrator.New(db)
if err := m.Register("20240101000001_create_users_table", &migrations.CreateUsersTable{}); err != nil {
log.Fatal(err)
}
if err := m.Register("20240115000001_create_posts_table", &migrations.CreatePostsTable{}); err != nil {
log.Fatal(err)
}
if err := m.Register("20240201000001_add_phone_to_users", &migrations.AddPhoneToUsers{}); err != nil {
log.Fatal(err)
}
statuses, err := m.Status()
if err != nil {
log.Fatal(err)
}
fmt.Println("Migration Status:")
fmt.Println("─────────────────────────────────────────────────────")
for _, s := range statuses {
status := "Pending"
if s.Applied {
status = fmt.Sprintf("Applied (Batch %d)", s.Batch)
}
fmt.Printf(" %s — %s\n", s.Name, status)
}
}m.Status() only reports on migrations that are registered with the migrator. If a migration was previously run but is no longer registered, it won't appear in the status output.
What's Next?
- Running Migrations — execute pending migrations
- Rollback — undo migrations when needed