migrate

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package migrate provides versioned database migrations for SQL backends.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppliedMigration

type AppliedMigration struct {
	Version     int       `json:"version"`
	Description string    `json:"description"`
	AppliedAt   time.Time `json:"applied_at"`
}

AppliedMigration records a migration that has been applied.

type Dialect added in v0.4.0

type Dialect int

Dialect identifies the SQL dialect a Migrator targets. It controls placeholder syntax (`?` vs `$N`) for the migrator's own bookkeeping statements and whether a cross-process advisory lock is available.

const (
	// DialectSQLite uses `?` placeholders and a no-op advisory lock (SQLite is
	// single-writer, so concurrent migrators cannot corrupt each other).
	DialectSQLite Dialect = iota
	// DialectPostgres uses `$N` placeholders and a pg_advisory_lock so multiple
	// processes migrating the same database serialize safely.
	DialectPostgres
)

type Migration

type Migration struct {
	Version     int
	Description string
	Up          string // SQL to apply
	Down        string // SQL to roll back
}

Migration represents a single versioned migration.

type MigrationStatus

type MigrationStatus struct {
	CurrentVersion int                `json:"current_version"`
	Applied        []AppliedMigration `json:"applied"`
	Pending        []Migration        `json:"pending"`
}

MigrationStatus reports the current migration version plus applied and pending migrations.

type Migrator

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

Migrator manages versioned migrations for a SQL database.

func New

func New(db *sql.DB, opts ...Option) *Migrator

New creates a new Migrator for the given database connection. Without options it targets SQLite; pass WithDialect(DialectPostgres) for PostgreSQL.

func (*Migrator) Add

func (m *Migrator) Add(version int, description, up, down string) *Migrator

Add registers a migration. Migrations are applied in version order.

func (*Migrator) Migrate

func (m *Migrator) Migrate(ctx context.Context) error

Migrate applies all pending migrations. Under Postgres it holds a pg_advisory_lock for the duration so concurrent migrators serialize.

func (*Migrator) Rollback

func (m *Migrator) Rollback(ctx context.Context) error

Rollback reverts the last applied migration. Like Migrate it pins a single connection, holds the advisory lock for the duration (Postgres), and runs the Down SQL together with the version-table delete inside one transaction, so a partial failure never leaves the _migrations table inconsistent.

func (*Migrator) Status

func (m *Migrator) Status(ctx context.Context) (*MigrationStatus, error)

Status returns the current migration version and the applied/pending lists.

type Option added in v0.4.0

type Option func(*Migrator)

Option configures a Migrator.

func WithDialect added in v0.4.0

func WithDialect(d Dialect) Option

WithDialect sets the SQL dialect. The default is DialectSQLite.

Jump to

Keyboard shortcuts

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