Migrations
Migrations
Overview
Lemmego’s migration system provides a version-controlled, bidirectional database schema evolution tool. It supports SQLite, MySQL, and PostgreSQL with a fluent schema builder DSL.
Features
- Bidirectional — Up and Down migrations for safe rollbacks
- Schema Builder DSL — Fluent API for creating, altering, and dropping tables
- Multi-Database — SQLite, MySQL, PostgreSQL
- Migration Tracking — Tracks applied migrations in a
schema_migrationstable - Batch Management — Migrations are grouped into batches for rollback
- Savepoints — Each migration runs in a transaction for atomicity
Creating Migrations
Use the CLI to generate a migration file:
lemmego g migration create_users_tableThis creates a timestamped file in internal/migrations/:
// internal/migrations/20250706000001_create_users_table.go
package migrations
import (
"database/sql"
"github.com/lemmego/migration"
)
func init() {
migration.GetMigrator().AddMigration(&migration.Migration{
Version: "20250706000001",
Up: mig_users_up,
Down: mig_users_down,
})
}
func mig_users_up(tx *sql.Tx) error {
schema := migration.Create("users", func(t *migration.Table) {
t.BigIncrements("id")
t.String("name").NotNull()
t.String("email").Unique()
t.Timestamps()
}).Build()
_, err := tx.Exec(schema)
return err
}
func mig_users_down(tx *sql.Tx) error {
return migration.Drop("users")
}Running Migrations
Up (Apply)
lemmego run migrate upOr step by step:
lemmego run migrate up --step=2Down (Rollback)
Rollback the last batch:
lemmego run migrate downOr rollback a specific number of batches:
lemmego run migrate down --step=2Status
Check which migrations are pending:
lemmego run migrate statusCreate from CLI
lemmego run migrate create create_posts_tableHow It Works
- On
Init(), the migrator creates aschema_migrationstable (if it doesn’t exist) - Already-applied migrations are marked as
donein memory - Running
upapplies pending migrations in version order, grouped into a batch - Each migration runs inside a transaction — failure rolls back the individual migration
- Running
downrolls back the last N batches in reverse version order