migration

package
v1.0.0-b001 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2018 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// StatApplied is a stat name.
	StatApplied = "applied"
	// StatFailed is a stat name.
	StatFailed = "failed"
	// StatSkipped is a stat name.
	StatSkipped = "skipped"
	// StatTotal is a stat name.
	StatTotal = "total"
)
View Source
const (
	// Flag is a logger event flag.
	Flag logger.Flag = "db.migration"

	// FlagStats is a logger event flag.
	FlagStats logger.Flag = "db.migration.stats"
)

Variables

View Source
var (
	// NoOp is used when testing guards.
	NoOp = noOp{}
)

Functions

func RegisterDefault

func RegisterDefault(m ...Migration) error

RegisterDefault adds migrations to the default group.

func SetDefault

func SetDefault(group *Group)

SetDefault sets the default group.

Types

type Collector

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

Collector is a results collector.

func (*Collector) Applyf

func (c *Collector) Applyf(m Migration, body string, args ...interface{})

Applyf active actions to the log.

func (*Collector) Error

func (c *Collector) Error(m Migration, err error) error

Errorf writes errors to the log.

func (*Collector) Skipf

func (c *Collector) Skipf(m Migration, body string, args ...interface{})

Skipf passive actions to the log.

func (*Collector) WriteStats

func (c *Collector) WriteStats()

WriteStats writes final stats to output

type DataFileReader

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

DataFileReader reads a postgres dump.

func ReadDataFile

func ReadDataFile(filePath string) *DataFileReader

ReadDataFile returns a new DataFileReader

func (*DataFileReader) Apply

func (dfr *DataFileReader) Apply(c *db.Connection, optionalTx ...*sql.Tx) (err error)

Apply applies the data file reader.

func (*DataFileReader) Collector

func (dfr *DataFileReader) Collector() *Collector

Collector returns the logger.

func (*DataFileReader) Invoke

func (dfr *DataFileReader) Invoke(c *db.Connection, tx *sql.Tx) (err error)

Invoke consumes the data file and writes it to the db.

func (*DataFileReader) Label

func (dfr *DataFileReader) Label() string

Label returns the label for the data file reader.

func (*DataFileReader) Parent

func (dfr *DataFileReader) Parent() Migration

Parent returns the parent for the data file reader.

func (*DataFileReader) TransactionBound

func (dfr *DataFileReader) TransactionBound() bool

TransactionBound returns if the migration manages its own transactions.

func (*DataFileReader) WithCollector

func (dfr *DataFileReader) WithCollector(collector *Collector) Migration

WithCollector sets the logger for the data file reader.

func (*DataFileReader) WithLabel

func (dfr *DataFileReader) WithLabel(value string) Migration

WithLabel sets the migration label.

func (*DataFileReader) WithParent

func (dfr *DataFileReader) WithParent(parent Migration) Migration

WithParent sets the parent for the data file reader.

func (*DataFileReader) WithTransactionBound

func (dfr *DataFileReader) WithTransactionBound(transactionBound bool) Migration

WithTransactionBound sets if the migration manages its own transactions or not.

type Event

type Event struct {
	*logger.EventMeta
	// contains filtered or unexported fields
}

Event is a migration logger event.

func NewEvent

func NewEvent(result, body string, labels ...string) *Event

NewEvent returns a new event.

func (Event) WriteJSON

func (e Event) WriteJSON() logger.JSONObj

WriteJSON implements logger.JSONWritable.

func (Event) WriteText

func (e Event) WriteText(tf logger.TextFormatter, buf *bytes.Buffer)

WriteText writes the migration event as text.

type Group

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

Group is an atomic series of migrations. It uses transactions to apply a set of sub-migrations as a unit.

func Default

func Default() *Group

Default returns the default migration suite.

func NewGroup

func NewGroup(migrations ...Migration) *Group

NewGroup creates a new migration group.

func (*Group) AbortOnError

func (g *Group) AbortOnError() bool

AbortOnError indicates that the group will abort if it sees an error from a step.

func (*Group) Add

func (g *Group) Add(migrations ...Migration)

Add adds migrations to the group.

func (*Group) Apply

func (g *Group) Apply(c *db.Connection, txs ...*sql.Tx) (err error)

Apply wraps the action in a transaction and commits it if there were no errors, rolling back if there were.

func (*Group) Collector

func (g *Group) Collector() *Collector

Collector returns the collector.

func (*Group) IsRoot

func (g *Group) IsRoot() bool

IsRoot denotes if the runner is the root runner (or not).

func (*Group) Label

func (g *Group) Label() string

Label returns a label for the runner.

func (*Group) Parent

func (g *Group) Parent() Migration

Parent returns the runner's parent.

func (*Group) RollbackOnComplete

func (g *Group) RollbackOnComplete() bool

RollbackOnComplete returns if we should rollback the migration on complete.

func (*Group) TransactionBound

func (g *Group) TransactionBound() bool

TransactionBound returns if the migration manages its own transactions. This is a group, it is the one doing the managing.

func (*Group) UseTransaction

func (g *Group) UseTransaction() bool

UseTransaction returns if the group should wrap child steps in a transaction.

func (*Group) With

func (g *Group) With(migrations ...Migration) *Group

With adds migrations to the group and returns a reference.

func (*Group) WithAbortOnError

func (g *Group) WithAbortOnError(value bool) *Group

WithAbortOnError sets if the group should abort on error.

func (*Group) WithCollector

func (g *Group) WithCollector(collector *Collector) Migration

WithCollector sets the collector.

func (*Group) WithLabel

func (g *Group) WithLabel(value string) Migration

WithLabel sets the migration label.

func (*Group) WithLogger

func (g *Group) WithLogger(log *logger.Logger) *Group

WithLogger sets the collector output logger.

func (*Group) WithParent

func (g *Group) WithParent(parent Migration) Migration

WithParent sets the runner's parent.

func (*Group) WithRollbackOnComplete

func (g *Group) WithRollbackOnComplete(rollbackOnComplete bool) *Group

WithRollbackOnComplete sets if we should roll the transaction back on complete.

func (*Group) WithUseTransaction

func (g *Group) WithUseTransaction(useTransaction bool) Migration

WithUseTransaction sets if we should begin a transaction for the work within the group.

type Guard

type Guard func(s *Step, c *db.Connection, tx *sql.Tx) error

Guard is a control for migration steps.

func AlwaysRun

func AlwaysRun() Guard

AlwaysRun always runs a step.

func ColumnExists

func ColumnExists(tableName, columnName string) Guard

ColumnExists alters an existing column, erroring if it doesn't exist

func ColumnNotExists

func ColumnNotExists(tableName, columnName string) Guard

ColumnNotExists creates a table on the given connection if it does not exist.

func ConstraintExists

func ConstraintExists(constraintName string) Guard

ConstraintExists alters an existing constraint, erroring if it doesn't exist

func ConstraintNotExists

func ConstraintNotExists(constraintName string) Guard

ConstraintNotExists creates a table on the given connection if it does not exist.

func DynamicGuard

func DynamicGuard(label string, guard func(c *db.Connection, tx *sql.Tx) (bool, error)) Guard

DynamicGuard is guard that can dynamically determine if the inner invocable should run.

func IfExists

func IfExists(statement string) Guard

IfExists only runs the statement if the given item exists.

func IfNotExists

func IfNotExists(statement string) Guard

IfNotExists only runs the statement if the given item doesn't exist.

func IndexExists

func IndexExists(tableName, indexName string) Guard

IndexExists alters an existing index, erroring if it doesn't exist

func IndexNotExists

func IndexNotExists(tableName, indexName string) Guard

IndexNotExists creates a index on the given connection if it does not exist.

func RoleExists

func RoleExists(roleName string) Guard

RoleExists alters an existing role in the db

func RoleNotExists

func RoleNotExists(roleName string) Guard

RoleNotExists creates a new role if it doesn't exist.

func TableExists

func TableExists(tableName string) Guard

TableExists alters an existing table, erroring if it doesn't exist

func TableNotExists

func TableNotExists(tableName string) Guard

TableNotExists creates a table on the given connection if it does not exist.

type Invocable

type Invocable interface {
	Invoke(c *db.Connection, tx *sql.Tx) error
}

Invocable is a thing that can be invoked.

func Actions

func Actions(invocableActions ...InvocableAction) Invocable

Actions returns an invocable of a set of actions.

func Statements

func Statements(stmts ...string) Invocable

Statements is an alias to Body(...Statement(stmt))

type InvocableAction

type InvocableAction func(c *db.Connection, tx *sql.Tx) error

InvocableAction is a function that can be run during a migration step.

type Migration

type Migration interface {
	WithLabel(label string) Migration
	Label() string

	WithParent(parent Migration) Migration
	Parent() Migration

	WithCollector(*Collector) Migration
	Collector() *Collector

	// TransactionBound indicates if this migration manages its transaction context.
	// If this returns true, a parent will not pass a transaction into Apply.
	// If it is false, a transaction will be started for this
	TransactionBound() bool

	// Apply runs the migration.
	Apply(c *db.Connection, txs ...*sql.Tx) error
}

Migration is either a group of steps or the entire suite.

type StatsEvent

type StatsEvent struct {
	*logger.EventMeta
	// contains filtered or unexported fields
}

StatsEvent is a migration logger event.

func NewStatsEvent

func NewStatsEvent(applied, skipped, failed, total int) *StatsEvent

NewStatsEvent returns a new stats event.

func (StatsEvent) WriteJSON

func (se StatsEvent) WriteJSON() logger.JSONObj

WriteJSON implements logger.JSONWritable.

func (StatsEvent) WriteText

func (se StatsEvent) WriteText(tf logger.TextFormatter, buf *bytes.Buffer)

WriteText writes the event to a text writer.

type Step

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

Step is a single guarded function.

func NewStep

func NewStep(guard Guard, body Invocable) *Step

NewStep is an alias to NewOperation.

func (*Step) Apply

func (s *Step) Apply(c *db.Connection, txs ...*sql.Tx) (err error)

Apply wraps the action in a transaction and commits it if there were no errors, rolling back if there were.

func (*Step) Collector

func (s *Step) Collector() *Collector

Collector returns the collector

func (*Step) Label

func (s *Step) Label() string

Label returns the operation label.

func (*Step) Parent

func (s *Step) Parent() Migration

Parent returns the parent.

func (*Step) TransactionBound

func (s *Step) TransactionBound() bool

TransactionBound returns if the migration manages its own transactions.

func (*Step) WithCollector

func (s *Step) WithCollector(collector *Collector) Migration

WithCollector sets the collector.

func (*Step) WithLabel

func (s *Step) WithLabel(label string) Migration

WithLabel sets the operation label.

func (*Step) WithParent

func (s *Step) WithParent(parent Migration) Migration

WithParent sets the operation parent.

func (*Step) WithTransactionBound

func (s *Step) WithTransactionBound(transactionBound bool) Migration

WithTransactionBound sets if the migration manages its own transactions or not.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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