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 ¶
var ErrChanged = errors.New("migrate: an applied migration was changed")
ErrChanged is returned when an applied migration's file was edited.
Functions ¶
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 ¶
Down undoes the last steps applied migrations with their "-- +down" sections, newest first, and returns them.
func Load ¶
Load reads the migrations in fsys: every <version>_<name>.sql file in its root, or in a migrations directory if the root has one.
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.