package migrate

import "github.com/pressly/goose/v3/internal/migrate"

Package migrate defines a Migration struct and implements the migration logic for executing Go and SQL migrations.

Lastly, SQL migrations are lazily parsed. This means that the SQL migration is parsed the first time it is executed.

Index

Functions

func ParseSQL

func ParseSQL(fsys fs.FS, debug bool, migrations []*Migration) error

ParseSQL parses all SQL migrations in BOTH directions. If a migration has already been parsed, it will not be parsed again.

Important: This function will mutate SQL migrations.

Types

type Go

type Go struct {
	// We used an explicit bool instead of relying on a pointer because registered funcs may be nil.
	// These are still valid Go and versioned migrations, but they are just empty.
	//
	// For example: goose.AddMigration(nil, nil)
	UseTx bool

	// Only one of these func pairs will be set:
	UpFn, DownFn func(context.Context, *sql.Tx) error
	// -- or --
	UpFnNoTx, DownFnNoTx func(context.Context, *sql.DB) error
}

func (*Go) IsEmpty

func (g *Go) IsEmpty(direction bool) bool

type Migration

type Migration struct {
	// Fullpath is the full path to the migration file.
	//
	// Example: /path/to/migrations/123_create_users_table.go
	Fullpath string
	// Version is the version of the migration.
	Version int64
	// Type is the type of migration.
	Type MigrationType
	// A migration is either a Go migration or a SQL migration, but never both.
	//
	// Note, the SQLParsed field is used to determine if the SQL migration has been parsed. This is
	// an optimization to avoid parsing the SQL migration if it is never required. Also, the
	// majority of the time migrations are incremental, so it is likely that the user will only want
	// to run the last few migrations, and there is no need to parse ALL prior migrations.
	//
	// Exactly one of these fields will be set:
	Go *Go
	// -- or --
	SQLParsed bool
	SQL       *SQL
}

func (*Migration) GetSQLStatements

func (m *Migration) GetSQLStatements(direction bool) ([]string, error)

func (*Migration) IsEmpty

func (m *Migration) IsEmpty(direction bool) bool

func (*Migration) Run

func (m *Migration) Run(ctx context.Context, tx *sql.Tx, direction bool) error

Run runs the migration inside of a transaction.

func (*Migration) RunConn

func (m *Migration) RunConn(ctx context.Context, conn *sql.Conn, direction bool) error

RunConn runs the migration without a transaction using the provided connection.

func (*Migration) RunNoTx

func (m *Migration) RunNoTx(ctx context.Context, db *sql.DB, direction bool) error

RunNoTx runs the migration without a transaction.

func (*Migration) UseTx

func (m *Migration) UseTx() bool

type MigrationType

type MigrationType int
const (
	TypeGo MigrationType = iota + 1
	TypeSQL
)

func (MigrationType) String

func (t MigrationType) String() string

type SQL

type SQL struct {
	UseTx          bool
	UpStatements   []string
	DownStatements []string
}

func (*SQL) IsEmpty

func (s *SQL) IsEmpty(direction bool) bool

Source Files

doc.go migration.go parse.go run.go

Version
v3.15.1
Published
Oct 10, 2023
Platform
windows/amd64
Imports
10 packages
Last checked
48 minutes ago

Tools for package owners.