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