Documentation ¶
Overview ¶
Package migration provides helpers for writing rerunnable database migrations.
These are built around Suites, which are sets of Groups that execute within a transaction, those Groups are composed of Steps, which are a Guard and an Action.
Index ¶
- Constants
- func GetContextLabels(ctx context.Context) []string
- func NoOp(_ context.Context, _ *db.Connection, _ *sql.Tx) error
- func Not(proceed bool, err error) (bool, error)
- func PredicateAny(ctx context.Context, c *db.Connection, tx *sql.Tx, selectStatement string, ...) (bool, error)
- func PredicateColumnExists(ctx context.Context, c *db.Connection, tx *sql.Tx, ...) (bool, error)
- func PredicateColumnExistsInSchema(ctx context.Context, c *db.Connection, tx *sql.Tx, ...) (bool, error)
- func PredicateConstraintExists(ctx context.Context, c *db.Connection, tx *sql.Tx, ...) (bool, error)
- func PredicateConstraintExistsInSchema(ctx context.Context, c *db.Connection, tx *sql.Tx, ...) (bool, error)
- func PredicateIndexExists(ctx context.Context, c *db.Connection, tx *sql.Tx, tableName, indexName string) (bool, error)
- func PredicateIndexExistsInSchema(ctx context.Context, c *db.Connection, tx *sql.Tx, ...) (bool, error)
- func PredicateNone(ctx context.Context, c *db.Connection, tx *sql.Tx, selectStatement string, ...) (bool, error)
- func PredicateRoleExists(ctx context.Context, c *db.Connection, tx *sql.Tx, roleName string) (bool, error)
- func PredicateSchemaExists(ctx context.Context, c *db.Connection, tx *sql.Tx, schemaName string) (bool, error)
- func PredicateTableExists(ctx context.Context, c *db.Connection, tx *sql.Tx, tableName string) (bool, error)
- func PredicateTableExistsInSchema(ctx context.Context, c *db.Connection, tx *sql.Tx, ...) (bool, error)
- func WithLabel(ctx context.Context, label string) context.Context
- func WithSuite(ctx context.Context, suite *Suite) context.Context
- type Action
- type Actionable
- type Event
- type Group
- type GroupOption
- type GuardFunc
- func Always() GuardFunc
- func ColumnExists(tableName, columnName string) GuardFunc
- func ColumnExistsInSchema(schemaName, tableName, columnName string) GuardFunc
- func ColumnNotExists(tableName, columnName string) GuardFunc
- func ColumnNotExistsInSchema(schemaName, tableName, columnName string) GuardFunc
- func ConstraintExists(tableName, constraintName string) GuardFunc
- func ConstraintExistsInSchema(schemaName, tableName, constraintName string) GuardFunc
- func ConstraintNotExists(tableName, constraintName string) GuardFunc
- func ConstraintNotExistsInSchema(schemaName, tableName, constraintName string) GuardFunc
- func Guard(description string, predicate GuardPredicateFunc) GuardFunc
- func IfExists(statement string, args ...interface{}) GuardFunc
- func IfNotExists(statement string, args ...interface{}) GuardFunc
- func IndexExists(tableName, indexName string) GuardFunc
- func IndexExistsInSchema(schemaName, tableName, indexName string) GuardFunc
- func IndexNotExists(tableName, indexName string) GuardFunc
- func IndexNotExistsInSchema(schemaName, tableName, indexName string) GuardFunc
- func RoleExists(roleName string) GuardFunc
- func RoleNotExists(roleName string) GuardFunc
- func SchemaExists(schemaName string) GuardFunc
- func SchemaNotExists(schemaName string) GuardFunc
- func TableExists(tableName string) GuardFunc
- func TableExistsInSchema(schemaName, tableName string) GuardFunc
- func TableNotExists(tableName string) GuardFunc
- func TableNotExistsInSchema(schemaName, tableName string) GuardFunc
- type GuardPredicateFunc
- type StatsEvent
- type Step
- type Suite
- func (s *Suite) Apply(ctx context.Context, c *db.Connection) (err error)
- func (s *Suite) Applyf(ctx context.Context, format string, args ...interface{})
- func (s *Suite) Error(ctx context.Context, err error) error
- func (s *Suite) Errorf(ctx context.Context, format string, args ...interface{})
- func (s *Suite) Results() (applied, skipped, failed, total int)
- func (s *Suite) Skipf(ctx context.Context, format string, args ...interface{})
- func (s *Suite) Write(ctx context.Context, result, body string)
- func (s *Suite) WriteStats(ctx context.Context)
- type SuiteOption
Constants ¶
const ( StatApplied = "applied" StatFailed = "failed" StatSkipped = "skipped" StatTotal = "total" )
Migration Stats
const ( // Flag is a logger event flag. Flag = "db.migration" // FlagStats is a logger event flag. FlagStats = "db.migration.stats" )
Variables ¶
This section is empty.
Functions ¶
func GetContextLabels ¶ added in v1.20201204.1
GetContextLabels gets a group from a context as a value.
func PredicateAny ¶ added in v1.20201204.1
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 ¶ added in v1.20201204.1
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 the default schema of the given connection.
func PredicateColumnExistsInSchema ¶ added in v1.20201204.1
func PredicateColumnExistsInSchema(ctx context.Context, c *db.Connection, tx *sql.Tx, schemaName, tableName, columnName string) (bool, error)
PredicateColumnExistsInSchema returns if a column exists on a table in a specific schema on the given connection.
func PredicateConstraintExists ¶ added in v1.20201204.1
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 the default schema of the given connection.
func PredicateConstraintExistsInSchema ¶ added in v1.20201204.1
func PredicateConstraintExistsInSchema(ctx context.Context, c *db.Connection, tx *sql.Tx, schemaName, tableName, constraintName string) (bool, error)
PredicateConstraintExistsInSchema returns if a constraint exists on a table in a specific schema on the given connection.
func PredicateIndexExists ¶ added in v1.20201204.1
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 the default schema of the given connection.
func PredicateIndexExistsInSchema ¶ added in v1.20201204.1
func PredicateIndexExistsInSchema(ctx context.Context, c *db.Connection, tx *sql.Tx, schemaName, tableName, indexName string) (bool, error)
PredicateIndexExistsInSchema returns if a index exists on a table in a specific schema on the given connection.
func PredicateNone ¶ added in v1.20201204.1
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 ¶ added in v1.20201204.1
func PredicateRoleExists(ctx context.Context, c *db.Connection, tx *sql.Tx, roleName string) (bool, error)
PredicateRoleExists returns if a role exists or not.
func PredicateSchemaExists ¶ added in v1.20201204.1
func PredicateSchemaExists(ctx context.Context, c *db.Connection, tx *sql.Tx, schemaName string) (bool, error)
PredicateSchemaExists returns if a schema exists or not.
func PredicateTableExists ¶ added in v1.20201204.1
func PredicateTableExists(ctx context.Context, c *db.Connection, tx *sql.Tx, tableName string) (bool, error)
PredicateTableExists returns if a table exists in the default schema of the given connection.
func PredicateTableExistsInSchema ¶ added in v1.20201204.1
func PredicateTableExistsInSchema(ctx context.Context, c *db.Connection, tx *sql.Tx, schemaName, tableName string) (bool, error)
PredicateTableExistsInSchema returns if a table exists in a specific schema on the given connection.
Types ¶
type Action ¶ added in v1.20201204.1
Action is a function that can be run during a migration step.
func Actions ¶
Actions creates an Action with a single body func that executes all the variadic argument actions serially
func Exec ¶ added in v1.20201204.1
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 ¶
Statements returns a body func that executes the statments serially.
type Actionable ¶ added in v1.20201204.1
Actionable is a type that represents a migration action.
type Event ¶
Event is a migration logger event.
type Group ¶
type Group struct { Actions []Actionable 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 ¶ added in v1.20201204.1
func NewGroupWithAction(guard GuardFunc, action Action, options ...GroupOption) *Group
NewGroupWithAction returns a new group with a single action.
type GroupOption ¶ added in v1.20201204.1
type GroupOption func(g *Group)
GroupOption is an option for migration Groups (Group)
func OptGroupActions ¶ added in v1.20201204.1
func OptGroupActions(actions ...Actionable) 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 ¶ added in v1.20201204.1
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 ¶ added in v1.20201204.1
func OptGroupTx(tx *sql.Tx) GroupOption
OptGroupTx sets a transaction on the group.
type GuardFunc ¶
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 ColumnExists ¶
ColumnExists returns a guard that ensures a column exists
func ColumnExistsInSchema ¶ added in v1.20201204.1
ColumnExistsInSchema returns a guard that ensures a column exists
func ColumnNotExists ¶
ColumnNotExists returns a guard that ensures a column does not exist
func ColumnNotExistsInSchema ¶ added in v1.20201204.1
ColumnNotExistsInSchema returns a guard that ensures a column does not exist
func ConstraintExists ¶
ConstraintExists returns a guard that ensures a constraint exists
func ConstraintExistsInSchema ¶ added in v1.20201204.1
ConstraintExistsInSchema returns a guard that ensures a constraint exists
func ConstraintNotExists ¶
ConstraintNotExists returns a guard that ensures a constraint does not exist
func ConstraintNotExistsInSchema ¶ added in v1.20201204.1
ConstraintNotExistsInSchema 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 IfNotExists ¶
IfNotExists only runs the statement if the given item doesn't exist.
func IndexExists ¶
IndexExists returns a guard that ensures an index exists
func IndexExistsInSchema ¶ added in v1.20201204.1
IndexExistsInSchema returns a guard that ensures an index exists
func IndexNotExists ¶
IndexNotExists returns a guard that ensures an index does not exist
func IndexNotExistsInSchema ¶ added in v1.20201204.1
IndexNotExistsInSchema returns a guard that ensures an index does not exist
func RoleExists ¶
RoleExists returns a guard that ensures a role (user) exists
func RoleNotExists ¶
RoleNotExists returns a guard that ensures a role (user) does not exist
func SchemaExists ¶ added in v1.20201204.1
SchemaExists is a guard function for asserting that a schema exists
func SchemaNotExists ¶ added in v1.20201204.1
SchemaNotExists is a guard function for asserting that a schema does not exist
func TableExists ¶
TableExists returns a guard that ensures a table exists
func TableExistsInSchema ¶ added in v1.20201204.1
TableExistsInSchema returns a guard that ensures a table exists
func TableNotExists ¶
TableNotExists returns a guard that ensures a table does not exist
func TableNotExistsInSchema ¶ added in v1.20201204.1
TableNotExistsInSchema returns a guard that ensures a table exists
type GuardPredicateFunc ¶ added in v1.20201204.1
GuardPredicateFunc is a function that can act as a guard
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) Decompose ¶ added in v1.20201204.1
func (se StatsEvent) Decompose() map[string]interface{}
Decompose implements logger.JSONWritable.
func (StatsEvent) GetFlag ¶ added in v1.20201204.1
func (se StatsEvent) GetFlag() string
GetFlag implements logger.Event.
func (StatsEvent) WriteText ¶
func (se StatsEvent) WriteText(tf logger.TextFormatter, wr io.Writer)
WriteText writes the event to a text writer.
type Step ¶
Step is a guarded action. The GuardFunc will decide whether to execute this Action
type Suite ¶
Suite is a migration suite.
func GetContextSuite ¶ added in v1.20201204.1
GetContextSuite gets a suite from a context as a value.
func NewWithActions ¶ added in v1.20201204.1
func NewWithActions(actions ...Actionable) *Suite
NewWithActions returns a new suite, with a new group, made up of given actions.
func (*Suite) Results ¶ added in v1.20201204.1
Results provides a window into the results of this migration
func (*Suite) WriteStats ¶ added in v1.20201204.1
WriteStats writes the stats if a logger is configured.
type SuiteOption ¶ added in v1.20201204.1
type SuiteOption func(s *Suite)
SuiteOption is an option for migration Suites
func OptGroups ¶ added in v1.20201204.1
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 ¶ added in v1.20201204.1
func OptLog(log logger.Log) SuiteOption
OptLog allows you to add a logger to the Suite.