migration

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package migration 数据库迁移模块 参考 Laravel migration 功能,支持多数据源分组、MySQL 和 SQLite

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateMigrationFile

func GenerateMigrationFile(dbName, name, destDir string) (string, error)

GenerateMigrationFile 生成迁移文件 dbName 数据源名称 name 迁移名称,如 "create_users" destDir 迁移文件存放的根目录,默认 "database/migrations"

func GetDatabases

func GetDatabases() []string

GetDatabases 获取所有已注册迁移的数据源名称

func GetMigrations

func GetMigrations(dbName string) []migratorEntry

GetMigrations 获取指定数据源的所有已注册迁移

func MigrationName

func MigrationName(m Migrator) string

MigrationName 便捷函数,获取 Migrator 的名称

func Register

func Register(dbName string, m Migrator)

Register 注册迁移,在迁移文件的 init() 中调用 dbName 为数据源名称,如 "main" m 为实现 Migrator 接口的结构体实例 迁移名称自动从结构体类型名推导

Types

type MigrationStatus

type MigrationStatus struct {
	Name  string
	Ran   bool
	Batch int
}

MigrationStatus 单个迁移的状态

type Migrator

type Migrator interface {
	Up() string   // 返回升级 SQL
	Down() string // 返回回滚 SQL
}

Migrator 迁移接口,每个迁移文件实现此接口

type Runner

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

Runner 迁移执行器,绑定到一个数据源连接

func NewRunner

func NewRunner(dbName string, db *database.DB) *Runner

NewRunner 创建迁移执行器 dbName 为数据源名称

func (*Runner) Rollback

func (r *Runner) Rollback() error

Rollback 回滚最后一批迁移

func (*Runner) Run

func (r *Runner) Run() error

Run 执行所有待执行的迁移

func (*Runner) Status

func (r *Runner) Status() ([]MigrationStatus, error)

Status 获取迁移状态

Jump to

Keyboard shortcuts

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