Documentation
¶
Overview ¶
Package migrate provides a database migration framework with support for versioned migrations, rollbacks, and migration tracking.
The package is designed to be database-agnostic and can be used as a standalone library for managing database schema changes.
Key features:
- Lexicographical migration ordering by ID
- Transactional migration execution
- Migration rollback support
- Customizable logging
- Duplicate migration detection
Basic Usage:
db, err := sql.Open("sqlite", "database.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
migrator := migrate.New(db, nil)
if err := migrator.AddMigration(&YourMigration{}); err != nil {
log.Fatal(err)
}
if err := migrator.Up(context.Background()); err != nil {
log.Fatal(err)
}
The Up() and Down() methods accept a context.Context parameter for cancellation support. Use context.Background() for non-cancelable operations, or a context with timeout for long-running migrations.
Migration ID Format:
Migrations must have an ID in the format: YYYY_MM_DD_description For example: 2026_03_21_create_users_table
The date part must be a valid calendar date (e.g., February 30 will be rejected).
Index ¶
Constants ¶
const ( DefaultTableName = "schema_migrations" ColumnID = "id" ColumnBatch = "batch" ColumnDescription = "description" ColumnStartedAt = "started_at" ColumnCompletedAt = "completed_at" DirectionUp = "up" DirectionDown = "down" BuiltinMigrationID = "2022_01_01_000_create_schema_migrations" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MigrationInterface ¶
type MigrationInterface interface {
// ID returns the unique identifier for this migration
// Format: YYYYMMDD_NNN (e.g., 20260321_001)
ID() string
// Description returns a human-readable description for the migration
// Example: "Create users table with email index"
Description() string
// Up executes the migration to apply database changes
// Takes context for cancellation support and transaction for atomic operations
Up(ctx context.Context, tx *sql.Tx) error
// Down executes the rollback to revert database changes
// Takes context for cancellation support and transaction for atomic operations
// Should undo exactly what Up() did
Down(ctx context.Context, tx *sql.Tx) error
}
MigrationInterface defines the contract that all migrations must implement
func GetBuiltinMigrations ¶
func GetBuiltinMigrations(tableName string) []MigrationInterface
GetBuiltinMigrations returns the built-in migrations that must always run first
func NewCreateSchemaMigrationsTable ¶
func NewCreateSchemaMigrationsTable(tableName string) MigrationInterface
type MigratorInterface ¶
type MigratorInterface interface {
// AddMigration adds a new migration to the list
AddMigration(migration MigrationInterface) error
// AddMigrations adds multiple migrations to the runner
AddMigrations(migrations []MigrationInterface) error
// Up runs all pending migrations
Up(ctx context.Context) error
// Down rolls back the last migration
Down(ctx context.Context) error
// Status shows migration status
Status(ctx context.Context) error
}
MigratorInterface defines the contract for database migration operations
type Options ¶
type Options struct {
// MigrationTableName is the name of the table used to track applied migrations.
// Defaults to "schema_migrations" if not specified.
MigrationTableName string
// Logger is used for migration logging.
// If nil, logging is disabled.
Logger *slog.Logger
}
Options configures the Migrator behavior