schemadiff

package
v0.19.4 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: Apache-2.0 Imports: 21 Imported by: 2

Documentation

Index

Constants

View Source
const (
	AutoIncrementIgnore int = iota
	AutoIncrementApplyHigher
	AutoIncrementApplyAlways
)
View Source
const (
	RangeRotationFullSpec = iota
	RangeRotationDistinctStatements
	RangeRotationIgnore
)
View Source
const (
	ConstraintNamesIgnoreVitess = iota
	ConstraintNamesIgnoreAll
	ConstraintNamesStrict
)
View Source
const (
	ColumnRenameAssumeDifferent = iota
	ColumnRenameHeuristicStatement
)
View Source
const (
	TableRenameAssumeDifferent = iota
	TableRenameHeuristicStatement
)
View Source
const (
	FullTextKeyDistinctStatements = iota
	FullTextKeyUnifyStatements
)
View Source
const (
	TableCharsetCollateStrict int = iota
	TableCharsetCollateIgnoreEmpty
	TableCharsetCollateIgnoreAlways
)
View Source
const (
	TableQualifierDefault int = iota
	TableQualifierDeclared
)
View Source
const (
	AlterTableAlgorithmStrategyNone int = iota
	AlterTableAlgorithmStrategyInstant
	AlterTableAlgorithmStrategyInplace
	AlterTableAlgorithmStrategyCopy
)
View Source
const (
	EnumReorderStrategyAllow int = iota
	EnumReorderStrategyReject
)
View Source
const (
	ApplyDiffsNoConstraint = "ApplyDiffsNoConstraint"
	ApplyDiffsInOrder      = "ApplyDiffsInOrder"
	ApplyDiffsSequential   = "ApplyDiffsSequential"
)

Variables

View Source
var (
	ErrEntityTypeMismatch             = errors.New("mismatched entity type")
	ErrStrictIndexOrderingUnsupported = errors.New("strict index ordering is unsupported")
	ErrUnexpectedDiffAction           = errors.New("unexpected diff action")
	ErrUnexpectedTableSpec            = errors.New("unexpected table spec")
	ErrExpectedCreateTable            = errors.New("expected a CREATE TABLE statement")
	ErrExpectedCreateView             = errors.New("expected a CREATE VIEW statement")
)

Functions

func AlterTableCapableOfInstantDDL added in v0.19.0

func AlterTableCapableOfInstantDDL(alterTable *sqlparser.AlterTable, createTable *sqlparser.CreateTable, capableOf capabilities.CapableOf) (bool, error)

AlterTableCapableOfInstantDDL checks if the specific ALTER TABLE is eligible to run via ALGORITHM=INSTANT, given the existing table schema and the MySQL server capabilities. The function is intentionally public, as it is intended to be used by other packages, such as onlineddl.

func DDLActionStr

func DDLActionStr(diff EntityDiff) (string, error)

DDLActionStr returns the action implied by the given diff: CREATE", "DROP", "ALTER" or empty

func ExtractConstraintOriginalName

func ExtractConstraintOriginalName(tableName string, constraintName string) string

ExtractConstraintOriginalName extracts what used to be the constraint name before schemadiff/vitess generated a replacement name. e.g. input: "check1_7no794p1x6zw6je1gfqmt7bca", output: "check1"

func IsIntegralType added in v0.16.0

func IsIntegralType(columnType string) bool

Types

type AlterTableEntityDiff

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

func (*AlterTableEntityDiff) AlterTable

func (d *AlterTableEntityDiff) AlterTable() *sqlparser.AlterTable

AlterTable returns the underlying sqlparser.AlterTable that was generated for the diff.

func (*AlterTableEntityDiff) CanonicalStatementString

func (d *AlterTableEntityDiff) CanonicalStatementString() string

CanonicalStatementString implements EntityDiff

func (*AlterTableEntityDiff) Entities

func (d *AlterTableEntityDiff) Entities() (from Entity, to Entity)

Entities implements EntityDiff

func (*AlterTableEntityDiff) EntityName added in v0.17.0

func (d *AlterTableEntityDiff) EntityName() string

EntityName implements EntityDiff

func (*AlterTableEntityDiff) InstantDDLCapability added in v0.19.0

func (d *AlterTableEntityDiff) InstantDDLCapability() InstantDDLCapability

InstantDDLCapability implements EntityDiff

func (*AlterTableEntityDiff) IsEmpty

func (d *AlterTableEntityDiff) IsEmpty() bool

IsEmpty implements EntityDiff

func (*AlterTableEntityDiff) SetSubsequentDiff

func (d *AlterTableEntityDiff) SetSubsequentDiff(subDiff EntityDiff)

SetSubsequentDiff implements EntityDiff

func (*AlterTableEntityDiff) Statement

func (d *AlterTableEntityDiff) Statement() sqlparser.Statement

Statement implements EntityDiff

func (*AlterTableEntityDiff) StatementString

func (d *AlterTableEntityDiff) StatementString() (s string)

StatementString implements EntityDiff

func (*AlterTableEntityDiff) SubsequentDiff

func (d *AlterTableEntityDiff) SubsequentDiff() EntityDiff

SubsequentDiff implements EntityDiff

type AlterViewEntityDiff

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

func (*AlterViewEntityDiff) AlterView

func (d *AlterViewEntityDiff) AlterView() *sqlparser.AlterView

AlterView returns the underlying sqlparser.AlterView that was generated for the diff.

func (*AlterViewEntityDiff) CanonicalStatementString

func (d *AlterViewEntityDiff) CanonicalStatementString() string

CanonicalStatementString implements EntityDiff

func (*AlterViewEntityDiff) Entities

func (d *AlterViewEntityDiff) Entities() (from Entity, to Entity)

Entities implements EntityDiff

func (*AlterViewEntityDiff) EntityName added in v0.17.0

func (d *AlterViewEntityDiff) EntityName() string

EntityName implements EntityDiff

func (*AlterViewEntityDiff) InstantDDLCapability added in v0.19.0

func (d *AlterViewEntityDiff) InstantDDLCapability() InstantDDLCapability

InstantDDLCapability implements EntityDiff

func (*AlterViewEntityDiff) IsEmpty

func (d *AlterViewEntityDiff) IsEmpty() bool

IsEmpty implements EntityDiff

func (*AlterViewEntityDiff) SetSubsequentDiff

func (d *AlterViewEntityDiff) SetSubsequentDiff(EntityDiff)

SetSubsequentDiff implements EntityDiff

func (*AlterViewEntityDiff) Statement

func (d *AlterViewEntityDiff) Statement() sqlparser.Statement

Statement implements EntityDiff

func (*AlterViewEntityDiff) StatementString

func (d *AlterViewEntityDiff) StatementString() (s string)

StatementString implements EntityDiff

func (*AlterViewEntityDiff) SubsequentDiff

func (d *AlterViewEntityDiff) SubsequentDiff() EntityDiff

SubsequentDiff implements EntityDiff

type ApplyColumnAfterNotFoundError

type ApplyColumnAfterNotFoundError struct {
	Table       string
	Column      string
	AfterColumn string
}

func (*ApplyColumnAfterNotFoundError) Error

type ApplyColumnNotFoundError

type ApplyColumnNotFoundError struct {
	Table  string
	Column string
}

func (*ApplyColumnNotFoundError) Error

func (e *ApplyColumnNotFoundError) Error() string

type ApplyConstraintNotFoundError

type ApplyConstraintNotFoundError struct {
	Table      string
	Constraint string
}

func (*ApplyConstraintNotFoundError) Error

type ApplyDuplicateColumnError

type ApplyDuplicateColumnError struct {
	Table  string
	Column string
}

func (*ApplyDuplicateColumnError) Error

func (e *ApplyDuplicateColumnError) Error() string

type ApplyDuplicateConstraintError

type ApplyDuplicateConstraintError struct {
	Table      string
	Constraint string
}

func (*ApplyDuplicateConstraintError) Error

type ApplyDuplicateEntityError

type ApplyDuplicateEntityError struct {
	Entity string
}

func (*ApplyDuplicateEntityError) Error

func (e *ApplyDuplicateEntityError) Error() string

type ApplyDuplicateKeyError

type ApplyDuplicateKeyError struct {
	Table string
	Key   string
}

func (*ApplyDuplicateKeyError) Error

func (e *ApplyDuplicateKeyError) Error() string

type ApplyDuplicatePartitionError

type ApplyDuplicatePartitionError struct {
	Table     string
	Partition string
}

func (*ApplyDuplicatePartitionError) Error

type ApplyKeyNotFoundError

type ApplyKeyNotFoundError struct {
	Table string
	Key   string
}

func (*ApplyKeyNotFoundError) Error

func (e *ApplyKeyNotFoundError) Error() string

type ApplyNoPartitionsError

type ApplyNoPartitionsError struct {
	Table string
}

func (*ApplyNoPartitionsError) Error

func (e *ApplyNoPartitionsError) Error() string

type ApplyPartitionNotFoundError

type ApplyPartitionNotFoundError struct {
	Table     string
	Partition string
}

func (*ApplyPartitionNotFoundError) Error

type ApplyTableNotFoundError

type ApplyTableNotFoundError struct {
	Table string
}

func (*ApplyTableNotFoundError) Error

func (e *ApplyTableNotFoundError) Error() string

type ApplyViewNotFoundError

type ApplyViewNotFoundError struct {
	View string
}

func (*ApplyViewNotFoundError) Error

func (e *ApplyViewNotFoundError) Error() string

type ColumnDefinitionEntity

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

func (*ColumnDefinitionEntity) ColumnDiff

func (c *ColumnDefinitionEntity) ColumnDiff(
	env *Environment,
	tableName string,
	other *ColumnDefinitionEntity,
	t1cc *charsetCollate,
	t2cc *charsetCollate,
	hints *DiffHints,
) (*ModifyColumnDiff, error)

ColumnDiff compares this table statement with another table statement, and sees what it takes to change this table to look like the other table. It returns an ModifyColumnDiff statement if changes are found, or nil if not. The function also requires the charset/collate on the source & target tables. This is because the column's charset & collation, if undefined, are really defined by the table's charset & collation.

Anecdotally, in CreateTableEntity.normalize() we actually actively strip away the charset/collate properties
from the column definition, to get a cleaner table definition.

Things get complicated when we consider hints.TableCharsetCollateStrategy. Consider this test case:

from: "create table t (a varchar(64)) default charset=latin1",
to:   "create table t (a varchar(64) CHARACTER SET latin1 COLLATE latin1_bin)",

In both cases, the column is really a latin1. But the tables themselves have different collations. We need to denormalize the column's charset/collate properties, so that the comparison can be done.

func (*ColumnDefinitionEntity) IsTextual

func (c *ColumnDefinitionEntity) IsTextual() bool

IsTextual returns true when this column is of textual type, and is capable of having a character set property

type CreateTableEntity

type CreateTableEntity struct {
	*sqlparser.CreateTable
	Env *Environment
}

CreateTableEntity stands for a TABLE construct. It contains the table's CREATE statement.

func NewCreateTableEntity

func NewCreateTableEntity(env *Environment, c *sqlparser.CreateTable) (*CreateTableEntity, error)

func (*CreateTableEntity) Apply

func (c *CreateTableEntity) Apply(diff EntityDiff) (Entity, error)

Apply attempts to apply given ALTER TABLE diff onto the table defined by this entity. This entity is unmodified. If successful, a new CREATE TABLE entity is returned.

func (*CreateTableEntity) Clone added in v0.15.0

func (c *CreateTableEntity) Clone() Entity

func (*CreateTableEntity) Create

func (c *CreateTableEntity) Create() EntityDiff

Create implements Entity interface

func (*CreateTableEntity) Diff

func (c *CreateTableEntity) Diff(other Entity, hints *DiffHints) (EntityDiff, error)

Diff implements Entity interface function

func (*CreateTableEntity) Drop

func (c *CreateTableEntity) Drop() EntityDiff

Drop implements Entity interface

func (*CreateTableEntity) GetCharset added in v0.17.0

func (c *CreateTableEntity) GetCharset() string

GetCharset returns the explicit character set name specified in the CREATE TABLE statement (if any).

func (*CreateTableEntity) GetCollation added in v0.17.0

func (c *CreateTableEntity) GetCollation() string

GetCollation returns the explicit collation name specified in the CREATE TABLE statement (if any).

func (*CreateTableEntity) Name

func (c *CreateTableEntity) Name() string

Name implements Entity interface

func (*CreateTableEntity) TableDiff

func (c *CreateTableEntity) TableDiff(other *CreateTableEntity, hints *DiffHints) (*AlterTableEntityDiff, error)

TableDiff compares this table statement with another table statement, and sees what it takes to change this table to look like the other table. It returns an AlterTable statement if changes are found, or nil if not. the other table may be of different name; its name is ignored.

type CreateTableEntityDiff

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

func (*CreateTableEntityDiff) CanonicalStatementString

func (d *CreateTableEntityDiff) CanonicalStatementString() string

CanonicalStatementString implements EntityDiff

func (*CreateTableEntityDiff) CreateTable

func (d *CreateTableEntityDiff) CreateTable() *sqlparser.CreateTable

CreateTable returns the underlying sqlparser.CreateTable that was generated for the diff.

func (*CreateTableEntityDiff) Entities

func (d *CreateTableEntityDiff) Entities() (from Entity, to Entity)

Entities implements EntityDiff

func (*CreateTableEntityDiff) EntityName added in v0.17.0

func (d *CreateTableEntityDiff) EntityName() string

EntityName implements EntityDiff

func (*CreateTableEntityDiff) InstantDDLCapability added in v0.19.0

func (d *CreateTableEntityDiff) InstantDDLCapability() InstantDDLCapability

InstantDDLCapability implements EntityDiff

func (*CreateTableEntityDiff) IsEmpty

func (d *CreateTableEntityDiff) IsEmpty() bool

IsEmpty implements EntityDiff

func (*CreateTableEntityDiff) SetSubsequentDiff

func (d *CreateTableEntityDiff) SetSubsequentDiff(EntityDiff)

SetSubsequentDiff implements EntityDiff

func (*CreateTableEntityDiff) Statement

func (d *CreateTableEntityDiff) Statement() sqlparser.Statement

Statement implements EntityDiff

func (*CreateTableEntityDiff) StatementString

func (d *CreateTableEntityDiff) StatementString() (s string)

StatementString implements EntityDiff

func (*CreateTableEntityDiff) SubsequentDiff

func (d *CreateTableEntityDiff) SubsequentDiff() EntityDiff

SubsequentDiff implements EntityDiff

type CreateViewEntity

type CreateViewEntity struct {
	*sqlparser.CreateView
	// contains filtered or unexported fields
}

CreateViewEntity stands for a VIEW construct. It contains the view's CREATE statement.

func NewCreateViewEntity

func NewCreateViewEntity(env *Environment, c *sqlparser.CreateView) (*CreateViewEntity, error)

func (*CreateViewEntity) Apply

func (c *CreateViewEntity) Apply(diff EntityDiff) (Entity, error)

Apply attempts to apply given ALTER VIEW diff onto the view defined by this entity. This entity is unmodified. If successful, a new CREATE VIEW entity is returned.

func (*CreateViewEntity) Clone added in v0.15.0

func (c *CreateViewEntity) Clone() Entity

func (*CreateViewEntity) Create

func (c *CreateViewEntity) Create() EntityDiff

Create implements Entity interface

func (*CreateViewEntity) Diff

func (c *CreateViewEntity) Diff(other Entity, hints *DiffHints) (EntityDiff, error)

Diff implements Entity interface function

func (*CreateViewEntity) Drop

func (c *CreateViewEntity) Drop() EntityDiff

Drop implements Entity interface

func (*CreateViewEntity) Name

func (c *CreateViewEntity) Name() string

Name implements Entity interface

func (*CreateViewEntity) ViewDiff

ViewDiff compares this view statement with another view statement, and sees what it takes to change this view to look like the other view. It returns an AlterView statement if changes are found, or nil if not. the other view may be of different name; its name is ignored.

type CreateViewEntityDiff

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

func (*CreateViewEntityDiff) CanonicalStatementString

func (d *CreateViewEntityDiff) CanonicalStatementString() string

CanonicalStatementString implements EntityDiff

func (*CreateViewEntityDiff) CreateView

func (d *CreateViewEntityDiff) CreateView() *sqlparser.CreateView

CreateView returns the underlying sqlparser.CreateView that was generated for the diff.

func (*CreateViewEntityDiff) Entities

func (d *CreateViewEntityDiff) Entities() (from Entity, to Entity)

Entities implements EntityDiff

func (*CreateViewEntityDiff) EntityName added in v0.17.0

func (d *CreateViewEntityDiff) EntityName() string

EntityName implements EntityDiff

func (*CreateViewEntityDiff) InstantDDLCapability added in v0.19.0

func (d *CreateViewEntityDiff) InstantDDLCapability() InstantDDLCapability

InstantDDLCapability implements EntityDiff

func (*CreateViewEntityDiff) IsEmpty

func (d *CreateViewEntityDiff) IsEmpty() bool

IsEmpty implements EntityDiff

func (*CreateViewEntityDiff) SetSubsequentDiff

func (d *CreateViewEntityDiff) SetSubsequentDiff(EntityDiff)

SetSubsequentDiff implements EntityDiff

func (*CreateViewEntityDiff) Statement

func (d *CreateViewEntityDiff) Statement() sqlparser.Statement

Statement implements EntityDiff

func (*CreateViewEntityDiff) StatementString

func (d *CreateViewEntityDiff) StatementString() (s string)

StatementString implements EntityDiff

func (*CreateViewEntityDiff) SubsequentDiff

func (d *CreateViewEntityDiff) SubsequentDiff() EntityDiff

SubsequentDiff implements EntityDiff

type DiffDependency added in v0.17.0

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

DiffDependency indicates a dependency between two diffs, and the type of that dependency

func NewDiffDependency added in v0.17.0

func NewDiffDependency(diff EntityDiff, dependentDiff EntityDiff, typ DiffDependencyType) *DiffDependency

NewDiffDependency returns a new diff dependency pairing.

func (*DiffDependency) DependentDiff added in v0.17.0

func (d *DiffDependency) DependentDiff() EntityDiff

DependentDiff returns the diff that depends on the "benefactor" diff, ie must run 2nd

func (*DiffDependency) Diff added in v0.17.0

func (d *DiffDependency) Diff() EntityDiff

Diff returns the "benefactor" diff, on which DependentDiff() depends on, ie, should run 1st.

func (*DiffDependency) IsInOrder added in v0.19.0

func (d *DiffDependency) IsInOrder() bool

IsInOrder returns true if this dependency indicates a known order

func (*DiffDependency) IsSequential added in v0.19.0

func (d *DiffDependency) IsSequential() bool

IsSequential returns true if this is a sequential dependency

func (*DiffDependency) Type added in v0.17.0

Type returns the dependency type. Types are numeric and comparable: the higher the value, the stricter, or more constrained, the dependency is.

type DiffDependencyType added in v0.17.0

type DiffDependencyType int
const (
	DiffDependencyNone DiffDependencyType = iota // not a dependency
	DiffDependencyOrderUnknown
	DiffDependencyInOrderCompletion
	DiffDependencySequentialExecution
)

diff dependencies in increasing restriction severity

type DiffHints

type DiffHints struct {
	StrictIndexOrdering         bool
	AutoIncrementStrategy       int
	RangeRotationStrategy       int
	ConstraintNamesStrategy     int
	ColumnRenameStrategy        int
	TableRenameStrategy         int
	FullTextKeyStrategy         int
	TableCharsetCollateStrategy int
	TableQualifierHint          int
	AlterTableAlgorithmStrategy int
	EnumReorderStrategy         int
}

DiffHints is an assortment of rules for diffing entities

type DropTableEntityDiff

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

func (*DropTableEntityDiff) CanonicalStatementString

func (d *DropTableEntityDiff) CanonicalStatementString() string

CanonicalStatementString implements EntityDiff

func (*DropTableEntityDiff) DropTable

func (d *DropTableEntityDiff) DropTable() *sqlparser.DropTable

DropTable returns the underlying sqlparser.DropTable that was generated for the diff.

func (*DropTableEntityDiff) Entities

func (d *DropTableEntityDiff) Entities() (from Entity, to Entity)

Entities implements EntityDiff

func (*DropTableEntityDiff) EntityName added in v0.17.0

func (d *DropTableEntityDiff) EntityName() string

EntityName implements EntityDiff

func (*DropTableEntityDiff) InstantDDLCapability added in v0.19.0

func (d *DropTableEntityDiff) InstantDDLCapability() InstantDDLCapability

InstantDDLCapability implements EntityDiff

func (*DropTableEntityDiff) IsEmpty

func (d *DropTableEntityDiff) IsEmpty() bool

IsEmpty implements EntityDiff

func (*DropTableEntityDiff) SetSubsequentDiff

func (d *DropTableEntityDiff) SetSubsequentDiff(EntityDiff)

SetSubsequentDiff implements EntityDiff

func (*DropTableEntityDiff) Statement

func (d *DropTableEntityDiff) Statement() sqlparser.Statement

Statement implements EntityDiff

func (*DropTableEntityDiff) StatementString

func (d *DropTableEntityDiff) StatementString() (s string)

StatementString implements EntityDiff

func (*DropTableEntityDiff) SubsequentDiff

func (d *DropTableEntityDiff) SubsequentDiff() EntityDiff

SubsequentDiff implements EntityDiff

type DropViewEntityDiff

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

func (*DropViewEntityDiff) CanonicalStatementString

func (d *DropViewEntityDiff) CanonicalStatementString() string

CanonicalStatementString implements EntityDiff

func (*DropViewEntityDiff) DropView

func (d *DropViewEntityDiff) DropView() *sqlparser.DropView

DropView returns the underlying sqlparser.DropView that was generated for the diff.

func (*DropViewEntityDiff) Entities

func (d *DropViewEntityDiff) Entities() (from Entity, to Entity)

Entities implements EntityDiff

func (*DropViewEntityDiff) EntityName added in v0.17.0

func (d *DropViewEntityDiff) EntityName() string

EntityName implements EntityDiff

func (*DropViewEntityDiff) InstantDDLCapability added in v0.19.0

func (d *DropViewEntityDiff) InstantDDLCapability() InstantDDLCapability

InstantDDLCapability implements EntityDiff

func (*DropViewEntityDiff) IsEmpty

func (d *DropViewEntityDiff) IsEmpty() bool

IsEmpty implements EntityDiff

func (*DropViewEntityDiff) SetSubsequentDiff

func (d *DropViewEntityDiff) SetSubsequentDiff(EntityDiff)

SetSubsequentDiff implements EntityDiff

func (*DropViewEntityDiff) Statement

func (d *DropViewEntityDiff) Statement() sqlparser.Statement

Statement implements EntityDiff

func (*DropViewEntityDiff) StatementString

func (d *DropViewEntityDiff) StatementString() (s string)

StatementString implements EntityDiff

func (*DropViewEntityDiff) SubsequentDiff

func (d *DropViewEntityDiff) SubsequentDiff() EntityDiff

SubsequentDiff implements EntityDiff

type DuplicateKeyNameError added in v0.16.0

type DuplicateKeyNameError struct {
	Table string
	Key   string
}

func (*DuplicateKeyNameError) Error added in v0.16.0

func (e *DuplicateKeyNameError) Error() string

type Entity

type Entity interface {
	// Name of entity, ie table name, view name, etc.
	Name() string
	// Diff returns an entitty diff given another entity. The diff direction is from this entity and to the other entity.
	Diff(other Entity, hints *DiffHints) (diff EntityDiff, err error)
	// Create returns an entity diff that describes how to create this entity
	Create() EntityDiff
	// Drop returns an entity diff that describes how to drop this entity
	Drop() EntityDiff
	// Clone returns a deep copy of the entity.
	Clone() Entity
}

Entity stands for a database object we can diff: - A table - A view

type EntityDiff

type EntityDiff interface {
	// IsEmpty returns true when the two entities are considered identical
	IsEmpty() bool
	// EntityName returns the name of affected entity
	EntityName() string
	// Entities returns the two diffed entitied, aka "from" and "to"
	Entities() (from Entity, to Entity)
	// Statement returns a valid SQL statement that applies the diff, e.g. an ALTER TABLE ...
	// It returns nil if the diff is empty
	Statement() sqlparser.Statement
	// StatementString "stringifies" this diff's Statement(). It returns an empty string if the diff is empty
	StatementString() string
	// CanonicalStatementString "stringifies" this diff's Statement() to a canonical string. It returns an empty string if the diff is empty
	CanonicalStatementString() string
	// SubsequentDiff returns a followup diff to this one, if exists
	SubsequentDiff() EntityDiff
	// SetSubsequentDiff updates the existing subsequent diff to the given one
	SetSubsequentDiff(EntityDiff)
	// InstantDDLCapability returns the ability of this diff to run with ALGORITHM=INSTANT
	InstantDDLCapability() InstantDDLCapability
}

EntityDiff represents the diff between two entities

func AllSubsequent

func AllSubsequent(diff EntityDiff) (diffs []EntityDiff)

AllSubsequent returns a list of diffs starting the given diff and followed by all subsequent diffs, if any

func DiffCreateTablesQueries

func DiffCreateTablesQueries(env *Environment, query1 string, query2 string, hints *DiffHints) (EntityDiff, error)

DiffCreateTablesQueries compares two `CREATE TABLE ...` queries (in string form) and returns the diff from table1 to table2. Either or both of the queries can be empty. Based on this, the diff could be nil, CreateTable, DropTable or AlterTable

func DiffCreateViewsQueries

func DiffCreateViewsQueries(env *Environment, query1 string, query2 string, hints *DiffHints) (EntityDiff, error)

DiffCreateViewsQueries compares two `CREATE TABLE ...` queries (in string form) and returns the diff from table1 to table2. Either or both of the queries can be empty. Based on this, the diff could be nil, CreateView, DropView or AlterView

func DiffTables

func DiffTables(env *Environment, create1 *sqlparser.CreateTable, create2 *sqlparser.CreateTable, hints *DiffHints) (EntityDiff, error)

DiffTables compares two tables and returns the diff from table1 to table2. Either or both of the CreateTable statements can be nil. Based on this, the diff could be nil, CreateTable, DropTable or AlterTable

func DiffViews

func DiffViews(env *Environment, create1 *sqlparser.CreateView, create2 *sqlparser.CreateView, hints *DiffHints) (EntityDiff, error)

DiffViews compares two views and returns the diff from view1 to view2 Either or both of the CreateView statements can be nil. Based on this, the diff could be nil, CreateView, DropView or AlterView

type EntityNotFoundError added in v0.17.0

type EntityNotFoundError struct {
	Name string
}

func (*EntityNotFoundError) Error added in v0.17.0

func (e *EntityNotFoundError) Error() string

type EnumValueOrdinalChangedError added in v0.19.0

type EnumValueOrdinalChangedError struct {
	Table      string
	Column     string
	Value      string
	Ordinal    int
	NewOrdinal int
}

func (*EnumValueOrdinalChangedError) Error added in v0.19.0

type Environment added in v0.19.0

type Environment struct {
	*vtenv.Environment
	DefaultColl collations.ID
}

func NewEnv added in v0.19.0

func NewEnv(env *vtenv.Environment, defaultColl collations.ID) *Environment

func NewTestEnv added in v0.19.0

func NewTestEnv() *Environment

type ForeignKeyColumnCountMismatchError added in v0.16.0

type ForeignKeyColumnCountMismatchError struct {
	Table                 string
	Constraint            string
	ColumnCount           int
	ReferencedTable       string
	ReferencedColumnCount int
}

func (*ForeignKeyColumnCountMismatchError) Error added in v0.16.0

type ForeignKeyColumnTypeMismatchError added in v0.16.0

type ForeignKeyColumnTypeMismatchError struct {
	Table            string
	Constraint       string
	Column           string
	ReferencedTable  string
	ReferencedColumn string
}

func (*ForeignKeyColumnTypeMismatchError) Error added in v0.16.0

type ForeignKeyDependencyUnresolvedError added in v0.16.0

type ForeignKeyDependencyUnresolvedError struct {
	Table string
}

func (*ForeignKeyDependencyUnresolvedError) Error added in v0.16.0

type ForeignKeyLoopError added in v0.19.0

type ForeignKeyLoopError struct {
	Table string
	Loop  []string
}

func (*ForeignKeyLoopError) Error added in v0.19.0

func (e *ForeignKeyLoopError) Error() string

type ForeignKeyNonexistentReferencedTableError added in v0.19.0

type ForeignKeyNonexistentReferencedTableError struct {
	Table           string
	ReferencedTable string
}

func (*ForeignKeyNonexistentReferencedTableError) Error added in v0.19.0

type ForeignKeyReferencesViewError added in v0.19.0

type ForeignKeyReferencesViewError struct {
	Table          string
	ReferencedView string
}

func (*ForeignKeyReferencesViewError) Error added in v0.19.0

type ImpossibleApplyDiffOrderError added in v0.17.0

type ImpossibleApplyDiffOrderError struct {
	UnorderedDiffs   []EntityDiff
	ConflictingDiffs []EntityDiff
}

func (*ImpossibleApplyDiffOrderError) ConflictingStatements added in v0.17.0

func (e *ImpossibleApplyDiffOrderError) ConflictingStatements() (result []string)

func (*ImpossibleApplyDiffOrderError) Error added in v0.17.0

type IndexNeededByForeignKeyError added in v0.16.0

type IndexNeededByForeignKeyError struct {
	Table string
	Key   string
}

func (*IndexNeededByForeignKeyError) Error added in v0.16.0

type InstantDDLCapability added in v0.19.0

type InstantDDLCapability int
const (
	InstantDDLCapabilityUnknown InstantDDLCapability = iota
	InstantDDLCapabilityIrrelevant
	InstantDDLCapabilityImpossible
	InstantDDLCapabilityPossible
)

type InvalidColumnInCheckConstraintError

type InvalidColumnInCheckConstraintError struct {
	Table      string
	Constraint string
	Column     string
}

func (*InvalidColumnInCheckConstraintError) Error

type InvalidColumnInForeignKeyConstraintError

type InvalidColumnInForeignKeyConstraintError struct {
	Table      string
	Constraint string
	Column     string
}

func (*InvalidColumnInForeignKeyConstraintError) Error

type InvalidColumnInGeneratedColumnError

type InvalidColumnInGeneratedColumnError struct {
	Table           string
	Column          string
	GeneratedColumn string
}

func (*InvalidColumnInGeneratedColumnError) Error

type InvalidColumnInKeyError

type InvalidColumnInKeyError struct {
	Table  string
	Column string
	Key    string
}

func (*InvalidColumnInKeyError) Error

func (e *InvalidColumnInKeyError) Error() string

type InvalidColumnInPartitionError

type InvalidColumnInPartitionError struct {
	Table  string
	Column string
}

func (*InvalidColumnInPartitionError) Error

type InvalidColumnReferencedInViewError added in v0.17.0

type InvalidColumnReferencedInViewError struct {
	View      string
	Column    string
	Ambiguous bool
}

func (*InvalidColumnReferencedInViewError) Error added in v0.17.0

type InvalidReferencedColumnInForeignKeyConstraintError added in v0.16.0

type InvalidReferencedColumnInForeignKeyConstraintError struct {
	Table            string
	Constraint       string
	ReferencedTable  string
	ReferencedColumn string
}

func (*InvalidReferencedColumnInForeignKeyConstraintError) Error added in v0.16.0

type InvalidStarExprInViewError added in v0.17.0

type InvalidStarExprInViewError struct {
	View string
}

func (*InvalidStarExprInViewError) Error added in v0.17.0

type MissingForeignKeyReferencedIndexError added in v0.16.0

type MissingForeignKeyReferencedIndexError struct {
	Table           string
	Constraint      string
	ReferencedTable string
}

func (*MissingForeignKeyReferencedIndexError) Error added in v0.16.0

type MissingPartitionColumnInUniqueKeyError

type MissingPartitionColumnInUniqueKeyError struct {
	Table     string
	Column    string
	UniqueKey string
}

func (*MissingPartitionColumnInUniqueKeyError) Error

type ModifyColumnDiff

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

func NewModifyColumnDiff

func NewModifyColumnDiff(modifyColumn *sqlparser.ModifyColumn) *ModifyColumnDiff

func NewModifyColumnDiffByDefinition

func NewModifyColumnDiffByDefinition(definition *sqlparser.ColumnDefinition) *ModifyColumnDiff

type NotFullyParsedError

type NotFullyParsedError struct {
	Entity    string
	Statement string
}

func (*NotFullyParsedError) Error

func (e *NotFullyParsedError) Error() string

type RenameTableEntityDiff added in v0.15.0

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

func (*RenameTableEntityDiff) CanonicalStatementString added in v0.15.0

func (d *RenameTableEntityDiff) CanonicalStatementString() string

CanonicalStatementString implements EntityDiff

func (*RenameTableEntityDiff) Entities added in v0.15.0

func (d *RenameTableEntityDiff) Entities() (from Entity, to Entity)

Entities implements EntityDiff

func (*RenameTableEntityDiff) EntityName added in v0.17.0

func (d *RenameTableEntityDiff) EntityName() string

EntityName implements EntityDiff

func (*RenameTableEntityDiff) InstantDDLCapability added in v0.19.0

func (d *RenameTableEntityDiff) InstantDDLCapability() InstantDDLCapability

InstantDDLCapability implements EntityDiff

func (*RenameTableEntityDiff) IsEmpty added in v0.15.0

func (d *RenameTableEntityDiff) IsEmpty() bool

IsEmpty implements EntityDiff

func (*RenameTableEntityDiff) RenameTable added in v0.15.0

func (d *RenameTableEntityDiff) RenameTable() *sqlparser.RenameTable

RenameTable returns the underlying sqlparser.RenameTable that was generated for the diff.

func (*RenameTableEntityDiff) SetSubsequentDiff added in v0.15.0

func (d *RenameTableEntityDiff) SetSubsequentDiff(EntityDiff)

SetSubsequentDiff implements EntityDiff

func (*RenameTableEntityDiff) Statement added in v0.15.0

func (d *RenameTableEntityDiff) Statement() sqlparser.Statement

Statement implements EntityDiff

func (*RenameTableEntityDiff) StatementString added in v0.15.0

func (d *RenameTableEntityDiff) StatementString() (s string)

StatementString implements EntityDiff

func (*RenameTableEntityDiff) SubsequentDiff added in v0.15.0

func (d *RenameTableEntityDiff) SubsequentDiff() EntityDiff

SubsequentDiff implements EntityDiff

type Schema

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

Schema represents a database schema, which may contain entities such as tables and views. Schema is not in itself an Entity, since it is more of a collection of entities.

func NewSchemaFromEntities

func NewSchemaFromEntities(env *Environment, entities []Entity) (*Schema, error)

NewSchemaFromEntities creates a valid and normalized schema based on list of entities

func NewSchemaFromQueries

func NewSchemaFromQueries(env *Environment, queries []string) (*Schema, error)

NewSchemaFromQueries creates a valid and normalized schema based on list of queries

func NewSchemaFromSQL

func NewSchemaFromSQL(env *Environment, sql string) (*Schema, error)

NewSchemaFromSQL creates a valid and normalized schema based on a SQL blob that contains CREATE statements for various objects (tables, views)

func NewSchemaFromStatements

func NewSchemaFromStatements(env *Environment, statements []sqlparser.Statement) (*Schema, error)

NewSchemaFromStatements creates a valid and normalized schema based on list of valid statements

func (*Schema) Apply

func (s *Schema) Apply(diffs []EntityDiff) (*Schema, error)

Apply attempts to apply given list of diffs to the schema described by this object. These diffs are CREATE/DROP/ALTER TABLE/VIEW. The operation does not modify this object. Instead, if successful, a new (modified) Schema is returned.

func (*Schema) Entities

func (s *Schema) Entities() []Entity

Entities returns this schema's entities in good order (may be applied without error)

func (*Schema) Entity

func (s *Schema) Entity(name string) Entity

Entity returns an entity by name, or nil if nonexistent

func (*Schema) EntityNames

func (s *Schema) EntityNames() []string

EntityNames is a convenience function that returns just the names of entities, in good order

func (*Schema) SchemaDiff added in v0.17.0

func (s *Schema) SchemaDiff(other *Schema, hints *DiffHints) (*SchemaDiff, error)

SchemaDiff calculates a rich diff between this schema and the given schema. It builds on top of diff(): on top of the list of diffs that can take this schema into the given schema, this function also evaluates the dependencies between those diffs, if any, and the resulting SchemaDiff object offers OrderedDiffs(), the safe ordering of diffs that, when applied sequentially, does not produce any conflicts and keeps schema valid at each step.

func (*Schema) Table

func (s *Schema) Table(name string) *CreateTableEntity

Table returns a table by name, or nil if nonexistent

func (*Schema) TableNames

func (s *Schema) TableNames() []string

TableNames is a convenience function that returns just the names of tables, in good order

func (*Schema) Tables

func (s *Schema) Tables() []*CreateTableEntity

Tables returns this schema's tables in good order (may be applied without error)

func (*Schema) ToQueries

func (s *Schema) ToQueries() []string

ToQueries returns an ordered list of queries which can be applied to create the schema

func (*Schema) ToSQL

func (s *Schema) ToSQL() string

ToSQL returns a SQL blob with ordered sequence of queries which can be applied to create the schema

func (*Schema) ToStatements

func (s *Schema) ToStatements() []sqlparser.Statement

ToStatements returns an ordered list of statements which can be applied to create the schema

func (*Schema) ValidateViewReferences added in v0.17.0

func (s *Schema) ValidateViewReferences() error

func (*Schema) View

func (s *Schema) View(name string) *CreateViewEntity

View returns a view by name, or nil if nonexistent

func (*Schema) ViewNames

func (s *Schema) ViewNames() []string

ViewNames is a convenience function that returns just the names of views, in good order

func (*Schema) Views

func (s *Schema) Views() []*CreateViewEntity

Views returns this schema's views in good order (may be applied without error)

type SchemaDiff added in v0.17.0

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

SchemaDiff is a rich diff between two schemas. It includes the following: - The source schema (on which the diff would operate) - A list of SQL diffs (e.g. CREATE VIEW, ALTER TABLE, ...) - A map of dependencies between the diffs Operations on SchemaDiff are not concurrency-safe.

func DiffSchemas

func DiffSchemas(env *Environment, schema1 *Schema, schema2 *Schema, hints *DiffHints) (*SchemaDiff, error)

DiffSchemas compares two schemas and returns the list of diffs that turn 1st schema into 2nd. Any of the schemas may be nil.

func DiffSchemasSQL

func DiffSchemasSQL(env *Environment, sql1 string, sql2 string, hints *DiffHints) (*SchemaDiff, error)

DiffSchemasSQL compares two schemas and returns the rich diff that turns 1st schema into 2nd. Schemas are build from SQL, each of which can contain an arbitrary number of CREATE TABLE and CREATE VIEW statements.

func NewSchemaDiff added in v0.17.0

func NewSchemaDiff(schema *Schema) *SchemaDiff

func (*SchemaDiff) AllDependenciess added in v0.17.0

func (d *SchemaDiff) AllDependenciess() (deps []*DiffDependency)

AllDependenciess returns all known dependencies

func (*SchemaDiff) AllSequentialExecutionDependencies added in v0.17.0

func (d *SchemaDiff) AllSequentialExecutionDependencies() (deps []*DiffDependency)

AllSequentialExecutionDependencies returns all diffs that are of "sequential execution" type.

func (*SchemaDiff) Empty added in v0.17.0

func (d *SchemaDiff) Empty() bool

Empty returns 'true' when there are no diff entries

func (*SchemaDiff) HasDependencies added in v0.17.0

func (d *SchemaDiff) HasDependencies() bool

HasDependencies returns `true` if there is at least one known diff dependency. If this function returns `false` then that means there is no restriction whatsoever to the order of diffs.

func (*SchemaDiff) HasSequentialExecutionDependencies added in v0.17.0

func (d *SchemaDiff) HasSequentialExecutionDependencies() bool

HasSequentialExecutionDependencies return `true` if there is at least one "subsequential execution" type diff. If not, that means all diffs can be applied in parallel.

func (*SchemaDiff) InstantDDLCapability added in v0.19.0

func (d *SchemaDiff) InstantDDLCapability() InstantDDLCapability

InstantDDLCapability returns an overall summary of the ability of the diffs to run with ALGORITHM=INSTANT. It is a convenience method, whose logic anyone can reimplement.

func (*SchemaDiff) OrderedDiffs added in v0.17.0

func (d *SchemaDiff) OrderedDiffs(ctx context.Context) ([]EntityDiff, error)

OrderedDiffs returns the list of diff in applicable order, if possible. This is a linearized representation where diffs may be applied in-order one after another, keeping the schema in valid state at all times.

func (*SchemaDiff) UnorderedDiffs added in v0.17.0

func (d *SchemaDiff) UnorderedDiffs() []EntityDiff

UnorderedDiffs returns all the diffs. These are not sorted by dependencies. These are basically the original diffs, but "flattening" any subsequent diffs they may have. as result: - Diffs in the returned slice have no subsequent diffs - The returned slice may be longer than the number of diffs supplied by loadDiffs()

type UnknownColumnCharsetCollationError added in v0.19.0

type UnknownColumnCharsetCollationError struct {
	Column  string
	Charset string
}

func (*UnknownColumnCharsetCollationError) Error added in v0.19.0

type UnknownColumnCollationCharsetError added in v0.19.0

type UnknownColumnCollationCharsetError struct {
	Column    string
	Collation string
}

func (*UnknownColumnCollationCharsetError) Error added in v0.19.0

type UnsupportedApplyOperationError

type UnsupportedApplyOperationError struct {
	Statement string
}

func (*UnsupportedApplyOperationError) Error

type UnsupportedEntityError

type UnsupportedEntityError struct {
	Entity    string
	Statement string
}

func (*UnsupportedEntityError) Error

func (e *UnsupportedEntityError) Error() string

type UnsupportedStatementError

type UnsupportedStatementError struct {
	Statement string
}

func (*UnsupportedStatementError) Error

func (e *UnsupportedStatementError) Error() string

type UnsupportedTableOptionError

type UnsupportedTableOptionError struct {
	Table  string
	Option string
}

func (*UnsupportedTableOptionError) Error

type ViewDependencyUnresolvedError added in v0.15.0

type ViewDependencyUnresolvedError struct {
	View string
}

func (*ViewDependencyUnresolvedError) Error added in v0.15.0

Jump to

Keyboard shortcuts

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