migration

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

包 migration 提供数据库 Schema 迁移管理能力,支持 PostgreSQL、 MySQL 与 SQLite 三种数据库,基于 golang-migrate 实现。

概述

本包通过 embed.FS 内嵌各数据库方言的 SQL 迁移文件,结合 golang-migrate 引擎实现版本化的 Schema 变更管理。支持正向迁移、 回滚、按步执行、跳转到指定版本以及强制设置版本号等操作。

核心接口与类型

  • Migrator:迁移器接口,定义 Up/Down/DownAll/Steps/Goto/Force/ Version/Status/Info/Close 等完整操作集。
  • DefaultMigrator:Migrator 的默认实现,封装 golang-migrate 实例 与数据库连接管理。
  • Config:迁移配置,包含数据库类型、连接 URL、迁移表名与锁超时。
  • DatabaseType:数据库类型枚举(postgres/mysql/sqlite)。
  • MigrationStatus / MigrationInfo:迁移状态与摘要信息。
  • CLI:命令行交互层,封装 Migrator 提供格式化输出。

主要能力

  • 多数据库支持:通过 DatabaseType 与内嵌 SQL 文件自动适配方言。
  • 工厂函数:NewMigratorFromConfig / NewMigratorFromDatabaseConfig / NewMigratorFromURL 支持从不同配置源快速创建迁移器。
  • CLI 集成:CLI 类型提供 RunUp/RunDown/RunStatus/RunInfo 等 面向终端的格式化操作。
  • 辅助工具:ParseDatabaseType 解析类型字符串,BuildDatabaseURL 按方言拼接连接 URL。

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