migrate

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2022 License: Apache-2.0 Imports: 12 Imported by: 78

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoPlan = errors.New("sql/migrate: no plan for matched states")

ErrNoPlan is returned by Plan when there is no change between the two states.

View Source
var (
	// TemplateFuncs defines the global functions available for the templates.
	TemplateFuncs = template.FuncMap{
		"hasSuffix": strings.HasSuffix,
		"timestamp": func() int64 {
			return time.Now().Unix()
		},
	}
)

Functions

This section is empty.

Types

type Change

type Change struct {
	// Cmd or statement to execute.
	Cmd string

	// Args for placeholder parameters in the statement above.
	Args []interface{}

	// A Comment describes the change.
	Comment string

	// Reverse contains the "reversed statement" if
	// command is reversible.
	Reverse string

	// The Source that caused this change, or nil.
	Source schema.Change
}

A Change of migration.

type Dir

type Dir struct {
	// contains filtered or unexported fields
}

Dir represents a versioned migration directory.

func NewDir

func NewDir(opts ...DirOption) (*Dir, error)

NewDir creates a new workspace directory based on the given configuration options.

func (*Dir) Compact

func (d *Dir) Compact(ctx context.Context, name string, n int) error

Compact compacts the first n migration files into one. If n < 0, all files are selected.

func (*Dir) Plan

func (d *Dir) Plan(ctx context.Context, name string, to StateReader) (*Plan, error)

Plan calculates the migration Plan required for moving from the directory state to the next state (to). A StateReader, can be another directory, static schema elements or a Driver connection.

func (*Dir) ReadState

func (d *Dir) ReadState(ctx context.Context) (*schema.Realm, error)

ReadState reads the current database/realm state that is stored in the migration directory. The given emulator driver is used for playing all migration files against to it.

func (*Dir) WritePlan

func (d *Dir) WritePlan(p *Plan) error

WritePlan writes the given plan to the directory based on the given Write configuration.

type DirOption

type DirOption func(*Dir) error

DirOption allows configuring the Dir using functional options.

func DirConn

func DirConn(conn Driver) DirOption

DirConn provides a Driver connection to a database. It is usually connected to an ephemeral database for emulating migration changes on it and calculating the "current state" to be compared with the "desired state".

func DirFS

func DirFS(fs fs.FS) DirOption

DirFS configures the FS used by the migration directory.

func DirGlob

func DirGlob(pattern string) DirOption

DirGlob configures the glob/pattern for reading migration files from the directory. For example:

migrate.NewDir(
	migrate.DirPath("migrations"),
	migrate.DirGlob("*.up.sql"),
)

func DirPath

func DirPath(path string) DirOption

DirPath configures the FS used by the migration directory to point the given OS directory.

func DirTemplates

func DirTemplates(nameFileTmpl ...string) DirOption

DirTemplates configures template files for writing the Plan object to the migration directory.

migrate.NewDir(
	migrate.DirPath("migrations"),
	migrate.DirTemplates("{{timestamp}}{{.Name}}.up.sql", "{{range c := .Changes}}{{println c.Cmd}}{{end}}"),
)

migrate.NewDir(
	migrate.DirPath("migrations"),
	migrate.DirTemplates(
		"{{timestamp}}{{.Name}}.up.sql", "{{range $c := .Changes}}{{println $c.Cmd}}{{end}}",
		"{{timestamp}}{{.Name}}.down.sql", "{{range $c := .Changes}}{{println $c.Reverse}}{{end}}",
	),
)

type Driver

The Driver interface must be implemented by the different dialects to support database migration authoring/planning and applying. ExecQuerier, Inspector and Differ, provide basic schema primitives for inspecting database schemas, calculate the difference between schema elements, and executing raw SQL statements. The PlanApplier interface wraps the methods for generating migration plan for applying the actual changes on the database.

type FileRemoveWriter

type FileRemoveWriter interface {
	RemoveFile(name string) error
	WriteFile(name string, data []byte, perm fs.FileMode) error
}

FileRemoveWriter wraps the WriteFile and RemoveFile methods to allow editing the migration directory on development.

type Plan

type Plan struct {
	// Name of the plan. Provided by the user or auto-generated.
	Name string

	// Reversible describes if the changeset is reversible.
	Reversible bool

	// Transactional describes if the changeset is transactional.
	Transactional bool

	// Changes defines the list of changeset in the plan.
	Changes []*Change
}

A Plan defines a planned changeset that its execution brings the database to the new desired state. Additional information is calculated by the different drivers to indicate if the changeset is transactional (can be rolled-back) and reversible (a down file can be generated to it).

type PlanApplier

type PlanApplier interface {
	// PlanChanges returns a migration plan for applying the given changeset.
	PlanChanges(context.Context, string, []schema.Change) (*Plan, error)

	// ApplyChanges is responsible for applying the given changeset.
	// An error may return from ApplyChanges if the driver is unable
	// to execute a change.
	ApplyChanges(context.Context, []schema.Change) error
}

PlanApplier wraps the methods for planning and applying changes on the database.

type StateReader

type StateReader interface {
	ReadState(ctx context.Context) (*schema.Realm, error)
}

StateReader wraps the method for reading a database/schema state. The types below provides a few builtin options for reading a state from a migration directory, a static object (e.g. a parsed file).

In next Go version, the State will be replaced with the following union type `interface { Realm | Schema }`.

func Realm

func Realm(r *schema.Realm) StateReader

Realm returns a state reader for the static Realm object.

func Schema

func Schema(s *schema.Schema) StateReader

Schema returns a state reader for the static Schema object.

type StateReaderFunc

type StateReaderFunc func(ctx context.Context) (*schema.Realm, error)

The StateReaderFunc type is an adapter to allow the use of ordinary functions as state readers.

func (StateReaderFunc) ReadState

func (f StateReaderFunc) ReadState(ctx context.Context) (*schema.Realm, error)

ReadState calls f(ctx).

Jump to

Keyboard shortcuts

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