simplemigrations

package module
v0.0.0-...-43a6f05 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 6 Imported by: 0

README

simplemigrations helps you run ordered database migrations in Go while staying agnostic to the underlying transport. You provide transactional primitives and the package coordinates schema upgrades.

core ideas

  • implement MinimalTx
    • it executes migration statements and records schema versions
  • you can optionally implement Tx and DB for integration with PostgreSQL
    • when present, the library can create schemas and manage retries through MigrateToLatestWithSchema
  • you can also supply a Logger to observe migration progress
    • NopLogger is available when you do not need logging

MigrateToLatest is the general entry point. Use MigrateToLatestWithSchema only with PostgreSQL-backed implementations that expose the additional DB/Tx methods.

getting started

  1. implement MinimalTx (and optionally Tx, DB, Logger)
  2. build a slice of Migration values ordered by version
  3. call MigrateToLatest with your transaction, migrations, and logger of choice

See simplemigrations_test.go for a runnable example showing the expected behaviours.

Documentation

Overview

Package simplemigrations provides helpers for applying ordered migrations against transactional backends.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MigrateToLatest

func MigrateToLatest(ctx context.Context, log Logger, tx MinimalTx, migrations []Migration, freshDB bool) error

MigrateToLatest runs pending migrations within the provided transaction.

func MigrateToLatestWithSchema

func MigrateToLatestWithSchema(ctx context.Context, log Logger, db DB, schema string, temporary bool, migrations []Migration, shouldRetry func(err error) bool) (cleanup func() error, err error)

MigrateToLatestWithSchema runs pending migrations, optionally creating an isolated schema.

func RollbackUnlessCommitted

func RollbackUnlessCommitted(ctx context.Context, log Logger, tx Tx) error

RollbackUnlessCommitted rolls back the transaction unless it has already been committed.

func SetSearchPathTo

func SetSearchPathTo(ctx context.Context, tx MinimalTx, schema string) error

SetSearchPathTo changes the search_path to the given schema within the transaction.

Types

type DB

type DB interface {
	Dialect() Dialect
	Open(ctx context.Context) (Tx, error)
	ExecContext(ctx context.Context, query string, args ...any) error
}

DB opens transactional connections and reports the active dialect.

type Dialect

type Dialect string

Dialect identifies the database dialect supported by simplemigrations.

const (
	// DialectPostgres identifies the PostgreSQL dialect.
	DialectPostgres Dialect = "postgres"
)

type Logger

type Logger interface {
	Debug(ctx context.Context, msg string, args ...any)
	Info(ctx context.Context, msg string, args ...any)
	Warn(ctx context.Context, msg string, args ...any)
}

Logger records diagnostic messages about migration progress.

type Migration

type Migration struct {
	Queries        []string
	Version        int
	VersionComment string
}

Migration describes a single schema change and its metadata.

type MinimalTx

type MinimalTx interface {
	ExecContext(ctx context.Context, query string, args ...any) error
	LatestSchemaVersion(ctx context.Context) (int, error)
	CreateSchema(ctx context.Context, version int, comment string) error
}

MinimalTx executes migration queries and manages schema versions.

type NopLogger

type NopLogger struct{}

NopLogger implements Logger without producing any output.

func (NopLogger) Debug

func (NopLogger) Debug(context.Context, string, ...any)

Debug discards debug messages.

func (NopLogger) Info

func (NopLogger) Info(context.Context, string, ...any)

Info discards informational messages.

func (NopLogger) Warn

func (NopLogger) Warn(context.Context, string, ...any)

Warn discards warning messages.

type Tx

type Tx interface {
	MinimalTx
	Commit() error
	Rollback() error
}

Tx wraps MinimalTx with commit and rollback semantics.

Directories

Path Synopsis
internal
fruitsdbx
Package fruitsdbx stores example fixtures for database-backed tests.
Package fruitsdbx stores example fixtures for database-backed tests.

Jump to

Keyboard shortcuts

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