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:
migrator := migrate.New(db, "postgres")
migrator.AddMigration(&MyMigration{})
if err := migrator.Up(); err != nil {
log.Fatal(err)
}
Index ¶
Constants ¶
View Source
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 ¶
func NewCreateSchemaMigrationsTable ¶
func NewCreateSchemaMigrationsTable(tableName string) *createSchemaMigrationsTable
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 transaction for atomic operations
Up(tx *sql.Tx) error
// Down executes the rollback to revert database changes
// Should undo exactly what Up() did
Down(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
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() error
// Down rolls back the last migration
Down() error
// Status shows migration status
Status() 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
Source Files
¶
Click to show internal directories.
Click to hide internal directories.