Help us shape the future of Go! Take the 2026 Go Developer Survey

migration

package
v1.11.3 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDatabaseURL

func BuildDatabaseURL(dbType DatabaseType, host string, port int, database, username, password, sslMode string) string

BuildDatabaseURL builds a database URL from components

func GetMigrationsPath

func GetMigrationsPath(dbType DatabaseType) string

GetMigrationsPath returns the path to migration files for a database type

Types

type CLI

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

CLI provides command-line interface functionality for migrations

func NewCLI

func NewCLI(migrator Migrator) *CLI

NewCLI creates a new CLI instance

func (*CLI) RunDown

func (c *CLI) RunDown(ctx context.Context) error

RunDown rolls back the last migration

func (*CLI) RunDownAll

func (c *CLI) RunDownAll(ctx context.Context) error

RunDownAll rolls back all migrations

func (*CLI) RunForce

func (c *CLI) RunForce(ctx context.Context, version int) error

RunForce forces the migration version

func (*CLI) RunGoto

func (c *CLI) RunGoto(ctx context.Context, version uint) error

RunGoto migrates to a specific version

func (*CLI) RunInfo

func (c *CLI) RunInfo(ctx context.Context) error

RunInfo shows detailed migration information

func (*CLI) RunStatus

func (c *CLI) RunStatus(ctx context.Context) error

RunStatus shows the status of all migrations

func (*CLI) RunSteps

func (c *CLI) RunSteps(ctx context.Context, n int) error

RunSteps applies or rolls back n migrations

func (*CLI) RunUp

func (c *CLI) RunUp(ctx context.Context) error

RunUp runs all pending migrations

func (*CLI) RunVersion

func (c *CLI) RunVersion(ctx context.Context) error

RunVersion shows the current migration version

func (*CLI) SetOutput

func (c *CLI) SetOutput(w io.Writer)

SetOutput sets the output writer for CLI messages

type Config

type Config struct {
	// DatabaseType specifies the type of database (postgres, mysql, sqlite)
	DatabaseType DatabaseType

	// DatabaseURL is the connection string for the database
	// Format depends on database type:
	// - PostgreSQL: postgres://user:password@host:port/dbname?sslmode=disable
	// - MySQL: user:password@tcp(host:port)/dbname?parseTime=true
	// - SQLite: file:path/to/db.sqlite?mode=rwc
	DatabaseURL string

	// MigrationsPath is the path to migration files (optional, uses embedded by default)
	MigrationsPath string

	// TableName is the name of the migrations table (default: schema_migrations)
	TableName string

	// LockTimeout is the timeout for acquiring migration lock
	LockTimeout time.Duration
}

Config holds the configuration for the migrator

type DatabaseType

type DatabaseType string

DatabaseType represents the type of database

const (
	// DatabaseTypePostgres represents PostgreSQL database
	DatabaseTypePostgres DatabaseType = "postgres"
	// DatabaseTypeMySQL represents MySQL database
	DatabaseTypeMySQL DatabaseType = "mysql"
	// DatabaseTypeSQLite represents SQLite database
	DatabaseTypeSQLite DatabaseType = "sqlite"
)

func ParseDatabaseType

func ParseDatabaseType(s string) (DatabaseType, error)

ParseDatabaseType parses a database type string

type DefaultMigrator

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

DefaultMigrator implements the Migrator interface using golang-migrate

func NewMigrator

func NewMigrator(cfg *Config) (*DefaultMigrator, error)

NewMigrator creates a new migrator instance

func NewMigratorFromConfig

func NewMigratorFromConfig(cfg *appconfig.Config) (*DefaultMigrator, error)

NewMigratorFromConfig creates a new migrator from application configuration

func NewMigratorFromDatabaseConfig

func NewMigratorFromDatabaseConfig(dbCfg appconfig.DatabaseConfig) (*DefaultMigrator, error)

NewMigratorFromDatabaseConfig creates a new migrator from database configuration

func NewMigratorFromURL

func NewMigratorFromURL(dbType, dbURL string) (*DefaultMigrator, error)

NewMigratorFromURL creates a new migrator from a database URL

func (*DefaultMigrator) Close

func (m *DefaultMigrator) Close() error

Close closes the migrator and releases resources

func (*DefaultMigrator) Down

func (m *DefaultMigrator) Down(ctx context.Context) error

Down rolls back the last migration

func (*DefaultMigrator) DownAll

func (m *DefaultMigrator) DownAll(ctx context.Context) error

DownAll rolls back all migrations

func (*DefaultMigrator) Force

func (m *DefaultMigrator) Force(ctx context.Context, version int) error

Force sets the migration version without running migrations

func (*DefaultMigrator) Goto

func (m *DefaultMigrator) Goto(ctx context.Context, version uint) error

Goto migrates to a specific version

func (*DefaultMigrator) Info

Info returns information about the current migration state

func (*DefaultMigrator) Status

func (m *DefaultMigrator) Status(ctx context.Context) ([]MigrationStatus, error)

Status returns the status of all migrations

func (*DefaultMigrator) Steps

func (m *DefaultMigrator) Steps(ctx context.Context, n int) error

Steps applies or rolls back n migrations

func (*DefaultMigrator) Up

func (m *DefaultMigrator) Up(ctx context.Context) error

Up applies all pending migrations

func (*DefaultMigrator) Version

func (m *DefaultMigrator) Version(ctx context.Context) (uint, bool, error)

Version returns the current migration version

type MigrationInfo

type MigrationInfo struct {
	CurrentVersion    uint
	Dirty             bool
	TotalMigrations   int
	AppliedMigrations int
	PendingMigrations int
}

MigrationInfo contains information about the current migration state

type MigrationStatus

type MigrationStatus struct {
	Version   uint
	Name      string
	Applied   bool
	AppliedAt *time.Time
	Dirty     bool
}

MigrationStatus represents the status of a migration

type Migrator

type Migrator interface {
	// Up applies all pending migrations
	Up(ctx context.Context) error

	// Down rolls back the last migration
	Down(ctx context.Context) error

	// DownAll rolls back all migrations
	DownAll(ctx context.Context) error

	// Steps applies or rolls back n migrations
	// Positive n applies migrations, negative n rolls back
	Steps(ctx context.Context, n int) error

	// Goto migrates to a specific version
	Goto(ctx context.Context, version uint) error

	// Force sets the migration version without running migrations
	Force(ctx context.Context, version int) error

	// Version returns the current migration version
	Version(ctx context.Context) (uint, bool, error)

	// Status returns the status of all migrations
	Status(ctx context.Context) ([]MigrationStatus, error)

	// Info returns information about the current migration state
	Info(ctx context.Context) (*MigrationInfo, error)

	// Close closes the migrator and releases resources
	Close() error
}

Migrator defines the interface for database migrations

Jump to

Keyboard shortcuts

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