migration

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// StatusUp represents an up migration status
	StatusUp = "up"
	// StatusDown represents a down migration status
	StatusDown = "down"
	// StatusSkip represents a skipped migration status
	StatusSkip = "skip"
	// StatusPending represents a pending migration status
	StatusPending = "pending"
)
View Source
const (
	MarkUp   = "-- Migrate:UP"
	MarkDown = "-- Migrate:DOWN"
	// DateLayout defines the layout for migration filename
	DateLayout   = "20060102-150405"
	PrefixFormat = "YYYYMMDD-HHMMSS"
)

Variables

This section is empty.

Functions

func CreateMigration

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

CreateMigration creates a new migration file with the specified name

func CreateMigrations

func CreateMigrations(migrationsDir string, names []string) ([]string, error)

CreateMigrations creates multi migration file with the specified names

func IsApplied

func IsApplied(db *database.DB, version string) (bool, string, error)

IsApplied checks if a specific migration has been applied(status=up)

func SaveRecord

func SaveRecord(db *database.DB, version, status string, tx *sql.Tx) error

SaveRecord records a migration in the database

  • status=up: insert a new record
  • status=down: update the record

func StatusText

func StatusText(status string) string

StatusText returns the text representation of a migration status

Types

type Executor

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

Executor handles the execution of migrations

func NewExecutor

func NewExecutor(db *database.DB, verbose bool) *Executor

NewExecutor creates a new migration executor

func (*Executor) ExecuteDown

func (e *Executor) ExecuteDown(migration *Migration) error

ExecuteDown executes the DOWN part of a migration

func (*Executor) ExecuteUp

func (e *Executor) ExecuteUp(migration *Migration) error

ExecuteUp executes the UP part of a migration

type FilenameInfo

type FilenameInfo struct {
	Time time.Time // parsed time from Date field
	Date string    // eg: 20251105-102430
	Name string    // eg: add-age-index
}

FilenameInfo represents the information extracted from a migration filename

eg: 20251105-102430-add-age-index.sql
=> {Time: time.Time{}, Date: "20251105-102430", Name: "add-age-index"}

type Migration

type Migration struct {
	FileName string // filename as version
	FilePath string // full path
	// Contents of migration file
	Contents string
	// time from filename
	Timestamp time.Time
	// Version same as filename
	Version string
	// UpSection UP section contents
	UpSection   string
	DownSection string
}

Migration represents a single migration file

func FindMigrations

func FindMigrations(migrationsDir string, recursive bool) ([]*Migration, error)

FindMigrations finds all migration files in the specified directory, and returns them sorted by timestamp

  • migrationsDir: allow multiple directories separated by comma

func MigrationsFrom

func MigrationsFrom(migPath string, files []string) ([]*Migration, error)

MigrationsFrom creates migrations from a list of files

func NewMigration

func NewMigration(filePath string) (*Migration, error)

NewMigration creates a new Migration instance from a file path

func ParseFile

func ParseFile(filePath string) (*Migration, error)

ParseFile parses a migration file to extract UP and DOWN sections

func (*Migration) IsBefore added in v0.2.0

func (m *Migration) IsBefore(other *Migration) bool

IsBefore 判断当前迁移文件是否早于指定迁移文件

func (*Migration) Parse

func (m *Migration) Parse() error

Parse reads migration file and parse it contents.

func (*Migration) ParseContents

func (m *Migration) ParseContents() error

ParseContents parses migration file contents, extracting UP and DOWN sections

func (*Migration) ResetContents

func (m *Migration) ResetContents()

ResetContents 重置迁移文件内容字段

type Record

type Record struct {
	// is migration filename
	Version   string    `db:"version"`
	AppliedAt time.Time `db:"applied_at"`
	// up, skip, down.
	Status string `db:"status"`
}

Record represents a record in the database migrations table

func GetAppliedSortedByVersion added in v0.2.2

func GetAppliedSortedByVersion(db *database.DB, limit int) ([]Record, error)

GetAppliedSortedByVersion returns applied migrations sorted by version (most recent first)

func GetMigrationsStatus

func GetMigrationsStatus(db *database.DB, allMigrations []*Migration) ([]Record, error)

GetMigrationsStatus retrieves the status of all migrations

func NewRecord

func NewRecord(version string, status string) *Record

NewRecord creates a new migration record

func (*Record) SetStatus

func (mr *Record) SetStatus(status string)

SetStatus updates the status of the migration record

Jump to

Keyboard shortcuts

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