migration

package
v0.0.0-...-bc49051 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatApplied = "applied"
	StatFailed  = "failed"
	StatSkipped = "skipped"
	StatTotal   = "total"
)

Migration Stats

Variables

This section is empty.

Functions

func GetContextLabels

func GetContextLabels(ctx context.Context) []string

GetContextLabels gets a group from a context as a value.

func NoOp

func NoOp(_ context.Context, _ *db.Connection, _ *sql.Tx) error

NoOp performs no action.

func Not

func Not(proceed bool, err error) (bool, error)

Not inverts the output of a predicate.

func PredicateAny

func PredicateAny(ctx context.Context, c *db.Connection, tx *sql.Tx, selectStatement string, params ...interface{}) (bool, error)

PredicateAny returns if a statement has results.

func PredicateColumnExists

func PredicateColumnExists(ctx context.Context, c *db.Connection, tx *sql.Tx, tableName, columnName string) (bool, error)

PredicateColumnExists returns if a column exists on a table in a specific schema on the given connection.

func PredicateConstraintExists

func PredicateConstraintExists(ctx context.Context, c *db.Connection, tx *sql.Tx, tableName, constraintName string) (bool, error)

PredicateConstraintExists returns if a constraint exists on a table in a specific schema on the given connection.

func PredicateIndexExists

func PredicateIndexExists(ctx context.Context, c *db.Connection, tx *sql.Tx, tableName, indexName string) (bool, error)

PredicateIndexExists returns if a index exists on a table in a specific schema on the given connection.

func PredicateNone

func PredicateNone(ctx context.Context, c *db.Connection, tx *sql.Tx, selectStatement string, params ...interface{}) (bool, error)

PredicateNone returns if a statement doesnt have results.

func PredicateRoleExists

func PredicateRoleExists(ctx context.Context, c *db.Connection, tx *sql.Tx, roleName string) (bool, error)

PredicateRoleExists returns if a role exists or not.

func PredicateTableExists

func PredicateTableExists(ctx context.Context, c *db.Connection, tx *sql.Tx, tableName string) (bool, error)

PredicateTableExists returns if a table exists in a specific schema on the given connection.

func WithLabel

func WithLabel(ctx context.Context, label string) context.Context

WithLabel adds a label to the context

func WithSuite

func WithSuite(ctx context.Context, suite *Suite) context.Context

WithSuite adds a suite as a value to a context.

Types

type Action

type Action interface {
	Action(context.Context, *db.Connection, *sql.Tx) error
}

Action is a type that represents a migration action.

func Actions

func Actions(actions ...Action) Action

Actions creates an Action with a single body func that executes all the variadic argument actions serially

func Exec

func Exec(statement string, args ...interface{}) Action

Exec creates an Action that will run a statement with a given set of arguments. It can be used in lieu of Statements, when parameterization is needed

func Statements

func Statements(statements ...string) Action

Statements returns a body func that executes the statments serially.

type ActionFunc

type ActionFunc func(context.Context, *db.Connection, *sql.Tx) error

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

func (ActionFunc) Action

func (a ActionFunc) Action(ctx context.Context, conn *db.Connection, tx *sql.Tx) error

Action implements actioner.

type Event

type Event struct {
	Result string
	Body   string
	Labels []string
}

Event is a migration logger event.

func NewEvent

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

NewEvent returns a new event.

func (Event) String

func (e Event) String() string

WriteText writes the migration event as text.

type Group

type Group struct {
	Actions         []Action
	Tx              *sql.Tx
	SkipTransaction bool
}

Group is an series of migration actions. It uses normally transactions to apply these actions as an atomic unit, but this transaction can be bypassed by setting the SkipTransaction flag to true. This allows the use of CONCURRENT index creation and other operations that postgres will not allow within a transaction.

func NewGroup

func NewGroup(options ...GroupOption) *Group

NewGroup creates a new Group from a given list of actionable.

func NewGroupWithAction

func NewGroupWithAction(action Action, options ...GroupOption) *Group

NewGroupWithAction returns a new group with a single action.

func NewGroupWithStep

func NewGroupWithStep(guard GuardFunc, action Action, options ...GroupOption) *Group

NewGroupWithStep returns a new group with a single step.

func (*Group) Action

func (ga *Group) Action(ctx context.Context, c *db.Connection) (err error)

Action runs the groups actions within a transaction.

type GroupOption

type GroupOption func(g *Group)

GroupOption is an option for migration Groups (Group)

func OptGroupActions

func OptGroupActions(actions ...Action) GroupOption

OptGroupActions allows you to add actions to the NewGroup. If you want, multiple OptActions can be applied to the same NewGroup. They are additive.

func OptGroupSkipTransaction

func OptGroupSkipTransaction() GroupOption

OptGroupSkipTransaction will allow this group to be run outside of a transaction. Use this to concurrently create indices and perform other actions that cannot be executed in a Tx

func OptGroupTx

func OptGroupTx(tx *sql.Tx) GroupOption

OptGroupTx sets a transaction on the group.

type GuardFunc

type GuardFunc func(context.Context, *db.Connection, *sql.Tx, Action) error

GuardFunc is a control for migration steps. It should internally evaluate if the action should be called. The action is typically given separately so these two components can be composed.

func Always

func Always() GuardFunc

Always always runs a step.

func ColumnExists

func ColumnExists(tableName, columnName string) GuardFunc

ColumnExists returns a guard that ensures a column exists

func ColumnNotExists

func ColumnNotExists(tableName, columnName string) GuardFunc

ColumnNotExists returns a guard that ensures a column does not exist

func ConstraintExists

func ConstraintExists(tableName, constraintName string) GuardFunc

ConstraintExists returns a guard that ensures a constraint exists

func ConstraintNotExists

func ConstraintNotExists(tableName, constraintName string) GuardFunc

ConstraintNotExists returns a guard that ensures a constraint does not exist

func Guard

func Guard(description string, predicate GuardPredicateFunc) GuardFunc

Guard returns a function that determines if a step in a group should run.

func IfExists

func IfExists(statement string, args ...interface{}) GuardFunc

IfExists only runs the statement if the given item exists.

func IfNotExists

func IfNotExists(statement string, args ...interface{}) GuardFunc

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

func IndexExists

func IndexExists(tableName, indexName string) GuardFunc

IndexExists returns a guard that ensures an index exists

func IndexNotExists

func IndexNotExists(tableName, indexName string) GuardFunc

IndexNotExists returns a guard that ensures an index does not exist

func RoleExists

func RoleExists(roleName string) GuardFunc

RoleExists returns a guard that ensures a role (user) exists

func RoleNotExists

func RoleNotExists(roleName string) GuardFunc

RoleNotExists returns a guard that ensures a role (user) does not exist

func TableExists

func TableExists(tableName string) GuardFunc

TableExists returns a guard that ensures a table exists

func TableNotExists

func TableNotExists(tableName string) GuardFunc

TableNotExists returns a guard that ensures a table does not exist

type GuardPredicateFunc

type GuardPredicateFunc func(context.Context, *db.Connection, *sql.Tx) (bool, error)

GuardPredicateFunc is a function that can act as a guard

type StatementSlice

type StatementSlice []string

StatementSlice is an array of strings.

func (StatementSlice) Action

func (s StatementSlice) Action(ctx context.Context, c *db.Connection, tx *sql.Tx) (err error)

Action implements Action.

type StatsEvent

type StatsEvent struct {
	// 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) String

func (se StatsEvent) String() string

WriteText writes the event to a text writer.

type Step

type Step struct {
	Guard GuardFunc
	Body  Action
}

Step is a guarded action. The GuardFunc will decide whether to execute this Action

func NewStep

func NewStep(guard GuardFunc, action Action) *Step

NewStep returns a new Step, given a GuardFunc and an Action

func (*Step) Action

func (ga *Step) Action(ctx context.Context, c *db.Connection, tx *sql.Tx) error

Action implements the Actionable interface and runs the body if the provided guard passes.

type Suite

type Suite struct {
	Groups []*Group
	Log    db.Logger

	Stats struct {
		Applied int
		Skipped int
		Failed  int
		Total   int
	}
}

Suite is a migration suite.

func GetContextSuite

func GetContextSuite(ctx context.Context) *Suite

GetContextSuite gets a suite from a context as a value.

func New

func New(options ...SuiteOption) *Suite

New returns a new suite of groups.

func NewWithActions

func NewWithActions(actions ...Action) *Suite

NewWithActions returns a new suite, with a new group, made up of given actions.

func NewWithGroups

func NewWithGroups(groups ...*Group) *Suite

NewWithGroups returns a new suite from a given list of groups.

func (*Suite) Apply

func (s *Suite) Apply(ctx context.Context, c *db.Connection) (err error)

Apply applies the suite.

func (*Suite) ApplyTx

func (s *Suite) ApplyTx(ctx context.Context, c *db.Connection, tx *sql.Tx) (err error)

ApplyTx applies the suite within a given transaction (which can be nil).

func (*Suite) Write

func (s *Suite) Write(ctx context.Context, result, body string)

Write writes a message for the suite.

func (*Suite) WriteApplyf

func (s *Suite) WriteApplyf(ctx context.Context, format string, args ...interface{})

WriteApplyf writes an applied step message.

func (*Suite) WriteError

func (s *Suite) WriteError(ctx context.Context, err error) error

WriteError writes an error for a given step.

func (*Suite) WriteErrorf

func (s *Suite) WriteErrorf(ctx context.Context, format string, args ...interface{})

WriteErrorf writes an error for a given step.

func (*Suite) WriteSkipf

func (s *Suite) WriteSkipf(ctx context.Context, format string, args ...interface{})

WriteSkipf skips a given step.

func (*Suite) WriteStats

func (s *Suite) WriteStats(ctx context.Context)

WriteStats writes the stats if a logger is configured.

type SuiteOption

type SuiteOption func(s *Suite)

SuiteOption is an option for migration Suites

func OptGroups

func OptGroups(groups ...*Group) SuiteOption

OptGroups allows you to add groups to the Suite. If you want, multiple OptGroups can be applied to the same Suite. They are additive.

func OptLog

func OptLog(log db.Logger) SuiteOption

OptLog sets the suite logger.

Jump to

Keyboard shortcuts

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