migrate

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package migrate applies versioned SQL migrations, in the file format and with the version table that goose uses — so a project that has been migrating with the goose binary can switch to `dbc migrate` without rewriting a single file or touching its goose_db_version table.

A migration file is NNN_name.sql with two annotated sections:

-- +goose Up
CREATE TABLE users (...);

-- +goose Down
DROP TABLE users;

Statements are split on semicolons by the same scanner the editor uses, so a $$-quoted function body is one statement without help. For anything the scanner cannot know is one statement, the goose escape hatch still works:

-- +goose StatementBegin
... verbatim, semicolons and all ...
-- +goose StatementEnd

`-- +goose NO TRANSACTION` at the top runs the file's statements outside a transaction, for DDL that refuses to run inside one (CREATE INDEX CONCURRENTLY, say).

Index

Constants

View Source
const VersionTable = "goose_db_version"

VersionTable is the bookkeeping table. The name and columns are goose's, so a database goose has been migrating carries straight over: its history is read as-is, and goose could even be pointed back at it later.

Variables

View Source
var (

	// Postgres: the exact DDL goose uses, minus the now() default which is
	// not needed since tstamp is always written explicitly.
	Postgres = Dialect{Name: "postgres", /* contains filtered or unexported fields */}
	MySQL    = Dialect{Name: "mysql", /* contains filtered or unexported fields */}
	SQLite   = Dialect{Name: "sqlite", /* contains filtered or unexported fields */}
	// bytdb speaks the Postgres dialect but runs DDL outside transactions.
	BytDB = Dialect{Name: "bytdb", /* contains filtered or unexported fields */}
)

Functions

func Create

func Create(dir, name string) (string, error)

Create writes a new empty migration file in dir, named with the current UTC time as goose does (YYYYMMDDHHMMSS), and returns its path. The name is sanitized to something file-system and shell friendly.

Types

type Dialect

type Dialect struct {
	Name string
	// contains filtered or unexported fields
}

Dialect is the little that differs per engine: how to spell the version table, how to spell a placeholder, and whether DDL may run in a transaction.

func DialectFor

func DialectFor(driver string) (Dialect, error)

DialectFor maps a driver name — a config `driver` value or the canonical database/sql driver name the manager resolves it to — to its Dialect.

type Migration

type Migration struct {
	Version int64  // leading number of the file name; the ordering key
	Name    string // file name, e.g. 20170419004813_CreateUsersTable.sql
	Path    string // where it was read from
	Up      []string
	Down    []string
	NoTx    bool // run outside a transaction (-- +goose NO TRANSACTION)
}

Migration is one parsed migration file.

func Load

func Load(dir string) ([]Migration, error)

Load reads every migration in dir, sorted by version. Files that do not look like migrations and subdirectories are ignored, so a stash of ad-hoc SQL beside the migrations does not break the run. Two files sharing a version is an error: with one applied and the other not, status would be a lie.

func ParseFile

func ParseFile(path string) (Migration, error)

ParseFile parses one migration file. Version is left to the caller, which has the file name.

type Migrator

type Migrator struct {

	// AllowMissing lets `up` apply a migration whose version is lower than
	// one already applied. That happens when two branches each add a
	// migration and merge; goose refuses by default because the lower one was
	// not tested on top of the higher one, and so do we.
	AllowMissing bool

	// Log receives a line per migration applied or rolled back. Nil is silent.
	Log func(string)
	// contains filtered or unexported fields
}

Migrator runs a set of migrations against one database.

func New

func New(dbh *sql.DB, d Dialect, dir string) (*Migrator, error)

New builds a Migrator over dbh for the migrations in dir.

func NewWith

func NewWith(dbh *sql.DB, d Dialect, migs []Migration) *Migrator

NewWith builds a Migrator from already-parsed migrations; they are sorted by version here so callers need not.

func (*Migrator) Down

func (m *Migrator) Down(ctx context.Context) ([]Migration, error)

Down rolls back the most recently applied migration (by version), and returns it.

func (*Migrator) DownTo

func (m *Migrator) DownTo(ctx context.Context, target int64) ([]Migration, error)

DownTo rolls back every applied migration with version > target, newest first. DownTo(0) undoes everything.

func (*Migrator) Migrations

func (m *Migrator) Migrations() []Migration

Migrations returns the loaded migrations in version order.

func (*Migrator) Redo

func (m *Migrator) Redo(ctx context.Context) error

Redo rolls back the latest migration and applies it again — the quickest way to iterate on a migration being written.

func (*Migrator) Statuses

func (m *Migrator) Statuses(ctx context.Context) ([]Status, error)

Statuses reports every loaded migration with its applied state, in version order — the `status` command.

func (*Migrator) Up

func (m *Migrator) Up(ctx context.Context) ([]Migration, error)

Up applies every pending migration in version order and returns the ones it applied.

func (*Migrator) UpByOne

func (m *Migrator) UpByOne(ctx context.Context) ([]Migration, error)

UpByOne applies just the next pending migration.

func (*Migrator) UpTo

func (m *Migrator) UpTo(ctx context.Context, target int64) ([]Migration, error)

UpTo applies pending migrations with version <= target; a negative target means all of them. It stops at the first failure, leaving that migration's transaction rolled back and the earlier ones committed.

func (*Migrator) Version

func (m *Migrator) Version(ctx context.Context) (int64, error)

Version returns the highest applied version, 0 when none is.

type Status

type Status struct {
	Migration
	Applied   bool
	AppliedAt string // as the database rendered it; empty when pending
}

Status is one migration's state as reported by `status`.

Jump to

Keyboard shortcuts

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