migrate

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package migrate provides database migration utilities for containerized databases.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMigration

func CreateMigration(dir, name string) (string, error)

CreateMigration creates a new migration file pair (up and down).

func Force

func Force(ctx context.Context, db *sql.DB, version string, opts ...Option) error

Force sets the migration version without running migrations. Useful for initial schema setup.

func Rollback

func Rollback(ctx context.Context, db *sql.DB, dir string, steps int, opts ...Option) error

Rollback rolls back the last N applied migrations.

func Run

func Run(ctx context.Context, db *sql.DB, dir string, opts ...Option) error

Run executes migrations from a directory.

Types

type Config

type Config struct {
	// TableName is the migration table name (default: "schema_migrations")
	TableName string
	// Timeout for each migration step
	Timeout time.Duration
}

Config holds migration configuration.

type Direction

type Direction bool

Direction represents migration direction.

const (
	// Up migration direction
	Up Direction = true
	// Down migration direction
	Down Direction = false
)

type Migration

type Migration struct {
	Version   string
	Name      string
	Direction Direction
}

Migration represents a single migration file.

func ReadDirectionalMigrations

func ReadDirectionalMigrations(dir string, direction Direction) ([]Migration, error)

ReadDirectionalMigrations reads only up or down migrations from a directory.

func ReadMigrations

func ReadMigrations(dir string) ([]Migration, error)

ReadMigrations reads migration files from a directory. Expects files named: {version}_{name}.up.sql or {version}_{name}.down.sql Only .up.sql files are included by default (use ReadDirectionalMigrations for both).

type Option

type Option func(*Config)

Option applies configuration to Config.

func WithTableName

func WithTableName(name string) Option

WithTableName sets the migration table name.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the timeout for each migration step.

type Status

type Status struct {
	Version   string
	Name      string
	Applied   bool
	AppliedAt *time.Time
}

Status represents the status of a migration.

func GetStatus

func GetStatus(ctx context.Context, db *sql.DB, dir string, opts ...Option) ([]Status, error)

GetStatus returns the status of all migrations in a directory.

Jump to

Keyboard shortcuts

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