tengo

package module
v0.8.17 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2019 License: Apache-2.0 Imports: 22 Imported by: 0

README

Go La Tengo

build status code coverage godoc latest release

Golang library for MySQL database automation

Features

Most of Go La Tengo's current functionality is focused on MySQL schema introspection and diff'ing. Future releases will add more general-purpose automation features.

Schema introspection

Go La Tengo examines several information_schema tables in order to build Go struct values representing schemas (databases), tables, columns, indexes, foreign key constraints, stored procedures, and functions. These values can be diff'ed to generate corresponding DDL statements.

Instance modeling

The tengo.Instance struct models a single database instance. It keeps track of multiple, separate connection pools for using different default schema and session settings. This helps to avoid problems with Go's database/sql methods, which are incompatible with USE statements and SET SESSION statements.

Status

This is beta software. The API is subject to change. Backwards-incompatible changes are generally avoided, but no guarantees are made yet. Documentation and usage examples have not yet been completed.

Supported databases

Tagged releases are tested against the following databases, all running on Linux:

  • MySQL 5.5, 5.6, 5.7, 8.0
  • Percona Server 5.5, 5.6, 5.7, 8.0
  • MariaDB 10.1, 10.2, 10.3

Outside of a tagged release, every commit to the master branch is automatically tested against MySQL 5.6 and 5.7.

Unsupported in diffs

Go La Tengo cannot diff tables containing any of the following MySQL features yet:

  • partitioned tables
  • triggers
  • fulltext indexes
  • spatial types
  • special features of non-InnoDB storage engines
  • generated/virtual columns (MySQL 5.7+ / Percona Server 5.7+ / MariaDB 5.2+)
  • column-level compression, with or without predefined dictionary (Percona Server 5.6.33+)
  • DEFAULT expressions (MariaDB 10.2+)
  • CHECK constraints (MariaDB 10.2+)

This list is not necessarily exhaustive. Many of these will be implemented in subsequent releases.

Go La Tengo also does not yet support rename operations, e.g. column renames or table renames.

External Dependencies

Credits

Created and maintained by @evanelias.

Additional contributions by:

Support for stored procedures and functions generously sponsored by Psyonix.

License

Copyright 2019 Skeema LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package tengo (Go La Tengo) is a database automation library. In its current form, its functionality is focused on MySQL schema introspection and diff'ing. Future releases will add more general-purpose automation features.

Index

Constants

This section is empty.

Variables

View Source
var ColumnDefaultNull = ColumnDefault{Null: true}

ColumnDefaultNull indicates a column has a default value of NULL.

View Source
var FlavorMariaDB101 = Flavor{VendorMariaDB, 10, 1}

FlavorMariaDB101 represents MariaDB 10.1.x

View Source
var FlavorMariaDB102 = Flavor{VendorMariaDB, 10, 2}

FlavorMariaDB102 represents MariaDB 10.2.x

View Source
var FlavorMariaDB103 = Flavor{VendorMariaDB, 10, 3}

FlavorMariaDB103 represents MariaDB 10.3.x

View Source
var FlavorMySQL55 = Flavor{VendorMySQL, 5, 5}

FlavorMySQL55 represents MySQL 5.5.x

View Source
var FlavorMySQL56 = Flavor{VendorMySQL, 5, 6}

FlavorMySQL56 represents MySQL 5.6.x

View Source
var FlavorMySQL57 = Flavor{VendorMySQL, 5, 7}

FlavorMySQL57 represents MySQL 5.7.x

View Source
var FlavorMySQL80 = Flavor{VendorMySQL, 8, 0}

FlavorMySQL80 represents MySQL 8.0.x

View Source
var FlavorPercona55 = Flavor{VendorPercona, 5, 5}

FlavorPercona55 represents Percona Server 5.5.x

View Source
var FlavorPercona56 = Flavor{VendorPercona, 5, 6}

FlavorPercona56 represents Percona Server 5.6.x

View Source
var FlavorPercona57 = Flavor{VendorPercona, 5, 7}

FlavorPercona57 represents Percona Server 5.7.x

View Source
var FlavorPercona80 = Flavor{VendorPercona, 8, 0}

FlavorPercona80 represents Percona Server 8.0.x

View Source
var FlavorUnknown = Flavor{VendorUnknown, 0, 0}

FlavorUnknown represents a flavor that cannot be parsed. This is the zero value for Flavor.

Functions

func EscapeIdentifier

func EscapeIdentifier(input string) string

EscapeIdentifier is for use in safely escaping MySQL identifiers (table names, column names, etc). It doubles any backticks already present in the input string, and then returns the string wrapped in outer backticks.

func EscapeValueForCreateTable

func EscapeValueForCreateTable(input string) string

EscapeValueForCreateTable returns the supplied value (typically obtained from querying an information_schema table) escaped in the same manner as SHOW CREATE TABLE would display it. Examples include default values, table comments, column comments, index comments.

func IsAccessError

func IsAccessError(err error) bool

IsAccessError returns true if err indicates an authentication or authorization problem, at connection time or query time. Can be a problem with credentials, client host, no access to requested default database, missing privilege, etc. There is no sense in immediately retrying the connection or query when encountering this type of error.

func IsDatabaseError

func IsDatabaseError(err error, specificErrors ...uint16) bool

IsDatabaseError returns true if err came from a database server, typically as a response to a query or connection attempt. If one or more specificErrors are supplied, IsDatabaseError only returns true if the database error code matched one of those numbers.

func IsForbiddenDiff

func IsForbiddenDiff(err error) bool

IsForbiddenDiff returns true if err represents an "unsafe" alteration that has not explicitly been permitted by the supplied StatementModifiers.

func IsSyntaxError

func IsSyntaxError(err error) bool

IsSyntaxError returns true if err is a SQL syntax error, or false otherwise.

func IsUnsupportedDiff

func IsUnsupportedDiff(err error) bool

IsUnsupportedDiff returns true if err represents an object that cannot be diff'ed due to use of features not supported by this package.

func NormalizeCreateOptions

func NormalizeCreateOptions(createStmt string) string

NormalizeCreateOptions adjusts the supplied CREATE TABLE statement to remove any no-op table options that are persisted in SHOW CREATE TABLE, but not reflected in information_schema and serve no purpose for InnoDB tables. This function is not guaranteed to be safe for non-InnoDB tables.

func ParseCreateAutoInc

func ParseCreateAutoInc(createStmt string) (string, uint64)

ParseCreateAutoInc parses a CREATE TABLE statement, formatted in the same manner as SHOW CREATE TABLE, and removes the table-level next-auto-increment clause if present. The modified CREATE TABLE will be returned, along with the next auto-increment value if one was found.

func ParseVersion

func ParseVersion(version string) (result [3]int)

ParseVersion takes a version string (e.g. @@version variable from MySQL) and returns a 3-element array of major, minor, and patch numbers. If parsing failed, the returned value will be {0, 0, 0}.

func RunSuite

func RunSuite(suite IntegrationTestSuite, t *testing.T, backends []string)

RunSuite runs all test methods in the supplied suite once per backend. It calls suite.Setup(backend) once per backend, then iterates through all Test methods in suite. For each test method, suite.BeforeTest will be run, followed by the test itself. Finally, suite.Teardown(backend) will be run. Backends are just strings, and may contain docker image names or any other string representation that the test suite understands.

func SplitEnv

func SplitEnv(key string) []string

SplitEnv examines the specified environment variable and splits its value on commas to return a list of strings. Note that if the env variable is blank or unset, an empty slice will be returned; this behavior differs from that of strings.Split.

func SplitHostOptionalPort

func SplitHostOptionalPort(hostaddr string) (string, int, error)

SplitHostOptionalPort takes an address string containing a hostname, ipv4 addr, or ipv6 addr; *optionally* followed by a colon and port number. It splits the hostname portion from the port portion and returns them separately. If no port was present, 0 will be returned for that portion. If hostaddr contains an ipv6 address, the IP address portion must be wrapped in brackets on input, and the brackets will still be present on output.

func UseFilteredDriverLogger

func UseFilteredDriverLogger()

UseFilteredDriverLogger overrides the mysql driver's logger to avoid excessive messages. This suppresses the driver's "unexpected EOF" output, which occurs when an initial connection is refused or a connection drops early. This excessive logging can occur whenever DockerClient.CreateInstance() or DockerClient.GetInstance() is waiting for the instance to finish starting.

Types

type AddColumn

type AddColumn struct {
	Table         *Table
	Column        *Column
	PositionFirst bool
	PositionAfter *Column
}

AddColumn represents a new column that is present on the right-side ("to") schema version of the table, but not the left-side ("from") version. It satisfies the TableAlterClause interface.

func (AddColumn) Clause

func (ac AddColumn) Clause(mods StatementModifiers) string

Clause returns an ADD COLUMN clause of an ALTER TABLE statement.

type AddForeignKey

type AddForeignKey struct {
	ForeignKey *ForeignKey
	// contains filtered or unexported fields
}

AddForeignKey represents a new foreign key that is present on the right-side ("to") schema version of the table, but not the left-side ("from") version. It satisfies the TableAlterClause interface.

func (AddForeignKey) Clause

func (afk AddForeignKey) Clause(mods StatementModifiers) string

Clause returns an ADD CONSTRAINT ... FOREIGN KEY clause of an ALTER TABLE statement.

type AddIndex

type AddIndex struct {
	Index *Index
	// contains filtered or unexported fields
}

AddIndex represents an index that is present on the right-side ("to") schema version of the table, but was not identically present on the left- side ("from") version. It satisfies the TableAlterClause interface.

func (AddIndex) Clause

func (ai AddIndex) Clause(mods StatementModifiers) string

Clause returns an ADD KEY clause of an ALTER TABLE statement.

type ChangeAutoIncrement

type ChangeAutoIncrement struct {
	OldNextAutoIncrement uint64
	NewNextAutoIncrement uint64
}

ChangeAutoIncrement represents a difference in next-auto-increment value between two versions of a table. It satisfies the TableAlterClause interface.

func (ChangeAutoIncrement) Clause

Clause returns an AUTO_INCREMENT clause of an ALTER TABLE statement.

type ChangeCharSet

type ChangeCharSet struct {
	CharSet   string
	Collation string // blank string means "default collation for CharSet"
}

ChangeCharSet represents a difference in default character set and/or collation between two versions of a table. It satisfies the TableAlterClause interface.

func (ChangeCharSet) Clause

func (ccs ChangeCharSet) Clause(_ StatementModifiers) string

Clause returns a DEFAULT CHARACTER SET clause of an ALTER TABLE statement.

type ChangeComment

type ChangeComment struct {
	NewComment string
}

ChangeComment represents a difference in the table-level comment between two versions of a table. It satisfies the TableAlterClause interface.

func (ChangeComment) Clause

Clause returns a clause of an ALTER TABLE statement that changes a table's comment.

type ChangeCreateOptions

type ChangeCreateOptions struct {
	OldCreateOptions string
	NewCreateOptions string
}

ChangeCreateOptions represents a difference in the create options (row_format, stats_persistent, stats_auto_recalc, etc) between two versions of a table. It satisfies the TableAlterClause interface.

func (ChangeCreateOptions) Clause

Clause returns a clause of an ALTER TABLE statement that sets one or more create options.

type ChangeStorageEngine

type ChangeStorageEngine struct {
	NewStorageEngine string
}

ChangeStorageEngine represents a difference in the table's storage engine. It satisfies the TableAlterClause interface. Please note that Go La Tengo's support for non-InnoDB storage engines is currently very limited, however it still provides the ability to generate ALTERs that change engine.

func (ChangeStorageEngine) Clause

Clause returns a clause of an ALTER TABLE statement that changes a table's storage engine.

func (ChangeStorageEngine) Unsafe

func (cse ChangeStorageEngine) Unsafe() bool

Unsafe returns true if this clause is potentially destructive of data. ChangeStorageEngine is always considered unsafe, due to the potential complexity in converting a table's data to the new storage engine.

type Column

type Column struct {
	Name               string
	TypeInDB           string
	Nullable           bool
	AutoIncrement      bool
	Default            ColumnDefault
	OnUpdate           string
	CharSet            string // Only populated if textual type
	Collation          string // Only populated if textual type
	CollationIsDefault bool   // Only populated if textual type; indicates default for CharSet
	Comment            string
}

Column represents a single column of a table.

func (*Column) Definition

func (c *Column) Definition(flavor Flavor, table *Table) string

Definition returns this column's definition clause, for use as part of a DDL statement. A table may optionally be supplied, which simply causes CHARACTER SET clause to be omitted if the table and column have the same *collation* (mirroring the specific display logic used by SHOW CREATE TABLE)

func (*Column) Equals

func (c *Column) Equals(other *Column) bool

Equals returns true if two columns are identical, false otherwise.

type ColumnDefault

type ColumnDefault struct {
	Null   bool
	Quoted bool
	Value  string
}

ColumnDefault represents the default value for a column.

func ColumnDefaultExpression

func ColumnDefaultExpression(expression string) ColumnDefault

ColumnDefaultExpression is a constructor for creating a default value that represents a SQL expression, which won't be wrapped in quotes. Examples include "CURRENT_TIMESTAMP", "CURRENT_TIMESTAMP(N)" where N is a digit for fractional precision, bit-value literals "b'N'" where N is a value expressed in binary, or arbitrary default expressions which some flavors support.

func ColumnDefaultValue

func ColumnDefaultValue(value string) ColumnDefault

ColumnDefaultValue is a constructor for creating non-NULL, non-CURRENT_TIMESTAMP default values.

func (ColumnDefault) Clause

func (cd ColumnDefault) Clause(flavor Flavor, col *Column) string

Clause returns the DEFAULT clause for use in a DDL statement. If non-blank, it will be prefixed with a space.

type DatabaseDiff added in v0.8.13

type DatabaseDiff struct {
	From *Schema
	To   *Schema
}

DatabaseDiff represents differences of schema characteristics (default character set or default collation), or a difference in the existence of the the schema.

func (*DatabaseDiff) DiffType added in v0.8.13

func (dd *DatabaseDiff) DiffType() DiffType

DiffType returns the type of diff operation.

func (*DatabaseDiff) ObjectKey added in v0.8.14

func (dd *DatabaseDiff) ObjectKey() ObjectKey

ObjectKey returns a value representing the type and name of the schema being diff'ed. The type is always ObjectTypeDatabase. The name will be the From side schema, unless it is nil (CREATE DATABASE), in which case the To side schema name is returned.

func (*DatabaseDiff) Statement added in v0.8.13

func (dd *DatabaseDiff) Statement(_ StatementModifiers) (string, error)

Statement returns a DDL statement corresponding to the DatabaseDiff. A blank string may be returned if there is no statement to execute.

type DiffType added in v0.8.13

type DiffType int

DiffType enumerates possible ways that two objects differ

const (
	DiffTypeNone DiffType = iota
	DiffTypeCreate
	DiffTypeDrop
	DiffTypeAlter
	DiffTypeRename
)

Constants representing the types of diff operations.

func (DiffType) String added in v0.8.13

func (dt DiffType) String() string

type DockerClient added in v0.8.8

type DockerClient struct {
	Options DockerClientOptions
	// contains filtered or unexported fields
}

DockerClient manages lifecycle of local Docker containers for sandbox database instances. It wraps and hides the implementation of a specific Docker client implementation. (This package currently uses github.com/fsouza/go-dockerclient, but may later switch to the official Docker Golang client.)

func NewDockerClient added in v0.8.8

func NewDockerClient(opts DockerClientOptions) (*DockerClient, error)

NewDockerClient is a constructor for DockerClient

func (*DockerClient) CreateInstance added in v0.8.8

func (dc *DockerClient) CreateInstance(opts DockerizedInstanceOptions) (*DockerizedInstance, error)

CreateInstance attempts to create a Docker container with the supplied name (any arbitrary name, or blank to assign random) and image (such as "mysql:5.6", or just "mysql" to indicate latest). A connection pool will be established for the instance.

func (*DockerClient) GetInstance added in v0.8.8

GetInstance attempts to find an existing container with name equal to opts.Name. If a non-blank opts.Image is supplied, and the container exists but has a different image, an error will be returned. Otherwise, if the container is found, it will be started if not already running, and a connection pool will be established. If the container does not exist or cannot be started or connected to, a nil *DockerizedInstance and a non-nil error will be returned.

func (*DockerClient) GetOrCreateInstance added in v0.8.8

func (dc *DockerClient) GetOrCreateInstance(opts DockerizedInstanceOptions) (*DockerizedInstance, error)

GetOrCreateInstance attempts to fetch an existing Docker container with name equal to opts.Name. If it exists and its image matches opts.Image, and there are no errors starting or connecting to the instance, it will be returned. If it exists but its image doesn't match, or it cannot be started or connected to, an error will be returned. If no container exists with this name, a new one will attempt to be created.

type DockerClientOptions added in v0.8.8

type DockerClientOptions struct{}

DockerClientOptions specifies options when instantiating a Docker client. No options are currently supported, but this may change in the future.

type DockerizedInstance

type DockerizedInstance struct {
	*Instance
	DockerizedInstanceOptions
	Manager *DockerClient
	// contains filtered or unexported fields
}

DockerizedInstance is a database instance running in a local Docker container.

func (*DockerizedInstance) DSN

func (di *DockerizedInstance) DSN() string

DSN returns a github.com/go-sql-driver/mysql formatted DSN corresponding to its containerized mysql-server instance.

func (*DockerizedInstance) Destroy

func (di *DockerizedInstance) Destroy() error

Destroy stops and deletes the corresponding containerized mysql-server.

func (*DockerizedInstance) NukeData

func (di *DockerizedInstance) NukeData() error

NukeData drops all non-system schemas and tables in the containerized mysql-server, making it useful as a per-test cleanup method in implementations of IntegrationTestSuite.BeforeTest.

func (*DockerizedInstance) Port

func (di *DockerizedInstance) Port() int

Port returns the actual port number on localhost that maps to the container's internal port 3306.

func (*DockerizedInstance) SourceSQL

func (di *DockerizedInstance) SourceSQL(filePath string) (string, error)

SourceSQL reads the specified file and executes it against the containerized mysql-server. The file should contain one or more valid SQL instructions, typically a mix of DML and/or DDL statements. It is useful as a per-test setup method in implementations of IntegrationTestSuite.BeforeTest.

func (*DockerizedInstance) Start added in v0.8.2

func (di *DockerizedInstance) Start() error

Start starts the corresponding containerized mysql-server. If it is not already running, an error will be returned if it cannot be started. If it is already running, nil will be returned.

func (*DockerizedInstance) Stop added in v0.8.2

func (di *DockerizedInstance) Stop() error

Stop halts the corresponding containerized mysql-server, but does not destroy the container. The connection pool will be removed. If the container was not already running, nil will be returned.

func (*DockerizedInstance) String

func (di *DockerizedInstance) String() string

func (*DockerizedInstance) TryConnect added in v0.8.3

func (di *DockerizedInstance) TryConnect() (err error)

TryConnect sets up a connection pool to the containerized mysql-server, and tests connectivity. It returns an error if a connection cannot be established within 30 seconds.

type DockerizedInstanceOptions added in v0.8.8

type DockerizedInstanceOptions struct {
	Name              string
	Image             string
	RootPassword      string
	DefaultConnParams string
}

DockerizedInstanceOptions specifies options for creating or finding a sandboxed database instance inside a Docker container.

type DropColumn

type DropColumn struct {
	Column *Column
}

DropColumn represents a column that was present on the left-side ("from") schema version of the table, but not the right-side ("to") version. It satisfies the TableAlterClause interface.

func (DropColumn) Clause

func (dc DropColumn) Clause(_ StatementModifiers) string

Clause returns a DROP COLUMN clause of an ALTER TABLE statement.

func (DropColumn) Unsafe

func (dc DropColumn) Unsafe() bool

Unsafe returns true if this clause is potentially destructive of data. DropColumn is always unsafe.

type DropForeignKey

type DropForeignKey struct {
	ForeignKey *ForeignKey
	// contains filtered or unexported fields
}

DropForeignKey represents a foreign key that was present on the left-side ("from") schema version of the table, but not the right-side ("to") version. It satisfies the TableAlterClause interface.

func (DropForeignKey) Clause

func (dfk DropForeignKey) Clause(mods StatementModifiers) string

Clause returns a DROP FOREIGN KEY clause of an ALTER TABLE statement.

type DropIndex

type DropIndex struct {
	Index *Index
	// contains filtered or unexported fields
}

DropIndex represents an index that was present on the left-side ("from") schema version of the table, but not identically present the right-side ("to") version. It satisfies the TableAlterClause interface.

func (DropIndex) Clause

func (di DropIndex) Clause(mods StatementModifiers) string

Clause returns a DROP KEY clause of an ALTER TABLE statement.

type Flavor

type Flavor struct {
	Vendor Vendor
	Major  int
	Minor  int
}

Flavor represents a database server release, including vendor along with major and minor version number.

func NewFlavor added in v0.8.2

func NewFlavor(base string, versionParts ...int) Flavor

NewFlavor returns a Flavor value based on its inputs, which can either be in the form of NewFlavor("vendor", major, minor) or NewFlavor("vendor:major.minor").

func ParseFlavor

func ParseFlavor(versionString, versionComment string) Flavor

ParseFlavor returns a Flavor value based on inputs obtained from server vars @@global.version and @@global.version_comment. It accounts for how some distributions and/or cloud platforms manipulate those values.

func (Flavor) AllowBlobDefaults added in v0.8.2

func (fl Flavor) AllowBlobDefaults() bool

AllowBlobDefaults returns true if the flavor permits blob and text types to have default values.

func (Flavor) AlwaysShowTableCollation added in v0.8.4

func (fl Flavor) AlwaysShowTableCollation(charSet string) bool

AlwaysShowTableCollation returns true if this flavor always emits a collation clause for the supplied character set, even if the collation is the default for the character set

func (Flavor) DefaultUtf8mb4Collation added in v0.8.4

func (fl Flavor) DefaultUtf8mb4Collation() string

DefaultUtf8mb4Collation returns the name of the default collation of the utf8mb4 character set in this flavor.

func (Flavor) FractionalTimestamps added in v0.8.2

func (fl Flavor) FractionalTimestamps() bool

FractionalTimestamps returns true if the flavor supports fractional seconds in timestamp and datetime values. Note that this returns true for FlavorUnknown as a special-case, since all recent flavors do support this.

func (Flavor) HasDataDictionary added in v0.8.4

func (fl Flavor) HasDataDictionary() bool

HasDataDictionary returns true if the flavor has a global transactional data dictionary instead of using traditional frm files.

func (Flavor) HasInnoFileFormat added in v0.8.10

func (fl Flavor) HasInnoFileFormat() bool

HasInnoFileFormat returns true if the innodb_file_format variable exists in the flavor, false otherwise.

func (Flavor) InnoRowFormatReqs added in v0.8.10

func (fl Flavor) InnoRowFormatReqs(format string) (filePerTable, barracudaFormat bool)

InnoRowFormatReqs returns information on the flavor's requirements for using the supplied row_format in InnoDB. If the first return value is true, the flavor requires innodb_file_per_table=1. If the second return value is true, the flavor requires innodb_file_format=Barracuda. The format arg must be one of "DYNAMIC", "COMPRESSED", "COMPACT", or "REDUNDANT" (case-insensitive), otherwise this method panics...

func (Flavor) Known added in v0.8.15

func (fl Flavor) Known() bool

Known returns true if both the vendor and major version of this flavor were parsed properly

func (Flavor) MySQLishMinVersion added in v0.8.15

func (fl Flavor) MySQLishMinVersion(major, minor int) bool

MySQLishMinVersion returns true if the vendor isn't VendorMariaDB, and this flavor has a version equal to or newer than the specified version. Note that this intentionally DOES consider VendorUnknown to be MySQLish.

func (Flavor) SortedForeignKeys added in v0.8.17

func (fl Flavor) SortedForeignKeys() bool

SortedForeignKeys returns true if the flavor sorts foreign keys lexicographically in SHOW CREATE TABLE.

func (Flavor) String

func (fl Flavor) String() string

func (Flavor) Supported added in v0.8.2

func (fl Flavor) Supported() bool

Supported returns true if package tengo officially supports this flavor

func (Flavor) VendorMinVersion added in v0.8.2

func (fl Flavor) VendorMinVersion(vendor Vendor, major, minor int) bool

VendorMinVersion returns true if this flavor matches the supplied vendor, and has a version equal to or newer than the specified version.

type ForbiddenDiffError

type ForbiddenDiffError struct {
	Reason    string
	Statement string
}

ForbiddenDiffError can be returned by ObjectDiff.Statement when the supplied statement modifiers do not permit the generated ObjectDiff to be used in this situation.

func (*ForbiddenDiffError) Error

func (e *ForbiddenDiffError) Error() string

Error satisfies the builtin error interface.

type ForeignKey

type ForeignKey struct {
	Name                  string
	Columns               []*Column
	ReferencedSchemaName  string // will be empty string if same schema
	ReferencedTableName   string
	ReferencedColumnNames []string // slice length always identical to len(Columns)
	UpdateRule            string
	DeleteRule            string
}

ForeignKey represents a single foreign key constraint in a table. Note that the "referenced" side of the FK is tracked as strings, rather than *Schema, *Table, *[]Column to avoid potentially having to introspect multiple schemas in a particular order. Also, the referenced side is not gauranteed to exist, especially if foreign_key_checks=0 has been used at any point in the past.

func (*ForeignKey) Definition

func (fk *ForeignKey) Definition(flavor Flavor) string

Definition returns this ForeignKey's definition clause, for use as part of a DDL statement.

func (*ForeignKey) Equals

func (fk *ForeignKey) Equals(other *ForeignKey) bool

Equals returns true if two ForeignKeys are identical, false otherwise.

func (*ForeignKey) Equivalent

func (fk *ForeignKey) Equivalent(other *ForeignKey) bool

Equivalent returns true if two ForeignKeys are functionally equivalent, regardless of whether or not they have the same names.

type Index

type Index struct {
	Name       string
	Columns    []*Column
	SubParts   []uint16
	PrimaryKey bool
	Unique     bool
	Comment    string
}

Index represents a single index (primary key, unique secondary index, or non- unique secondard index) in a table.

func (*Index) Definition

func (idx *Index) Definition(_ Flavor) string

Definition returns this index's definition clause, for use as part of a DDL statement.

func (*Index) Equals

func (idx *Index) Equals(other *Index) bool

Equals returns true if two indexes are identical, false otherwise.

func (*Index) Equivalent added in v0.8.16

func (idx *Index) Equivalent(other *Index) bool

Equivalent returns true if two Indexes are functionally equivalent, regardless of whether or not they have the same names or comments.

func (*Index) RedundantTo added in v0.8.16

func (idx *Index) RedundantTo(other *Index) bool

RedundantTo returns true if idx is equivalent to, or a strict subset of, other. Both idx and other should be indexes of the same table. Uniqueness and sub-parts are accounted for in the logic; for example, a unique index is not considered redundant with a non-unique index having the same or more cols. A primary key is never redundant, although another unique index may be redundant to the primary key.

type Instance

type Instance struct {
	BaseDSN    string // DSN ending in trailing slash; i.e. no schema name or params
	Driver     string
	User       string
	Password   string
	Host       string
	Port       int
	SocketPath string

	*sync.RWMutex // protects connectionPool for concurrent operations
	// contains filtered or unexported fields
}

Instance represents a single database server running on a specific host or address.

func NewInstance

func NewInstance(driver, dsn string) (*Instance, error)

NewInstance returns a pointer to a new Instance corresponding to the supplied driver and dsn. Currently only "mysql" driver is supported. dsn should be formatted according to driver specifications. If it contains a schema name, it will be ignored. If it contains any params, they will be applied as default params to all connections (in addition to whatever is supplied in Connect).

func (*Instance) AlterSchema

func (instance *Instance) AlterSchema(schema, newCharSet, newCollation string) error

AlterSchema changes the character set and/or collation of the supplied schema on instance. Supply an empty string for newCharSet to only change the collation, or supply an empty string for newCollation to use the default collation of newCharSet. (Supplying an empty string for both is also allowed, but is a no-op.)

func (*Instance) CanConnect

func (instance *Instance) CanConnect() (bool, error)

CanConnect verifies that the Instance can be connected to

func (*Instance) CloseAll added in v0.8.7

func (instance *Instance) CloseAll()

CloseAll closes all of instance's connection pools. This can be useful for graceful shutdown, to avoid aborted-connection counters/logging in some versions of MySQL.

func (*Instance) Connect

func (instance *Instance) Connect(defaultSchema string, params string) (*sqlx.DB, error)

Connect returns a connection pool (sql.DB) for this instance's host/port/ user/pass with the supplied default schema and params string. If a connection pool already exists for this combination, it will be returned; otherwise, one will be initialized and a connection attempt is made to confirm access. defaultSchema may be "" if it is not relevant. params should be supplied in format "foo=bar&fizz=buzz" with URL escaping already applied. Do not include a prefix of "?". params will be merged with instance.defaultParams, with params supplied here taking precedence. To avoid problems with unexpected disconnection, the connection pool will automatically have a max conn lifetime of at most 30sec, or less if a lower session-level wait_timeout was set in params or instance.defaultParams.

func (*Instance) CreateSchema

func (instance *Instance) CreateSchema(name, charSet, collation string) (*Schema, error)

CreateSchema creates a new database schema with the supplied name, and optionally the supplied default charSet and collation. (Leave charSet and collation blank to use server defaults.)

func (*Instance) DefaultCharSetAndCollation

func (instance *Instance) DefaultCharSetAndCollation() (serverCharSet, serverCollation string, err error)

DefaultCharSetAndCollation returns the instance's default character set and collation

func (*Instance) DropSchema

func (instance *Instance) DropSchema(schema string, onlyIfEmpty bool) error

DropSchema first drops all tables in the schema, and then drops the database schema itself. If onlyIfEmpty==true, returns an error if any of the tables have any rows.

func (*Instance) DropTablesInSchema

func (instance *Instance) DropTablesInSchema(schema string, onlyIfEmpty bool) error

DropTablesInSchema drops all tables in a schema. If onlyIfEmpty==true, returns an error if any of the tables have any rows.

func (*Instance) Flavor

func (instance *Instance) Flavor() Flavor

Flavor returns this instance's flavor value, representing the database distribution/fork/vendor as well as major and minor version. If this is unable to be determined or an error occurs, FlavorUnknown will be returned.

func (*Instance) ForceFlavor added in v0.8.15

func (instance *Instance) ForceFlavor(flavor Flavor)

ForceFlavor overrides this instance's flavor value. Only tests should call this method directly; all other callers should use SetFlavor instead and check the error return value.

func (*Instance) HasSchema

func (instance *Instance) HasSchema(name string) (bool, error)

HasSchema returns true if this instance has a schema with the supplied name visible to the user, or false otherwise. An error result will only be returned if a connection or query failed entirely and we weren't able to determine whether the schema exists.

func (*Instance) HostAndOptionalPort

func (instance *Instance) HostAndOptionalPort() string

HostAndOptionalPort is like String(), but omits the port if default

func (*Instance) Schema

func (instance *Instance) Schema(name string) (*Schema, error)

Schema returns a single schema by name. If the schema does not exist, nil will be returned along with a sql.ErrNoRows error.

func (*Instance) SchemaNames

func (instance *Instance) SchemaNames() ([]string, error)

SchemaNames returns a slice of all schema name strings on the instance visible to the user. System schemas are excluded.

func (*Instance) Schemas

func (instance *Instance) Schemas(onlyNames ...string) ([]*Schema, error)

Schemas returns a slice of schemas on the instance visible to the user. If called with no args, all non-system schemas will be returned. Or pass one or more schema names as args to filter the result to just those schemas. Note that the ordering of the resulting slice is not guaranteed.

func (*Instance) SchemasByName

func (instance *Instance) SchemasByName(onlyNames ...string) (map[string]*Schema, error)

SchemasByName returns a map of schema name string to *Schema. If called with no args, all non-system schemas will be returned. Or pass one or more schema names as args to filter the result to just those schemas.

func (*Instance) SetFlavor added in v0.8.15

func (instance *Instance) SetFlavor(flavor Flavor) error

SetFlavor attempts to set this instance's flavor value. If the instance's flavor has already been hydrated successfully, the value is not changed and an error is returned.

func (*Instance) ShowCreateTable

func (instance *Instance) ShowCreateTable(schema, table string) (string, error)

ShowCreateTable returns a string with a CREATE TABLE statement, representing how the instance views the specified table as having been created.

func (*Instance) StrictModeCompliant added in v0.8.10

func (instance *Instance) StrictModeCompliant(schemas []*Schema) (bool, error)

StrictModeCompliant returns true if all tables in the supplied schemas, if re-created on instance, would comply with innodb_strict_mode and a sql_mode including STRICT_TRANS_TABLES,NO_ZERO_DATE. This method does not currently detect invalid-but-nonzero dates in default values, although it may in the future.

func (*Instance) String

func (instance *Instance) String() string

String for an instance returns a "host:port" string (or "localhost:/path/to/socket" if using UNIX domain socket)

func (*Instance) TableHasRows

func (instance *Instance) TableHasRows(schema, table string) (bool, error)

TableHasRows returns true if the table has at least one row. If an error occurs in querying, also returns true (along with the error) since a false positive is generally less dangerous in this case than a false negative.

func (*Instance) TableSize

func (instance *Instance) TableSize(schema, table string) (int64, error)

TableSize returns an estimate of the table's size on-disk, based on data in information_schema. If the table or schema does not exist on this instance, the error will be sql.ErrNoRows. Please note that use of innodb_stats_persistent may negatively impact the accuracy. For example, see https://bugs.mysql.com/bug.php?id=75428.

func (*Instance) Version

func (instance *Instance) Version() (int, int, int)

Version returns three ints representing the database's major, minor, and patch version, respectively. If this is unable to be determined, all 0's will be returned.

type IntegrationTestSuite

type IntegrationTestSuite interface {
	Setup(backend string) error
	Teardown(backend string) error
	BeforeTest(method string, backend string) error
}

IntegrationTestSuite is the interface for a suite of test methods. In addition to implementing the 3 methods of the interface, an integration test suite struct should have any number of test methods of form TestFoo(t *testing.T), which will be executed automatically by RunSuite.

type ModifyColumn

type ModifyColumn struct {
	Table         *Table
	OldColumn     *Column
	NewColumn     *Column
	PositionFirst bool
	PositionAfter *Column
}

ModifyColumn represents a column that exists in both versions of the table, but with a different definition. It satisfies the TableAlterClause interface.

func (ModifyColumn) Clause

func (mc ModifyColumn) Clause(mods StatementModifiers) string

Clause returns a MODIFY COLUMN clause of an ALTER TABLE statement.

func (ModifyColumn) Unsafe

func (mc ModifyColumn) Unsafe() bool

Unsafe returns true if this clause is potentially destructive of data. ModifyColumn's safety depends on the nature of the column change; for example, increasing the size of a varchar is safe, but changing decreasing the size or changing the column type entirely is considered unsafe.

type NextAutoIncMode

type NextAutoIncMode int

NextAutoIncMode enumerates various ways of handling AUTO_INCREMENT discrepancies between two tables.

const (
	NextAutoIncIgnore      NextAutoIncMode = iota // omit auto-inc value changes in diff
	NextAutoIncIfIncreased                        // only include auto-inc value if the "from" side is less than the "to" side
	NextAutoIncIfAlready                          // only include auto-inc value if the "from" side is already greater than 1
	NextAutoIncAlways                             // always include auto-inc value in diff
)

Constants for how to handle next-auto-inc values in table diffs. Usually these are ignored in diffs entirely, but in some cases they are included.

type ObjectDiff added in v0.8.13

type ObjectDiff interface {
	DiffType() DiffType
	ObjectKey() ObjectKey
	Statement(StatementModifiers) (string, error)
}

ObjectDiff is an interface allowing generic handling of differences between two objects.

type ObjectKey added in v0.8.14

type ObjectKey struct {
	Type ObjectType
	Name string
}

ObjectKey is useful as a map key for indexing database objects within a single schema.

func (ObjectKey) String added in v0.8.14

func (key ObjectKey) String() string

type ObjectType added in v0.8.14

type ObjectType string

ObjectType defines a class of object in a relational database system.

const (
	ObjectTypeDatabase ObjectType = "database"
	ObjectTypeTable    ObjectType = "table"
	ObjectTypeProc     ObjectType = "procedure"
	ObjectTypeFunc     ObjectType = "function"
)

Constants enumerating valid object types. Currently we do not define separate types for sub-types such as columns, indexes, foreign keys, etc as these are handled within the table logic.

func (ObjectType) Caps added in v0.8.14

func (ot ObjectType) Caps() string

Caps returns the object type as an uppercase string.

type RenameColumn

type RenameColumn struct {
	OldColumn *Column
	NewName   string
}

RenameColumn represents a column that exists in both versions of the table, but with a different name. It satisfies the TableAlterClause interface.

func (RenameColumn) Clause

func (rc RenameColumn) Clause(_ StatementModifiers) string

Clause returns a CHANGE COLUMN clause of an ALTER TABLE statement.

func (RenameColumn) Unsafe

func (rc RenameColumn) Unsafe() bool

Unsafe returns true if this clause is potentially destructive of data. RenameColumn is always considered unsafe, despite it not directly destroying data, because it is high-risk for interfering with application logic that may be continuing to use the old column name.

type Routine added in v0.8.14

type Routine struct {
	Name              string
	Type              ObjectType // Will be ObjectTypeProcedure or ObjectTypeFunction
	Body              string     // From information_schema; different char escaping vs CreateStatement
	ParamString       string     // Formatted as per original CREATE
	ReturnDataType    string     // Includes charset/collation when relevant
	Definer           string
	DatabaseCollation string // from creation time
	Comment           string
	Deterministic     bool
	SQLDataAccess     string
	SecurityType      string
	SQLMode           string // sql_mode in effect at creation time
	CreateStatement   string // complete SHOW CREATE obtained from an instance
}

Routine represents a stored procedure or function.

func (*Routine) Definition added in v0.8.14

func (r *Routine) Definition(flavor Flavor) string

Definition generates and returns a canonical CREATE PROCEDURE or CREATE FUNCTION statement based on the Routine's Go field values.

func (*Routine) DropStatement added in v0.8.14

func (r *Routine) DropStatement() string

DropStatement returns a SQL statement that, if run, would drop this routine.

func (*Routine) Equals added in v0.8.14

func (r *Routine) Equals(other *Routine) bool

Equals returns true if two routines are identical, false otherwise.

type RoutineDiff added in v0.8.14

type RoutineDiff struct {
	From        *Routine
	To          *Routine
	ForMetadata bool // if true, routine is being replaced only to update creation-time metadata
}

RoutineDiff represents a difference between two routines.

func (*RoutineDiff) DiffType added in v0.8.14

func (rd *RoutineDiff) DiffType() DiffType

DiffType returns the type of diff operation.

func (*RoutineDiff) ObjectKey added in v0.8.14

func (rd *RoutineDiff) ObjectKey() ObjectKey

ObjectKey returns a value representing the type and name of the routine being diff'ed. The type will be either ObjectTypeFunc or ObjectTypeProc. The name will be the From side routine, unless this is a Create, in which case the To side routine name is used.

func (*RoutineDiff) Statement added in v0.8.14

func (rd *RoutineDiff) Statement(mods StatementModifiers) (string, error)

Statement returns the full DDL statement corresponding to the RoutineDiff. A blank string may be returned if the mods indicate the statement should be skipped. If the mods indicate the statement should be disallowed, it will still be returned as-is, but the error will be non-nil. Be sure not to ignore the error value of this method.

type Schema

type Schema struct {
	Name      string
	CharSet   string
	Collation string
	Tables    []*Table
	Routines  []*Routine
}

Schema represents a database schema.

func (*Schema) AlterStatement

func (s *Schema) AlterStatement(charSet, collation string) string

AlterStatement returns a SQL statement that, if run, would alter this schema's default charset and/or collation to the supplied values. If charSet is "" and collation isn't, only the collation will be changed. If collation is "" and charSet isn't, the default collation for charSet is used automatically. If both params are "", or if values equal to the schema's current charSet and collation are supplied, an empty string is returned.

func (*Schema) CreateStatement

func (s *Schema) CreateStatement() string

CreateStatement returns a SQL statement that, if run, would create this schema.

func (*Schema) Diff

func (s *Schema) Diff(other *Schema) *SchemaDiff

Diff returns the set of differences between this schema and another schema.

func (*Schema) DropStatement

func (s *Schema) DropStatement() string

DropStatement returns a SQL statement that, if run, would drop this schema.

func (*Schema) FunctionsByName added in v0.8.14

func (s *Schema) FunctionsByName() map[string]*Routine

FunctionsByName returns a mapping of function names to Routine struct pointers, for all functions in the schema.

func (*Schema) HasTable

func (s *Schema) HasTable(name string) bool

HasTable returns true if a table with the given name exists in the schema.

func (*Schema) ObjectDefinitions added in v0.8.14

func (s *Schema) ObjectDefinitions() map[ObjectKey]string

ObjectDefinitions returns a mapping of ObjectKey (type+name) to an SQL string containing the corresponding CREATE statement, for all supported object types in the schema. Note that the returned map does NOT include an entry for the schema itself.

func (*Schema) ProceduresByName added in v0.8.14

func (s *Schema) ProceduresByName() map[string]*Routine

ProceduresByName returns a mapping of stored procedure names to Routine struct pointers, for all stored procedures in the schema.

func (*Schema) Table

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

Table returns a table by name.

func (*Schema) TablesByName

func (s *Schema) TablesByName() map[string]*Table

TablesByName returns a mapping of table names to Table struct pointers, for all tables in the schema.

type SchemaDiff

type SchemaDiff struct {
	FromSchema   *Schema
	ToSchema     *Schema
	TableDiffs   []*TableDiff   // a set of statements that, if run, would turn tables in FromSchema into ToSchema
	RoutineDiffs []*RoutineDiff // " but for funcs and procs
}

SchemaDiff represents a set of differences between two database schemas, encapsulating diffs of various different object types.

func NewSchemaDiff

func NewSchemaDiff(from, to *Schema) *SchemaDiff

NewSchemaDiff computes the set of differences between two database schemas.

func (*SchemaDiff) DatabaseDiff added in v0.8.13

func (sd *SchemaDiff) DatabaseDiff() *DatabaseDiff

DatabaseDiff returns an object representing database-level DDL (CREATE DATABASE, ALTER DATABASE, DROP DATABASE), or nil if no database-level DDL is necessary.

func (*SchemaDiff) FilteredTableDiffs

func (sd *SchemaDiff) FilteredTableDiffs(onlyTypes ...DiffType) []*TableDiff

FilteredTableDiffs returns any TableDiffs of the specified type(s).

func (*SchemaDiff) ObjectDiffs added in v0.8.13

func (sd *SchemaDiff) ObjectDiffs() []ObjectDiff

ObjectDiffs returns a slice of all ObjectDiffs in the SchemaDiff. The results are returned in a sorted order, such that the diffs' Statements are legal. For example, if a CREATE DATABASE is present, it will occur in the slice prior to any table-level DDL in that schema.

func (*SchemaDiff) String

func (sd *SchemaDiff) String() string

String returns the set of differences between two schemas as a single string. In building this string representation, note that no statement modifiers are applied, and any errors from Statement() are ignored. This means the returned string may contain destructive statements, and should only be used for display purposes, not for DDL execution.

type StatementModifiers

type StatementModifiers struct {
	NextAutoInc            NextAutoIncMode // How to handle differences in next-auto-inc values
	AllowUnsafe            bool            // Whether to allow potentially-destructive DDL (drop table, drop column, modify col type, etc)
	LockClause             string          // Include a LOCK=[value] clause in generated ALTER TABLE
	AlgorithmClause        string          // Include an ALGORITHM=[value] clause in generated ALTER TABLE
	IgnoreTable            *regexp.Regexp  // Generate blank DDL if table name matches this regexp
	StrictIndexOrder       bool            // If true, maintain index order even in cases where there is no functional difference
	StrictForeignKeyNaming bool            // If true, maintain foreign key names even if no functional difference in definition
	CompareMetadata        bool            // If true, compare creation-time sql_mode and db collation for funcs, procs (and eventually events, triggers)
	Flavor                 Flavor          // Adjust generated DDL to match vendor/version. Zero value is FlavorUnknown which makes no adjustments.
}

StatementModifiers are options that may be applied to adjust the DDL emitted for a particular table, and/or generate errors if certain clauses are present.

type Table

type Table struct {
	Name               string
	Engine             string
	CharSet            string
	Collation          string
	CollationIsDefault bool   // true if Collation is default for CharSet
	CreateOptions      string // row_format, stats_persistent, stats_auto_recalc, etc
	Columns            []*Column
	PrimaryKey         *Index
	SecondaryIndexes   []*Index
	ForeignKeys        []*ForeignKey
	Comment            string
	NextAutoIncrement  uint64
	UnsupportedDDL     bool   // If true, tengo cannot diff this table or auto-generate its CREATE TABLE
	CreateStatement    string // complete SHOW CREATE TABLE obtained from an instance
}

Table represents a single database table.

func (*Table) AlterStatement

func (t *Table) AlterStatement() string

AlterStatement returns the prefix to a SQL "ALTER TABLE" statement.

func (*Table) ClusteredIndexKey

func (t *Table) ClusteredIndexKey() *Index

ClusteredIndexKey returns which index is used for an InnoDB table's clustered index. This will be the primary key if one exists; otherwise, it will be the first unique key with non-nullable columns. If there is no such key, or if the table's engine isn't InnoDB, this method returns nil.

func (*Table) ColumnsByName

func (t *Table) ColumnsByName() map[string]*Column

ColumnsByName returns a mapping of column names to Column value pointers, for all columns in the table.

func (*Table) Diff

func (t *Table) Diff(to *Table) (clauses []TableAlterClause, supported bool)

Diff returns a set of differences between this table and another table.

func (*Table) DropStatement

func (t *Table) DropStatement() string

DropStatement returns a SQL statement that, if run, would drop this table.

func (*Table) GeneratedCreateStatement

func (t *Table) GeneratedCreateStatement(flavor Flavor) string

GeneratedCreateStatement generates a CREATE TABLE statement based on the Table's Go field values. If t.UnsupportedDDL is false, this will match the output of MySQL's SHOW CREATE TABLE statement. But if t.UnsupportedDDL is true, this means the table uses MySQL features that Tengo does not yet support, and so the output of this method will differ from MySQL.

func (*Table) HasAutoIncrement

func (t *Table) HasAutoIncrement() bool

HasAutoIncrement returns true if the table contains an auto-increment column, or false otherwise.

func (*Table) RowFormatClause added in v0.8.10

func (t *Table) RowFormatClause() string

RowFormatClause returns the table's ROW_FORMAT clause, if one was explicitly specified in the table's creation options. If no ROW_FORMAT clause was specified, but a KEY_BLOCK_SIZE is, "COMPRESSED" will be returned since MySQL applies this automatically. If no ROW_FORMAT or KEY_BLOCK_SIZE was specified, a blank string is returned. This method does not query an instance to determine if the table's actual ROW_FORMAT differs from what was requested in creation options; nor does it query the default row format if none was specified.

func (*Table) SecondaryIndexesByName

func (t *Table) SecondaryIndexesByName() map[string]*Index

SecondaryIndexesByName returns a mapping of index names to Index value pointers, for all secondary indexes in the table.

type TableAlterClause

type TableAlterClause interface {
	Clause(StatementModifiers) string
}

TableAlterClause interface represents a specific single-element difference between two tables. Structs satisfying this interface can generate an ALTER TABLE clause, such as ADD COLUMN, MODIFY COLUMN, ADD KEY, etc.

type TableDiff

type TableDiff struct {
	Type DiffType
	From *Table
	To   *Table
	// contains filtered or unexported fields
}

TableDiff represents a difference between two tables.

func NewAlterTable

func NewAlterTable(from, to *Table) *TableDiff

NewAlterTable returns a *TableDiff representing an ALTER TABLE statement, i.e. a table that exists in the "from" and "to" side schemas but with one or more differences. If the supplied tables are identical, nil will be returned instead of a TableDiff.

func NewCreateTable

func NewCreateTable(table *Table) *TableDiff

NewCreateTable returns a *TableDiff representing a CREATE TABLE statement, i.e. a table that only exists in the "to" side schema in a diff.

func NewDropTable

func NewDropTable(table *Table) *TableDiff

NewDropTable returns a *TableDiff representing a DROP TABLE statement, i.e. a table that only exists in the "from" side schema in a diff.

func (*TableDiff) Clauses

func (td *TableDiff) Clauses(mods StatementModifiers) (string, error)

Clauses returns the body of the statement represented by the table diff. For DROP statements, this will be an empty string. For CREATE statements, it will be everything after "CREATE TABLE [name] ". For ALTER statements, it will be everything after "ALTER TABLE [name] ".

func (*TableDiff) DiffType added in v0.8.13

func (td *TableDiff) DiffType() DiffType

DiffType returns the type of diff operation.

func (*TableDiff) ObjectKey added in v0.8.14

func (td *TableDiff) ObjectKey() ObjectKey

ObjectKey returns a value representing the type and name of the table being diff'ed. The type is always ObjectTypeTable. The name will be the From side table, unless the diffType is DiffTypeCreate, in which case the To side table name is used.

func (*TableDiff) SplitAddForeignKeys

func (td *TableDiff) SplitAddForeignKeys() (*TableDiff, *TableDiff)

SplitAddForeignKeys looks through a TableDiff's alterClauses and pulls out any AddForeignKey clauses into a separate TableDiff. The first returned TableDiff is guaranteed to contain no AddForeignKey clauses, and the second returned value is guaranteed to only consist of AddForeignKey clauses. If the receiver contained no AddForeignKey clauses, the first return value will be the receiver, and the second will be nil. If the receiver contained only AddForeignKey clauses, the first return value will be nil, and the second will be the receiver. This method is useful for several reasons: it is desirable to only add FKs after other alters have been made (since FKs rely on indexes on both sides); it is illegal to drop and re-add an FK with the same name in the same ALTER; some versions of MySQL recommend against dropping and adding FKs in the same ALTER even if they have different names.

func (*TableDiff) Statement

func (td *TableDiff) Statement(mods StatementModifiers) (string, error)

Statement returns the full DDL statement corresponding to the TableDiff. A blank string may be returned if the mods indicate the statement should be skipped. If the mods indicate the statement should be disallowed, it will still be returned as-is, but the error will be non-nil. Be sure not to ignore the error value of this method.

type Unsafer

type Unsafer interface {
	Unsafe() bool
}

Unsafer interface represents a type of clause that may have the ability to destroy data. Structs satisfying this interface can indicate whether or not this particular clause destroys data.

type UnsupportedDiffError

type UnsupportedDiffError struct {
	ObjectKey      ObjectKey
	ExpectedCreate string
	ActualCreate   string
}

UnsupportedDiffError can be returned by ObjectDiff.Statement if Tengo is unable to transform the object due to use of unsupported features.

func (*UnsupportedDiffError) Error

func (e *UnsupportedDiffError) Error() string

Error satisfies the builtin error interface.

func (*UnsupportedDiffError) ExtendedError

func (e *UnsupportedDiffError) ExtendedError() string

ExtendedError returns a string with more information about why the diff is not supported.

type Vendor added in v0.8.2

type Vendor int

Vendor distinguishes between different database distributions/forks

const (
	VendorUnknown Vendor = iota
	VendorMySQL
	VendorPercona
	VendorMariaDB
)

Constants representing different supported vendors

func ParseVendor added in v0.8.2

func ParseVendor(versionComment string) Vendor

ParseVendor takes a version comment string (e.g. @@version_comment MySQL variable) and returns the corresponding Vendor constant, defaulting to VendorUnknown if the string is not recognized.

func (Vendor) String added in v0.8.2

func (v Vendor) String() string

Jump to

Keyboard shortcuts

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