migrate

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package migrate applies SQL schema migrations to one SQLite database (PLAN §4, decided instead of goose).

Rules:

  • Files are named NNNN_description.sql; versions start at 1 and have no gaps.
  • Forward only: there are no down migrations. Going back means restoring a backup (myownpki restore).
  • Each migration runs in its own transaction together with its bookkeeping row, so a crash leaves it either fully applied or not at all.
  • The SHA-256 of every applied file is stored. A file that changed after it was applied, or a database migrated by a newer binary, stops everything: the schema history of a CA must not diverge silently.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidSet is returned for a malformed set of migration files.
	ErrInvalidSet = errors.New("invalid migration set")
	// ErrModified means an applied migration file has changed since.
	ErrModified = errors.New("applied migration was modified")
	// ErrNewer means the database has migrations this binary doesn't know.
	ErrNewer = errors.New("database was migrated by a newer version")
)

Functions

func Apply

func Apply(ctx context.Context, db *sql.DB, set []Migration, now func() time.Time) ([]int, error)

Apply runs all pending migrations in order, each in its own transaction, and returns the versions it applied. now supplies applied_at (UTC). db must be the database's single writer connection.

Types

type Migration

type Migration struct {
	Version  int
	Name     string // file name without the version and .sql
	SQL      string
	Checksum string // hex SHA-256 of the file with LF line endings
}

Migration is one SQL file.

func Load

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

Load reads all *.sql files of fsys (its root directory), checks names and numbering and returns them in order. Other files are ignored.

type Querier

type Querier interface {
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}

Querier is satisfied by *sql.DB and *sql.Tx.

type Report

type Report struct {
	Migrations []Status
}

Report is the migration state of a database.

func Check

func Check(ctx context.Context, q Querier, set []Migration) (Report, error)

Check compares the database with the migration set without changing anything. It returns ErrModified or ErrNewer when they don't fit together.

func (Report) Pending

func (r Report) Pending() int

Pending returns the number of migrations not applied yet.

func (Report) Version

func (r Report) Version() int

Version returns the highest applied version (0 = none).

type Status

type Status struct {
	Migration
	Applied   bool
	AppliedAt string // RFC 3339 UTC, empty when pending
}

Status describes one migration in a database.

Jump to

Keyboard shortcuts

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