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 ¶
- Variables
- func EscapeIdentifier(input string) string
- func EscapeValueForCreateTable(input string) string
- func IsAccessError(err error) bool
- func IsDatabaseError(err error, specificErrors ...uint16) bool
- func IsForbiddenDiff(err error) bool
- func IsSyntaxError(err error) bool
- func IsUnsupportedDiff(err error) bool
- func NormalizeCreateOptions(createStmt string) string
- func ParseCreateAutoInc(createStmt string) (string, uint64)
- func ParseVersion(version string) (result [3]int)
- func RunSuite(suite IntegrationTestSuite, t *testing.T, backends []string)
- func SplitEnv(key string) []string
- func SplitHostOptionalPort(hostaddr string) (string, int, error)
- func UseFilteredDriverLogger()
- type AddColumn
- type AddForeignKey
- type AddIndex
- type ChangeAutoIncrement
- type ChangeCharSet
- type ChangeComment
- type ChangeCreateOptions
- type ChangeStorageEngine
- type Column
- type ColumnDefault
- type DatabaseDiff
- type DiffType
- type DockerClient
- func (dc *DockerClient) CreateInstance(opts DockerizedInstanceOptions) (*DockerizedInstance, error)
- func (dc *DockerClient) GetInstance(opts DockerizedInstanceOptions) (*DockerizedInstance, error)
- func (dc *DockerClient) GetOrCreateInstance(opts DockerizedInstanceOptions) (*DockerizedInstance, error)
- type DockerClientOptions
- type DockerizedInstance
- func (di *DockerizedInstance) DSN() string
- func (di *DockerizedInstance) Destroy() error
- func (di *DockerizedInstance) NukeData() error
- func (di *DockerizedInstance) Port() int
- func (di *DockerizedInstance) SourceSQL(filePath string) (string, error)
- func (di *DockerizedInstance) Start() error
- func (di *DockerizedInstance) Stop() error
- func (di *DockerizedInstance) String() string
- func (di *DockerizedInstance) TryConnect() (err error)
- type DockerizedInstanceOptions
- type DropColumn
- type DropForeignKey
- type DropIndex
- type Flavor
- func (fl Flavor) AllowBlobDefaults() bool
- func (fl Flavor) AlwaysShowTableCollation(charSet string) bool
- func (fl Flavor) DefaultUtf8mb4Collation() string
- func (fl Flavor) FractionalTimestamps() bool
- func (fl Flavor) HasDataDictionary() bool
- func (fl Flavor) HasInnoFileFormat() bool
- func (fl Flavor) InnoRowFormatReqs(format string) (filePerTable, barracudaFormat bool)
- func (fl Flavor) Known() bool
- func (fl Flavor) MySQLishMinVersion(major, minor int) bool
- func (fl Flavor) SortedForeignKeys() bool
- func (fl Flavor) String() string
- func (fl Flavor) Supported() bool
- func (fl Flavor) VendorMinVersion(vendor Vendor, major, minor int) bool
- type ForbiddenDiffError
- type ForeignKey
- type Index
- type Instance
- func (instance *Instance) AlterSchema(schema, newCharSet, newCollation string) error
- func (instance *Instance) CanConnect() (bool, error)
- func (instance *Instance) CloseAll()
- func (instance *Instance) Connect(defaultSchema string, params string) (*sqlx.DB, error)
- func (instance *Instance) CreateSchema(name, charSet, collation string) (*Schema, error)
- func (instance *Instance) DefaultCharSetAndCollation() (serverCharSet, serverCollation string, err error)
- func (instance *Instance) DropSchema(schema string, onlyIfEmpty bool) error
- func (instance *Instance) DropTablesInSchema(schema string, onlyIfEmpty bool) error
- func (instance *Instance) Flavor() Flavor
- func (instance *Instance) ForceFlavor(flavor Flavor)
- func (instance *Instance) HasSchema(name string) (bool, error)
- func (instance *Instance) HostAndOptionalPort() string
- func (instance *Instance) Schema(name string) (*Schema, error)
- func (instance *Instance) SchemaNames() ([]string, error)
- func (instance *Instance) Schemas(onlyNames ...string) ([]*Schema, error)
- func (instance *Instance) SchemasByName(onlyNames ...string) (map[string]*Schema, error)
- func (instance *Instance) SetFlavor(flavor Flavor) error
- func (instance *Instance) ShowCreateTable(schema, table string) (string, error)
- func (instance *Instance) StrictModeCompliant(schemas []*Schema) (bool, error)
- func (instance *Instance) String() string
- func (instance *Instance) TableHasRows(schema, table string) (bool, error)
- func (instance *Instance) TableSize(schema, table string) (int64, error)
- func (instance *Instance) Version() (int, int, int)
- type IntegrationTestSuite
- type ModifyColumn
- type NextAutoIncMode
- type ObjectDiff
- type ObjectKey
- type ObjectType
- type RenameColumn
- type Routine
- type RoutineDiff
- type Schema
- func (s *Schema) AlterStatement(charSet, collation string) string
- func (s *Schema) CreateStatement() string
- func (s *Schema) Diff(other *Schema) *SchemaDiff
- func (s *Schema) DropStatement() string
- func (s *Schema) FunctionsByName() map[string]*Routine
- func (s *Schema) HasTable(name string) bool
- func (s *Schema) ObjectDefinitions() map[ObjectKey]string
- func (s *Schema) ProceduresByName() map[string]*Routine
- func (s *Schema) Table(name string) *Table
- func (s *Schema) TablesByName() map[string]*Table
- type SchemaDiff
- type StatementModifiers
- type Table
- func (t *Table) AlterStatement() string
- func (t *Table) ClusteredIndexKey() *Index
- func (t *Table) ColumnsByName() map[string]*Column
- func (t *Table) Diff(to *Table) (clauses []TableAlterClause, supported bool)
- func (t *Table) DropStatement() string
- func (t *Table) GeneratedCreateStatement(flavor Flavor) string
- func (t *Table) HasAutoIncrement() bool
- func (t *Table) RowFormatClause() string
- func (t *Table) SecondaryIndexesByName() map[string]*Index
- type TableAlterClause
- type TableDiff
- type Unsafer
- type UnsupportedDiffError
- type Vendor
Constants ¶
This section is empty.
Variables ¶
var ColumnDefaultNull = ColumnDefault{Null: true}
ColumnDefaultNull indicates a column has a default value of NULL.
var FlavorMariaDB101 = Flavor{VendorMariaDB, 10, 1}
FlavorMariaDB101 represents MariaDB 10.1.x
var FlavorMariaDB102 = Flavor{VendorMariaDB, 10, 2}
FlavorMariaDB102 represents MariaDB 10.2.x
var FlavorMariaDB103 = Flavor{VendorMariaDB, 10, 3}
FlavorMariaDB103 represents MariaDB 10.3.x
var FlavorMySQL55 = Flavor{VendorMySQL, 5, 5}
FlavorMySQL55 represents MySQL 5.5.x
var FlavorMySQL56 = Flavor{VendorMySQL, 5, 6}
FlavorMySQL56 represents MySQL 5.6.x
var FlavorMySQL57 = Flavor{VendorMySQL, 5, 7}
FlavorMySQL57 represents MySQL 5.7.x
var FlavorMySQL80 = Flavor{VendorMySQL, 8, 0}
FlavorMySQL80 represents MySQL 8.0.x
var FlavorPercona55 = Flavor{VendorPercona, 5, 5}
FlavorPercona55 represents Percona Server 5.5.x
var FlavorPercona56 = Flavor{VendorPercona, 5, 6}
FlavorPercona56 represents Percona Server 5.6.x
var FlavorPercona57 = Flavor{VendorPercona, 5, 7}
FlavorPercona57 represents Percona Server 5.7.x
var FlavorPercona80 = Flavor{VendorPercona, 8, 0}
FlavorPercona80 represents Percona Server 8.0.x
var FlavorUnknown = Flavor{VendorUnknown, 0, 0}
FlavorUnknown represents a flavor that cannot be parsed. This is the zero value for Flavor.
Functions ¶
func EscapeIdentifier ¶
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 ¶
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 ¶
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 ¶
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 ¶
IsForbiddenDiff returns true if err represents an "unsafe" alteration that has not explicitly been permitted by the supplied StatementModifiers.
func IsSyntaxError ¶
IsSyntaxError returns true if err is a SQL syntax error, or false otherwise.
func IsUnsupportedDiff ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
ChangeAutoIncrement represents a difference in next-auto-increment value between two versions of a table. It satisfies the TableAlterClause interface.
func (ChangeAutoIncrement) Clause ¶
func (cai ChangeAutoIncrement) Clause(mods StatementModifiers) string
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 ¶
func (cc ChangeComment) Clause(_ StatementModifiers) string
Clause returns a clause of an ALTER TABLE statement that changes a table's comment.
type ChangeCreateOptions ¶
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 ¶
func (cco ChangeCreateOptions) Clause(_ StatementModifiers) string
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 ¶
func (cse ChangeStorageEngine) Clause(_ StatementModifiers) string
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 ¶
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)
type ColumnDefault ¶
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.
type DatabaseDiff ¶ added in v0.8.13
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
Constants representing the types of diff operations.
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
func (dc *DockerClient) GetInstance(opts DockerizedInstanceOptions) (*DockerizedInstance, error)
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 ¶
Flavor represents a database server release, including vendor along with major and minor version number.
func NewFlavor ¶ added in v0.8.2
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 ¶
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
AllowBlobDefaults returns true if the flavor permits blob and text types to have default values.
func (Flavor) AlwaysShowTableCollation ¶ added in v0.8.4
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
DefaultUtf8mb4Collation returns the name of the default collation of the utf8mb4 character set in this flavor.
func (Flavor) FractionalTimestamps ¶ added in v0.8.2
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
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
HasInnoFileFormat returns true if the innodb_file_format variable exists in the flavor, false otherwise.
func (Flavor) InnoRowFormatReqs ¶ added in v0.8.10
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
Known returns true if both the vendor and major version of this flavor were parsed properly
func (Flavor) MySQLishMinVersion ¶ added in v0.8.15
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
SortedForeignKeys returns true if the flavor sorts foreign keys lexicographically in SHOW CREATE TABLE.
type ForbiddenDiffError ¶
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 ¶
Definition returns this index's definition clause, for use as part of a DDL statement.
func (*Index) Equivalent ¶ added in v0.8.16
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
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
DropTablesInSchema drops all tables in a schema. If onlyIfEmpty==true, returns an error if any of the tables have any rows.
func (*Instance) 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
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 ¶
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 ¶
HostAndOptionalPort is like String(), but omits the port if default
func (*Instance) Schema ¶
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 ¶
SchemaNames returns a slice of all schema name strings on the instance visible to the user. System schemas are excluded.
func (*Instance) Schemas ¶
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 ¶
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
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 ¶
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
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 ¶
String for an instance returns a "host:port" string (or "localhost:/path/to/socket" if using UNIX domain socket)
func (*Instance) TableHasRows ¶
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 ¶
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.
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.
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 ¶
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
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
DropStatement returns a SQL statement that, if run, would drop this routine.
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 ¶
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 ¶
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 ¶
DropStatement returns a SQL statement that, if run, would drop this schema.
func (*Schema) FunctionsByName ¶ added in v0.8.14
FunctionsByName returns a mapping of function names to Routine struct pointers, for all functions in the schema.
func (*Schema) HasTable ¶
HasTable returns true if a table with the given name exists in the schema.
func (*Schema) ObjectDefinitions ¶ added in v0.8.14
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
ProceduresByName returns a mapping of stored procedure names to Routine struct pointers, for all stored procedures in the schema.
func (*Schema) TablesByName ¶
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 ¶
AlterStatement returns the prefix to a SQL "ALTER TABLE" statement.
func (*Table) ClusteredIndexKey ¶
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 ¶
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 ¶
DropStatement returns a SQL statement that, if run, would drop this table.
func (*Table) GeneratedCreateStatement ¶
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 ¶
HasAutoIncrement returns true if the table contains an auto-increment column, or false otherwise.
func (*Table) RowFormatClause ¶ added in v0.8.10
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 ¶
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 ¶
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 ¶
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 ¶
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) ObjectKey ¶ added in v0.8.14
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 ¶
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 ¶
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
Constants representing different supported vendors
func ParseVendor ¶ added in v0.8.2
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.