Documentation
¶
Index ¶
- Variables
- func Create(ctx context.Context, identifier string, db DBX) error
- func CreateSpanner(ctx context.Context, identifier string, db DBX) error
- type Action
- type CreateDB
- type DBX
- type Func
- type Migration
- func (migration *Migration) CurrentVersion(ctx context.Context, log *zap.Logger, db tagsql.DB) (int, error)
- func (migration *Migration) Run(ctx context.Context, log *zap.Logger) error
- func (migration *Migration) TargetVersion(version int) *Migration
- func (migration *Migration) ValidTableName() error
- func (migration *Migration) ValidateSteps() error
- func (migration *Migration) ValidateVersions(ctx context.Context, log *zap.Logger) error
- type SQL
- type Step
Constants ¶
This section is empty.
Variables ¶
var (
// ErrValidateVersionQuery is when there is an error querying version table.
ErrValidateVersionQuery = errs.Class("validate db version query")
// ErrValidateVersionMismatch is when the migration version does not match the current database version.
ErrValidateVersionMismatch = errs.Class("validate db version mismatch")
// ErrValidateMinVersion is when the migration version does not match the current database version.
ErrValidateMinVersion = errs.Class("validate minimum version")
)
var Error = errs.Class("migrate")
Error is the default migrate errs class.
Functions ¶
func Create ¶
func Create(ctx context.Context, identifier string, db DBX) error
Create with a previous schema check.
func CreateSpanner ¶ added in v1.109.1
func CreateSpanner(ctx context.Context, identifier string, db DBX) error
CreateSpanner creates the migration schema necessary to execute migrations in Spanner.
Types ¶
type Action ¶
type Action interface {
Run(ctx context.Context, log *zap.Logger, db tagsql.DB, tx tagsql.Tx) error
}
Action is something that needs to be done.
type CreateDB ¶ added in v1.16.1
type CreateDB func(ctx context.Context, log *zap.Logger) error
CreateDB is operation for creating new dbs.
type DBX ¶
type DBX interface {
tagsql.DB
Schema() []string
Rebind(string) string
}
DBX contains additional methods for migrations.
type Func ¶
type Func func(ctx context.Context, log *zap.Logger, db tagsql.DB, tx tagsql.Tx) error
Func is an arbitrary operation.
type Migration ¶
type Migration struct {
// Table is the table name to register the applied migration version.
// NOTE: Always validates its value with the ValidTableName method before it's
// concatenated in a query string for avoiding SQL injection attacks.
Table string
Steps []*Step
}
Migration describes a migration steps.
func (*Migration) CurrentVersion ¶ added in v0.27.0
func (migration *Migration) CurrentVersion(ctx context.Context, log *zap.Logger, db tagsql.DB) (int, error)
CurrentVersion finds the latest version for the db.
func (*Migration) Run ¶
func (migration *Migration) Run(ctx context.Context, log *zap.Logger) error
Run runs the migration steps.
func (*Migration) TargetVersion ¶
func (migration *Migration) TargetVersion(version int) *Migration
TargetVersion returns migration with steps upto specified version.
func (*Migration) ValidTableName ¶
func (migration *Migration) ValidTableName() error
ValidTableName checks whether the specified table name is only formed by at least one character and its only formed by lowercase letters and underscores.
NOTE: if you change this function to accept a wider range of characters, make sure that they cannot open to SQL injections because Table field is used concatenated in some queries performed by Mitration methods.
func (*Migration) ValidateSteps ¶
func (migration *Migration) ValidateSteps() error
ValidateSteps checks that the version for each migration step increments in order.
func (*Migration) ValidateVersions ¶
func (migration *Migration) ValidateVersions(ctx context.Context, log *zap.Logger) error
ValidateVersions checks that the version of the migration matches the state of the database.
type Step ¶
type Step struct {
DB *tagsql.DB // The DB to execute this step on
Description string
Version int // Versions should start at 0
Action Action
CreateDB CreateDB
// SeparateTx marks a step as it should not be merged together for optimization.
// Cockroach cannot add a column and update the value in the same transaction.
SeparateTx bool
}
Step describes a single step in migration.