migration

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package migration provides Goose-backed PostgreSQL schema migration helpers. Package migration 提供基于 Goose 的 PostgreSQL schema 迁移辅助函数。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSchemaAhead reports that the database was migrated beyond the available sources.
	// ErrSchemaAhead 表示数据库版本高于当前可用的迁移源。
	ErrSchemaAhead = errors.New("database schema is newer than available migrations")
	// ErrSchemaBehind reports that migrations remain unapplied.
	// ErrSchemaBehind 表示仍有迁移尚未应用。
	ErrSchemaBehind = errors.New("database schema migrations are pending")
	// ErrTargetInvalid reports that a RunTo target is outside the upward migration range.
	// ErrTargetInvalid 表示 RunTo 的目标超出向上迁移范围。
	ErrTargetInvalid = errors.New("invalid migration target")
)

Functions

func RequireCurrent

func RequireCurrent(ctx context.Context, cfg Config) error

RequireCurrent requires the database to have no pending migrations and not exceed the available version. RequireCurrent 要求数据库没有待执行迁移,且版本不高于当前可用版本。

Types

type Config

type Config struct {
	DB           *sql.DB
	Migrations   fs.FS
	LockID       int64
	GoMigrations []*goose.Migration
}

Config configures Run, RunTo, and RequireCurrent. DB remains owned by the caller. A zero LockID uses Goose's default PostgreSQL advisory lock ID. Config 配置 Run、RunTo 和 RequireCurrent。DB 的生命周期仍由调用方管理。 LockID 为零时使用 Goose 默认的 PostgreSQL advisory lock ID。

type Result

type Result struct {
	Total   int
	Applied int
	Skipped int
}

Result summarizes a successful upward migration run. RunTo excludes sources newer than its target from Total and Skipped. Result 汇总一次成功的向上迁移执行结果。 RunTo 的 Total 和 Skipped 不包含高于目标版本的迁移源。

func Run

func Run(ctx context.Context, cfg Config) (Result, error)

Run applies all pending migrations. It rejects a database newer than the available sources. Run 应用全部待执行迁移;数据库版本高于可用迁移源时拒绝执行。

func RunTo added in v0.2.8

func RunTo(ctx context.Context, cfg Config, target int64) (Result, error)

RunTo applies pending migrations through target. The target cannot be older than the database or newer than the available migrations. RunTo is intended for upgrade tests and maintenance, not normal application startup. RunTo 应用截至 target 的待执行迁移。 目标不能低于数据库版本,也不能高于当前可用迁移。 RunTo 用于升级测试和维护,不用于正常应用启动。

Jump to

Keyboard shortcuts

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