migrate

package
v0.8.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: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChecksumContent

func ChecksumContent(content string) string

ChecksumContent returns the SHA-256 hex digest of the given content string.

func ForceLockForTest added in v0.5.0

func ForceLockForTest(ctx context.Context, drv driver.Driver) error

ForceLockForTest acquires the SQLite/libSQL sentinel lock out of band. Test-only.

func ForceUnlockForTest added in v0.5.0

func ForceUnlockForTest(ctx context.Context, drv driver.Driver) error

ForceUnlockForTest releases the SQLite/libSQL sentinel lock. Test-only.

func WriteMigration

func WriteMigration(dir, name, upSQL, downSQL string) (string, error)

WriteMigration creates a new migration file pair in the given directory. It returns the generated version string (timestamp in YYYYMMDDHHmmss format).

Types

type LintResult

type LintResult struct {
	Version  string
	Name     string
	Expected string
	Actual   string
}

LintResult reports a migration file whose content has changed after it was applied to the database.

type Migration

type Migration struct {
	Version string
	Name    string
	UpSQL   string
	DownSQL string
}

Migration represents a single versioned migration with up and down SQL.

func MergeMigrations added in v0.7.0

func MergeMigrations(fsys ...fs.FS) ([]Migration, error)

MergeMigrations reads every filesystem and returns one set sorted by version, so the migrations of several modules run in the order they were written.

A version that appears in two modules is an error. Two slices generated in the same second, or on two branches, produce the same version, and there is no correct order between them. A failed start is better than a wrong apply order, so the error names both files.

func ParseMigrationDir

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

ParseMigrationDir reads a directory of migration files and returns them sorted by version. Files must follow the naming convention:

<version>_<name>.up.sql
<version>_<name>.down.sql

If the directory does not exist, it returns nil with no error.

func ParseMigrationFS added in v0.7.0

func ParseMigrationFS(fsys fs.FS) ([]Migration, error)

ParseMigrationFS reads migration files from the root of a filesystem and returns them sorted by version. It is the entry point for a module that embeds its own migrations with //go:embed.

type MigrationState

type MigrationState string

MigrationState indicates the lifecycle state of a migration.

const (
	// StateApplied means the migration is in the directory and has been applied
	// with a matching checksum.
	StateApplied MigrationState = "applied"
	// StatePending means the migration is in the directory but has not been applied.
	StatePending MigrationState = "pending"
	// StateModified means the migration has been applied but the file content
	// no longer matches the stored checksum.
	StateModified MigrationState = "modified"
	// StateMissing means the migration was applied but the file no longer exists
	// in the migration directory.
	StateMissing MigrationState = "missing"
)

type MigrationStatus

type MigrationStatus struct {
	Version string
	Name    string
	State   MigrationState
	// Applied is true when the migration has been applied to the database,
	// regardless of whether the file still matches. Kept for backward compatibility.
	Applied bool
}

MigrationStatus represents the state of a single migration.

type Runner

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

Runner executes migrations against a database using the driver interface.

func NewRunner

func NewRunner(drv driver.Driver, dir, dialect string) *Runner

NewRunner creates a Runner that reads migration files from dir and executes them against the provided driver. dialect ("postgres", "sqlite", or "libsql") selects the migration-lock strategy used by Up/Down.

func NewRunnerFS added in v0.7.0

func NewRunnerFS(drv driver.Driver, dialect string, fsys ...fs.FS) *Runner

NewRunnerFS creates a Runner that reads its migrations from one or more filesystems instead of a directory. A module embeds its own migrations with //go:embed, and the sets merge in version order.

func (*Runner) Dialect added in v0.5.0

func (r *Runner) Dialect() string

Dialect returns the dialect string the Runner was constructed with.

func (*Runner) Down

func (r *Runner) Down(ctx context.Context) error

Down rolls back the most recently applied migration.

func (*Runner) Lint

func (r *Runner) Lint(ctx context.Context) ([]LintResult, error)

Lint checks all applied migrations for checksum mismatches, detecting files that were modified after being applied.

func (*Runner) Pending added in v0.5.0

func (r *Runner) Pending(ctx context.Context) ([]Migration, error)

Pending returns the migrations present in the directory that have not yet been applied to the database, in version order. It is the basis for `migrate check`: generating a new migration while unapplied files exist compounds snapshot drift, so callers should surface a clear error.

func (*Runner) Status

func (r *Runner) Status(ctx context.Context) ([]MigrationStatus, error)

Status returns the status of all migrations, including those in the directory and those applied to the database. Each migration is classified as one of: applied, pending, modified, or missing.

func (*Runner) Up

func (r *Runner) Up(ctx context.Context) (int, error)

Up applies every pending migration in order and returns how many it applied.

Up takes the migration lock first, so several replicas that boot at one time serialise: one applies the migrations and the others wait and then find nothing to do.

Do not call Up from two goroutines of one process that share one connection pool. Each waiting call holds a pool connection while it waits for the lock, so a pool smaller than the count of callers starves the winner. Separate processes hold separate pools and are not affected.

Jump to

Keyboard shortcuts

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