migrate

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package migrate applies SQL migrations to SQLite or PostgreSQL. Write migrations as files named <version>_<name>.sql, embed them, and apply them when the app starts:

//go:embed migrations/*.sql
var migrations embed.FS

app, err := ternApp(kit.WithPlugins(migrate.Plugin(db, migrations)))

`tern migrate new add users` creates a file with a timestamp version. A file may have an "-- +down" section that undoes it, for Down:

CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);

-- +down
DROP TABLE users;

Each migration runs in a transaction and is recorded in the tern_migrations table with a checksum, so an applied migration that was edited afterwards is reported instead of silently ignored.

Index

Constants

This section is empty.

Variables

View Source
var ErrChanged = errors.New("migrate: an applied migration was changed")

ErrChanged is returned when an applied migration's file was edited.

Functions

func Plugin

func Plugin(db *sql.DB, fsys fs.FS) kit.Plugin

Plugin applies pending migrations when the server starts, before it accepts requests, and logs each one. A failed migration stops the server.

Types

type CheckOptions added in v1.16.0

type CheckOptions struct {
	// Dialect is the database the app runs on; some changes only lock
	// tables on PostgreSQL.
	Dialect sqldb.Dialect
	// DB, when set, is an empty SQLite database to run the migrations on:
	// up, down and up again, which finds migrations that fail and down
	// sections that don't undo them.
	DB *sql.DB
}

CheckOptions configure Check.

type Level added in v1.16.0

type Level string

Level is how serious a Problem is.

const (
	// Error is a migration that fails, or will on a database with data.
	Error Level = "error"
	// Warning is a change that breaks the version of the app still
	// running while a deploy rolls out, or loses data; a "-- safe:"
	// comment on the statement acknowledges it.
	Warning Level = "warning"
	// Note is worth knowing and needs nothing done.
	Note Level = "note"
)

Levels of problems.

type Migration

type Migration struct {
	Version   int64
	Name      string
	Applied   bool
	AppliedAt time.Time
	// contains filtered or unexported fields
}

Migration is one migration file and whether it was applied.

func Down

func Down(ctx context.Context, db *sql.DB, fsys fs.FS, steps int) ([]Migration, error)

Down undoes the last steps applied migrations with their "-- +down" sections, newest first, and returns them.

func Load

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

Load reads the migrations in fsys: every <version>_<name>.sql file in its root, or in a migrations directory if the root has one.

func Status

func Status(ctx context.Context, db *sql.DB, fsys fs.FS) ([]Migration, error)

Status lists every migration and whether it was applied.

func Up

func Up(ctx context.Context, db *sql.DB, fsys fs.FS) ([]Migration, error)

Up applies every pending migration in version order and returns the ones it applied. It stops at the first error; migrations before it stay applied. Running instances may call it concurrently on Postgres, where an advisory lock serializes them.

type Problem added in v1.16.0

type Problem struct {
	File    string `json:"file"`
	Line    int    `json:"line"`
	Level   Level  `json:"level"`
	Message string `json:"message"`
}

Problem is something Check found in a migration.

func Check added in v1.16.0

func Check(ctx context.Context, fsys fs.FS, opts CheckOptions) ([]Problem, error)

Check looks for migrations in fsys that fail, or break the running app while a deploy rolls out: see Level for what each finding means.

func (Problem) String added in v1.16.0

func (p Problem) String() string

Jump to

Keyboard shortcuts

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