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 ¶
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 ¶
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 ¶
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 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 ¶
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.