diff

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2023 License: MIT Imports: 16 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrColumnOrderingChanged is returned when the ordering of columns changes and column ordering is not ignored.
	// It is recommended to ignore column ordering changes to column order
	ErrColumnOrderingChanged = fmt.Errorf("column ordering changed: %w", ErrNotImplemented)
)
View Source
var ErrNotImplemented = fmt.Errorf("not implemented")

Functions

This section is empty.

Types

type MigrationHazard

type MigrationHazard struct {
	Type    MigrationHazardType
	Message string
}

MigrationHazard represents a hazard that a statement poses to a database

func (MigrationHazard) String

func (p MigrationHazard) String() string

type MigrationHazardType

type MigrationHazardType = string
const (
	MigrationHazardTypeAcquiresAccessExclusiveLock MigrationHazardType = "ACQUIRES_ACCESS_EXCLUSIVE_LOCK"
	MigrationHazardTypeAcquiresShareLock           MigrationHazardType = "ACQUIRES_SHARE_LOCK"
	MigrationHazardTypeDeletesData                 MigrationHazardType = "DELETES_DATA"
	MigrationHazardTypeHasUntrackableDependencies  MigrationHazardType = "HAS_UNTRACKABLE_DEPENDENCIES"
	MigrationHazardTypeIndexBuild                  MigrationHazardType = "INDEX_BUILD"
	MigrationHazardTypeIndexDropped                MigrationHazardType = "INDEX_DROPPED"
	MigrationHazardTypeImpactsDatabasePerformance  MigrationHazardType = "IMPACTS_DATABASE_PERFORMANCE"
	MigrationHazardTypeIsUserGenerated             MigrationHazardType = "IS_USER_GENERATED"
)

type Plan

type Plan struct {
	// Statements is the set of statements to be executed in order to migrate a database from schema A to schema B
	Statements []Statement
	// CurrentSchemaHash is the hash of the current schema, schema A. If you serialize this plans somewhere and
	// plan on running them later, you should verify that the current schema hash matches the current schema hash.
	// To get the current schema hash, you can use schema.GetPublicSchemaHash(ctx, conn)
	CurrentSchemaHash string
}

Plan represents a set of statements to be executed in order to migrate a database from schema A to schema B

func GeneratePlan

func GeneratePlan(ctx context.Context, conn *sql.Conn, tempDbFactory tempdb.Factory, newDDL []string, opts ...PlanOpt) (Plan, error)

GeneratePlan generates a migration plan to migrate the database to the target schema.

Parameters: conn: connection to the target database you wish to migrate. tempDbFactory: used to create a temporary database instance to extract the schema from the new DDL and validate the migration plan. It is recommended to use tempdb.NewOnInstanceFactory, or you can provide your own. newDDL: DDL encoding the new schema opts: Additional options to configure the plan generation

func (Plan) ApplyStatementTimeoutModifier

func (p Plan) ApplyStatementTimeoutModifier(regex *regexp.Regexp, timeout time.Duration) Plan

ApplyStatementTimeoutModifier applies the given timeout to all statements that match the given regex

func (Plan) InsertStatement

func (p Plan) InsertStatement(index int, statement Statement) (Plan, error)

InsertStatement inserts the given statement at the given index. If index is equal to the length of the statements, it will append the statement to the end of the statement in the plan

type PlanOpt

type PlanOpt func(opts *planOptions)

func WithDataPackNewTables

func WithDataPackNewTables() PlanOpt

WithDataPackNewTables configures the plan generation such that it packs the columns in the new tables to minimize padding. It will help minimize the storage used by the tables

func WithDoNotValidatePlan

func WithDoNotValidatePlan() PlanOpt

WithDoNotValidatePlan disables plan validation, where the migration plan is tested against a temporary database instance

func WithLogger

func WithLogger(logger log.Logger) PlanOpt

WithLogger configures plan generation to use the provided logger instead of the default

func WithRespectColumnOrder added in v0.2.0

func WithRespectColumnOrder() PlanOpt

WithRespectColumnOrder configures the plan generation to respect any changes to the ordering of columns in existing tables. You will most likely want this disabled, since column ordering changes are common

type Statement

type Statement struct {
	DDL string
	// The statement_timeout to apply to this statement. If implementing your own plan executor, be sure to set
	// the session-level statement_timeout to this value before executing the statement. A transaction-level statement_timeout
	// will not work since building indexes concurrently cannot be done in a transaction
	Timeout time.Duration
	// The hazards this statement poses
	Hazards []MigrationHazard
}

func (Statement) ToSQL

func (s Statement) ToSQL() string

Jump to

Keyboard shortcuts

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