migrate

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package migrate applies an app's SQL migrations: the files in its migrations directory, each once, in name order, recorded in the table maxcloud_migrations.

This file is the same in mcloud (internal/migrate) and in the Go SDK (functions/internal/migrate); a test in mcloud keeps the two identical. The TypeScript SDK (packages/functions/src/migrate.ts) does the same thing, and all of them are tested against testdata/migrate.

Index

Constants

View Source
const (
	// Dir is where an app keeps its migrations, next to maxcloud.yaml.
	Dir = "migrations"
	// Table records the applied migrations. It isn't schema-qualified: it
	// is the one the connection's search_path finds (public.maxcloud_migrations
	// on a default connection), and a new one goes in the connection's
	// current schema, next to the app's own tables (a test schema's own).
	Table = "maxcloud_migrations"
	// LockKey is the session advisory lock a run holds while it applies
	// migrations: the ASCII bytes of "maxcloud".
	LockKey int64 = 0x6d6178636c6f7564
)

Variables

View Source
var ErrChanged = errors.New("changed after it was applied")

ErrChanged is wrapped by the error for a migration file that changed after it was applied.

Functions

func Apply

func Apply(ctx context.Context, conn *pgx.Conn, files []File, log func(string)) ([]string, error)

Apply applies the pending migrations in name order, each in its own transaction together with its row in the table, so a migration that fails leaves nothing behind. It refuses to apply anything if an applied migration's file changed. Migrations the database has applied but files doesn't have are left alone. Only one run applies at a time (an advisory lock); when nothing is pending it takes no lock and creates nothing. log, if set, hears what it's doing. It returns the names it applied.

Types

type Entry

type Entry struct {
	Name      string
	State     State
	AppliedAt time.Time // when it's applied
}

Entry is one migration's state.

func Status

func Status(ctx context.Context, conn *pgx.Conn, files []File) ([]Entry, error)

Status compares files with what the database has applied. It changes nothing and takes no lock.

type File

type File struct {
	Name     string // 0001_init.sql
	Checksum string // hex SHA-256 of the file's bytes
	SQL      string
}

File is one migration.

func Load

func Load(fsys fs.FS, dir string) ([]File, error)

Load reads the migrations in dir of fsys: its files ending in .sql, in name order. Other files and subdirectories are left alone. A missing dir is no migrations. Every .sql file's name must be a number, an underscore and a description (0001_init.sql), and the numbers must all have the same width, so that name order is number order.

type State

type State string

State is where a migration stands.

const (
	Applied State = "applied"
	Pending State = "pending"
	// Changed: applied, but the file isn't what was applied any more.
	Changed State = "changed"
	// Missing: applied, but there's no file for it (an older version of
	// the app, or a deleted file).
	Missing State = "missing"
)

Jump to

Keyboard shortcuts

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