migrations

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package migrations provides a file-based migration engine for GORM.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(m Migration)

Register adds a migration to the global registry. Called from migration files (typically in init()).

func ResetRegistry

func ResetRegistry()

ResetRegistry clears all registered migrations. Used in tests only.

Types

type Migration

type Migration struct {
	Version string
	Up      MigrationFunc
	Down    MigrationFunc
}

Migration represents a single migration with up and down functions.

type MigrationFunc

type MigrationFunc func(db *gorm.DB) error

MigrationFunc is a function that performs a migration step.

type MigrationStatus

type MigrationStatus struct {
	Version string
	Applied bool
	Batch   int
}

MigrationStatus represents the status of a single migration.

type Migrator

type Migrator struct {
	DB *gorm.DB
}

Migrator manages database migrations.

func NewMigrator

func NewMigrator(db *gorm.DB) (*Migrator, error)

NewMigrator creates a Migrator and ensures the schema_migrations table exists.

func (*Migrator) Rollback

func (m *Migrator) Rollback() (int, error)

Rollback undoes the last batch of applied migrations. Returns the number of migrations rolled back.

func (*Migrator) Run

func (m *Migrator) Run() (int, error)

Run applies all pending migrations in version order. Returns the number of migrations applied.

func (*Migrator) Status

func (m *Migrator) Status() ([]MigrationStatus, error)

Status returns the status of all registered migrations.

type SchemaMigration

type SchemaMigration struct {
	ID        uint   `gorm:"primaryKey"`
	Version   string `gorm:"size:255;uniqueIndex;not null"`
	Batch     int    `gorm:"not null"`
	AppliedAt time.Time
}

SchemaMigration tracks applied migrations in the database.

Jump to

Keyboard shortcuts

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