migration

package
v0.0.0-20260608 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectFromEnv

func ConnectFromEnv() (*sql.DB, dialect.Dialect, error)

ConnectFromEnv reads standard environment variables (DB_CONNECTION, DB_DATABASE, etc.) and returns a ready *sql.DB connection along with the matching Dialect.

It does **not** automatically load .env files — callers (especially the CLI generated runner) should call godotenv.Load() first if they want .env support.

func Fresh

func Fresh(db *sql.DB, d dialect.Dialect) error

Fresh drops the migrations table (if it exists) and re-runs all migrations from scratch.

func Register

func Register(name string, m any)

Register registers a migration with the default (global) registry. Generated migration files call this in their init() function. It accepts either a Migration interface or a bare function with signature func(*schema.Builder) error.

func RegisterFunc

func RegisterFunc(name string, up func(builder *schema.Builder) error)

RegisterFunc registers a bare function as a Migration (Down is a no-op).

func Rollback

func Rollback(db *sql.DB, d dialect.Dialect, steps int) error

Rollback rolls back the last N batches of migrations.

func RunPending

func RunPending(db *sql.DB, d dialect.Dialect) error

RunPending runs all pending migrations using the provided DB and Dialect.

func Status

func Status(db *sql.DB, d dialect.Dialect) error

Status prints the current migration status to stdout.

Types

type FuncMigration

type FuncMigration struct {
	UpFunc   func(builder *schema.Builder) error
	DownFunc func(builder *schema.Builder) error
}

FuncMigration wraps a single Up function as a Migration with a no-op Down.

func (*FuncMigration) Down

func (f *FuncMigration) Down(builder *schema.Builder) error

func (*FuncMigration) Up

func (f *FuncMigration) Up(builder *schema.Builder) error

type Migration

type Migration interface {
	Up(builder *schema.Builder) error
	Down(builder *schema.Builder) error
}

Migration represents a single database migration.

type Migrator

type Migrator struct {
	// contains filtered or unexported fields
}

Migrator runs migrations against the database.

func DefaultMigrator

func DefaultMigrator(db *sql.DB, d dialect.Dialect) *Migrator

DefaultMigrator returns a new Migrator wired to the default registry. This is the recommended way to run migrations when using the high-level API.

func NewMigrator

func NewMigrator(db *sql.DB, builder *schema.Builder, registry *Registry) *Migrator

NewMigrator creates a new Migrator instance.

func (*Migrator) Fresh

func (m *Migrator) Fresh() error

Fresh drops the migrations table and re-runs all migrations from scratch. This is equivalent to a clean database state.

func (*Migrator) Migrate

func (m *Migrator) Migrate() error

Migrate runs all pending migrations.

func (*Migrator) MigrateOne

func (m *Migrator) MigrateOne(name string) error

MigrateOne runs a single specific migration (by name) if it is pending.

func (*Migrator) Refresh

func (m *Migrator) Refresh() error

Refresh rolls back ALL batches of migrations, then runs Migrate again.

func (*Migrator) Rollback

func (m *Migrator) Rollback() error

Rollback rolls back the last batch of migrations.

func (*Migrator) RollbackMigration

func (m *Migrator) RollbackMigration(name string) error

RollbackMigration rolls back one specific migration by name.

func (*Migrator) RollbackSteps

func (m *Migrator) RollbackSteps(steps int) error

RollbackSteps rolls back the last N migrations (regardless of batch). This matches Laravel's `migrate:rollback --step=N` behavior.

func (*Migrator) Setup

func (m *Migrator) Setup() error

Setup creates the migrations table if it doesn't exist.

func (*Migrator) Status

func (m *Migrator) Status() error

Status shows all registered migrations and whether they have been run.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds the registered migrations.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new migration registry.

func (*Registry) Register

func (r *Registry) Register(name string, m any)

Register adds a migration to the registry. Accepts either a Migration interface or a bare function with signature func(*schema.Builder) error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL