migration

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2019 License: MIT Imports: 12 Imported by: 4

Documentation

Index

Constants

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"
)
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"
)

Variables

This section is empty.

Functions

func CopyIn

func CopyIn(table string, columns ...string) string

CopyIn creates a COPY FROM statement which can be prepared with Tx.Prepare(). The target table should be visible in search_path.

func Exists

func Exists(c *db.Connection, tx *sql.Tx, selectStatement string) (bool, error)

Exists returns if a statement has results.

func GetContextLabels added in v1.20201204.1

func GetContextLabels(ctx context.Context) []string

GetContextLabels gets a group from a context as a value.

func NoOp

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

NoOp performs no action.

func Not added in v1.20201204.1

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

Not inverts the output of a predicate.

func NotExists

func NotExists(c *db.Connection, tx *sql.Tx, selectStatement string) (bool, error)

NotExists returns if a statement doesnt have results.

func PredicateColumnExists added in v1.20201204.1

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

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

func PredicateConstraintExists added in v1.20201204.1

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

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

func PredicateIndexExists added in v1.20201204.1

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

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

func PredicateRoleExists added in v1.20201204.1

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

PredicateRoleExists returns if a role exists or not.

func PredicateTableExists added in v1.20201204.1

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

PredicateTableExists returns if a table exists on the given connection.

func QuoteIdentifier

func QuoteIdentifier(name string) string

QuoteIdentifier quotes an "identifier" (e.g. a table or a column name) to be used as part of an SQL statement. For example:

tblname := "my_table"
data := "my_data"
quoted := pq.QuoteIdentifier(tblname)
err := db.Exec(fmt.Sprintf("INSERT INTO %s VALUES ($1)", quoted), data)

Any double quotes in name will be escaped. The quoted identifier will be case sensitive when used in a query. If the input string contains a zero byte, the result will be truncated immediately before it.

func WithLabel added in v1.20201204.1

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

WithLabel adds a label to the context

func WithSuite added in v1.20201204.1

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

WithSuite adds a suite as a value to a context.

Types

type Action added in v1.20201204.1

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

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

func Actions

func Actions(actions ...Action) Action

Actions returns a single body func that executes all the given actions serially.

func Statements

func Statements(statements ...string) Action

Statements returns a body func that executes the statments serially.

type Actionable added in v1.20201204.1

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

Actionable is a type that represents a migration action.

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) Action

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

Action applies the file reader.

func (*DataFileReader) Label

func (dfr *DataFileReader) Label() string

Label returns the label for the data file reader.

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 GroupedActions

type GroupedActions []Actionable

GroupedActions is an atomic series of migration actions. It uses transactions to apply these actions as an atomic unit.

func Group

func Group(actions ...Actionable) GroupedActions

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

func (GroupedActions) Action

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

Action runs the groups actions within a transaction.

type GuardFunc

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

GuardFunc is a control for migration steps.

func Always added in v1.20201204.1

func Always() GuardFunc

Always always runs a step.

func ColumnExists

func ColumnExists(tableName, columnName string) GuardFunc

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

func ColumnExistsWithPredicate

func ColumnExistsWithPredicate(predicate Predicate2, tableName, columnName string) GuardFunc

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

func ColumnNotExists

func ColumnNotExists(tableName, columnName string) GuardFunc

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

func ColumnNotExistsWithPredicate

func ColumnNotExistsWithPredicate(predicate Predicate2, tableName, columnName string) GuardFunc

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

func ConstraintExists

func ConstraintExists(constraintName string) GuardFunc

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

func ConstraintExistsWithPredicate

func ConstraintExistsWithPredicate(predicate Predicate, constraintName string) GuardFunc

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

func ConstraintNotExists

func ConstraintNotExists(constraintName string) GuardFunc

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

func ConstraintNotExistsWithPredicate

func ConstraintNotExistsWithPredicate(predicate Predicate, constraintName string) GuardFunc

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

func Guard

func Guard(description string, predicate func(c *db.Connection, tx *sql.Tx) (bool, error)) GuardFunc

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

func IfExists

func IfExists(statement string) GuardFunc

IfExists only runs the statement if the given item exists.

func IfNotExists

func IfNotExists(statement string) GuardFunc

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

func IndexExists

func IndexExists(tableName, indexName string) GuardFunc

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

func IndexExistsWithPredicate

func IndexExistsWithPredicate(predicate Predicate2, tableName, indexName string) GuardFunc

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

func IndexNotExists

func IndexNotExists(tableName, indexName string) GuardFunc

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

func IndexNotExistsWithPredicate

func IndexNotExistsWithPredicate(predicate Predicate2, tableName, indexName string) GuardFunc

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

func RoleExists

func RoleExists(roleName string) GuardFunc

RoleExists alters an existing role in the db

func RoleExistsWithPredicate

func RoleExistsWithPredicate(predicate Predicate, roleName string) GuardFunc

RoleExistsWithPredicate alters an existing role in the db

func RoleNotExists

func RoleNotExists(roleName string) GuardFunc

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

func RoleNotExistsWithPredicate

func RoleNotExistsWithPredicate(predicate Predicate, roleName string) GuardFunc

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

func TableExists

func TableExists(tableName string) GuardFunc

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

func TableExistsWithPredicate

func TableExistsWithPredicate(predicate Predicate, tableName string) GuardFunc

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

func TableNotExists

func TableNotExists(tableName string) GuardFunc

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

func TableNotExistsWithPredicate

func TableNotExistsWithPredicate(predicate Predicate, tableName string) GuardFunc

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

type GuardedAction

type GuardedAction struct {
	Guard GuardFunc
	Body  Action
}

GuardedAction is a guarded actionable.

func Step

func Step(guard GuardFunc, action Action) *GuardedAction

Step returns a new guarded actionable.

func (GuardedAction) Action

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

Action runs the body if the provided guard passes.

type Predicate

type Predicate func(*db.Connection, *sql.Tx, string) (bool, error)

Predicate is a function that evaluates based on a string param.

type Predicate2

type Predicate2 func(*db.Connection, *sql.Tx, string, string) (bool, error)

Predicate2 is a function that evaluates based on two string params.

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 Suite

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

Suite is a migration suite.

func GetContextSuite added in v1.20201204.1

func GetContextSuite(ctx context.Context) *Suite

GetContextSuite gets a suite from a context as a value.

func New

func New(groups ...GroupedActions) *Suite

New returns a new suite of groups.

func (*Suite) Apply

func (s *Suite) Apply(c *db.Connection) (err error)

Apply applies the suite.

func (*Suite) Applyf added in v1.20201204.1

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

Applyf writes an applied step message.

func (*Suite) Context

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

Context returns the suite context.

func (*Suite) Error added in v1.20201204.1

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

Error

func (*Suite) Errorf added in v1.20201204.1

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

Errorf writes an error for a given step.

func (*Suite) Logger

func (s *Suite) Logger() logger.Log

Logger returns the underlying logger.

func (*Suite) Skipf added in v1.20201204.1

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

Skipf skips a given step.

func (*Suite) WithContext

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

WithContext sets the suite context.

func (*Suite) WithGroups

func (s *Suite) WithGroups(groups ...GroupedActions) *Suite

WithGroups adds groups to the suite and returns the suite.

func (*Suite) WithLogger

func (s *Suite) WithLogger(log logger.Log) *Suite

WithLogger sets the suite logger.

func (*Suite) Write added in v1.20201204.1

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

func (*Suite) WriteStats added in v1.20201204.1

func (s *Suite) WriteStats()

WriteStats writes the stats if a logger is configured.

Jump to

Keyboard shortcuts

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