atlasariga.io/atlas/sql/mysql Index | Files | Directories

package mysql

import "ariga.io/atlas/sql/mysql"

Index

Constants

const (
	DriverName  = "mysql"
	DriverMaria = "mariadb"
)

DriverName and DriverMaria holds the names used for registration.

const (
	TypeBool    = "bool"
	TypeBoolean = "boolean"

	TypeBit       = "bit"       // MYSQL_TYPE_BIT
	TypeInt       = "int"       // MYSQL_TYPE_LONG
	TypeTinyInt   = "tinyint"   // MYSQL_TYPE_TINY
	TypeSmallInt  = "smallint"  // MYSQL_TYPE_SHORT
	TypeMediumInt = "mediumint" // MYSQL_TYPE_INT24
	TypeBigInt    = "bigint"    // MYSQL_TYPE_LONGLONG

	TypeDecimal = "decimal" // MYSQL_TYPE_DECIMAL
	TypeNumeric = "numeric" // MYSQL_TYPE_DECIMAL (numeric_type rule in sql_yacc.yy)
	TypeFloat   = "float"   // MYSQL_TYPE_FLOAT
	TypeDouble  = "double"  // MYSQL_TYPE_DOUBLE
	TypeReal    = "real"    // MYSQL_TYPE_FLOAT or MYSQL_TYPE_DOUBLE (real_type in sql_yacc.yy)

	TypeTimestamp = "timestamp" // MYSQL_TYPE_TIMESTAMP
	TypeDate      = "date"      // MYSQL_TYPE_DATE
	TypeTime      = "time"      // MYSQL_TYPE_TIME
	TypeDateTime  = "datetime"  // MYSQL_TYPE_DATETIME
	TypeYear      = "year"      // MYSQL_TYPE_YEAR

	TypeVarchar    = "varchar"    // MYSQL_TYPE_VAR_STRING, MYSQL_TYPE_VARCHAR
	TypeChar       = "char"       // MYSQL_TYPE_STRING
	TypeVarBinary  = "varbinary"  // MYSQL_TYPE_VAR_STRING + NULL CHARACTER_SET.
	TypeBinary     = "binary"     // MYSQL_TYPE_STRING + NULL CHARACTER_SET.
	TypeBlob       = "blob"       // MYSQL_TYPE_BLOB
	TypeTinyBlob   = "tinyblob"   // MYSQL_TYPE_TINYBLOB
	TypeMediumBlob = "mediumblob" // MYSQL_TYPE_MEDIUM_BLOB
	TypeLongBlob   = "longblob"   // MYSQL_TYPE_LONG_BLOB
	TypeText       = "text"       // MYSQL_TYPE_BLOB + CHARACTER_SET utf8mb4
	TypeTinyText   = "tinytext"   // MYSQL_TYPE_TINYBLOB + CHARACTER_SET utf8mb4
	TypeMediumText = "mediumtext" // MYSQL_TYPE_MEDIUM_BLOB + CHARACTER_SET utf8mb4
	TypeLongText   = "longtext"   // MYSQL_TYPE_LONG_BLOB with + CHARACTER_SET utf8mb4

	TypeEnum = "enum" // MYSQL_TYPE_ENUM
	TypeSet  = "set"  // MYSQL_TYPE_SET
	TypeJSON = "json" // MYSQL_TYPE_JSON

	TypeGeometry           = "geometry"           // MYSQL_TYPE_GEOMETRY
	TypePoint              = "point"              // Geometry_type::kPoint
	TypeMultiPoint         = "multipoint"         // Geometry_type::kMultipoint
	TypeLineString         = "linestring"         // Geometry_type::kLinestring
	TypeMultiLineString    = "multilinestring"    // Geometry_type::kMultilinestring
	TypePolygon            = "polygon"            // Geometry_type::kPolygon
	TypeMultiPolygon       = "multipolygon"       // Geometry_type::kMultipolygon
	TypeGeoCollection      = "geomcollection"     // Geometry_type::kGeometrycollection
	TypeGeometryCollection = "geometrycollection" // Geometry_type::kGeometrycollection

	TypeUUID = "uuid" // MariaDB supported uuid type from 10.7.0+

	TypeInet4 = "inet4" // MariaDB type for storage of IPv4 addresses, from 10.10.0+.
	TypeInet6 = "inet6" // MariaDB type for storage of IPv6 addresses, from 10.10.0+.
)

MySQL standard column types as defined in its codebase. Name and order is organized differently than MySQL.

https://github.com/mysql/mysql-server/blob/8.0/include/field_types.h https://github.com/mysql/mysql-server/blob/8.0/sql/dd/types/column.h https://github.com/mysql/mysql-server/blob/8.0/sql/sql_show.cc https://github.com/mysql/mysql-server/blob/8.0/sql/gis/geometries.cc https://dev.mysql.com/doc/refman/8.0/en/other-vendor-data-types.html

const (
	IndexTypeBTree    = "BTREE"
	IndexTypeHash     = "HASH"
	IndexTypeFullText = "FULLTEXT"
	IndexTypeSpatial  = "SPATIAL"

	IndexParserNGram = "ngram"
	IndexParserMeCab = "mecab"

	EngineInnoDB = "InnoDB"
	EngineMyISAM = "MyISAM"
	EngineMemory = "Memory"
	EngineCSV    = "CSV"
	EngineNDB    = "NDB" // NDBCLUSTER

)

Additional common constants in MySQL.

Variables

var (

	// MarshalHCL marshals v into an Atlas HCL DDL document.
	MarshalHCL = schemahcl.MarshalerFunc(codec.MarshalSpec)
	// EvalHCL implements the schemahcl.Evaluator interface.
	EvalHCL = schemahcl.EvalFunc(codec.Eval)
	// EvalHCLBytes is a helper that evaluates an HCL document from a byte slice.
	EvalHCLBytes = specutil.HCLBytesFunc(EvalHCL)
	// EvalMariaHCL implements the schemahcl.Evaluator interface for MariaDB flavor.
	EvalMariaHCL = schemahcl.EvalFunc(mariaCodec.Eval)
	// EvalMariaHCLBytes is a helper that evaluates a MariaDB HCL document from a byte slice.
	EvalMariaHCLBytes = specutil.HCLBytesFunc(EvalMariaHCL)
)
var DefaultDiff schema.Differ = &sqlx.Diff{DiffDriver: &diff{conn: noConn}}

DefaultDiff provides basic diffing capabilities for MySQL dialects. Note, it is recommended to call Open, create a new Driver and use its Differ when a database connection is available.

var (

	// DefaultPlan provides basic planning capabilities for MySQL dialects.
	// Note, it is recommended to call Open, create a new Driver and use its
	// migrate.PlanApplier when a database connection is available.
	DefaultPlan migrate.PlanApplier = &planApply{conn: noConn}
)
var TypeRegistry = schemahcl.NewRegistry(
	schemahcl.WithFormatter(FormatType),
	schemahcl.WithParser(ParseType),
	schemahcl.WithSpecs(
		&schemahcl.TypeSpec{
			Name: TypeEnum,
			T:    TypeEnum,
			Attributes: []*schemahcl.TypeAttr{
				{Name: "values", Kind: reflect.Slice, Required: true},
			},
			RType: reflect.TypeOf(schema.EnumType{}),
			FromSpec: func(t *schemahcl.Type) (schema.Type, error) {
				if len(t.Attrs) != 1 || t.Attrs[0].K != "values" {
					return nil, fmt.Errorf("invalid enum type spec: %v", t)
				}
				v, err := t.Attrs[0].Strings()
				if err != nil {
					return nil, err
				}
				return &schema.EnumType{T: "enum", Values: v}, nil
			},
		},
		&schemahcl.TypeSpec{
			Name: TypeSet,
			T:    TypeSet,
			Attributes: []*schemahcl.TypeAttr{
				{Name: "values", Kind: reflect.Slice, Required: true},
			},
			RType: reflect.TypeOf(SetType{}),
			FromSpec: func(t *schemahcl.Type) (schema.Type, error) {
				if len(t.Attrs) != 1 || t.Attrs[0].K != "values" {
					return nil, fmt.Errorf("invalid set type spec: %v", t)
				}
				v, err := t.Attrs[0].Strings()
				if err != nil {
					return nil, err
				}
				return &SetType{Values: v}, nil
			},
		},
		schemahcl.NewTypeSpec(TypeBool),
		schemahcl.NewTypeSpec(TypeBoolean),
		schemahcl.NewTypeSpec(TypeBit, schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
		schemahcl.NewTypeSpec(TypeInt, schemahcl.WithAttributes(unsignedTypeAttr(), schemahcl.SizeTypeAttr(false))),
		schemahcl.NewTypeSpec(TypeTinyInt, schemahcl.WithAttributes(unsignedTypeAttr(), schemahcl.SizeTypeAttr(false))),
		schemahcl.NewTypeSpec(TypeSmallInt, schemahcl.WithAttributes(unsignedTypeAttr(), schemahcl.SizeTypeAttr(false))),
		schemahcl.NewTypeSpec(TypeMediumInt, schemahcl.WithAttributes(unsignedTypeAttr(), schemahcl.SizeTypeAttr(false))),
		schemahcl.NewTypeSpec(TypeBigInt, schemahcl.WithAttributes(unsignedTypeAttr(), schemahcl.SizeTypeAttr(false))),
		schemahcl.NewTypeSpec(TypeDecimal, schemahcl.WithAttributes(unsignedTypeAttr(), schemahcl.PrecisionTypeAttr(), schemahcl.ScaleTypeAttr())),
		schemahcl.NewTypeSpec(TypeNumeric, schemahcl.WithAttributes(unsignedTypeAttr(), schemahcl.PrecisionTypeAttr(), schemahcl.ScaleTypeAttr())),
		schemahcl.NewTypeSpec(TypeFloat, schemahcl.WithAttributes(unsignedTypeAttr(), schemahcl.PrecisionTypeAttr(), schemahcl.ScaleTypeAttr())),
		schemahcl.NewTypeSpec(TypeDouble, schemahcl.WithAttributes(unsignedTypeAttr(), schemahcl.PrecisionTypeAttr(), schemahcl.ScaleTypeAttr())),
		schemahcl.NewTypeSpec(TypeReal, schemahcl.WithAttributes(unsignedTypeAttr(), schemahcl.PrecisionTypeAttr(), schemahcl.ScaleTypeAttr())),
		schemahcl.NewTypeSpec(TypeTimestamp, schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr())),
		schemahcl.NewTypeSpec(TypeDate),
		schemahcl.NewTypeSpec(TypeTime, schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr())),
		schemahcl.NewTypeSpec(TypeDateTime, schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr())),
		schemahcl.NewTypeSpec(TypeYear, schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr())),
		schemahcl.NewTypeSpec(TypeVarchar, schemahcl.WithAttributes(schemahcl.SizeTypeAttr(true))),
		schemahcl.NewTypeSpec(TypeChar, schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
		schemahcl.NewTypeSpec(TypeVarBinary, schemahcl.WithAttributes(schemahcl.SizeTypeAttr(true))),
		schemahcl.NewTypeSpec(TypeBinary, schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
		schemahcl.NewTypeSpec(TypeBlob, schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
		schemahcl.NewTypeSpec(TypeTinyBlob),
		schemahcl.NewTypeSpec(TypeMediumBlob),
		schemahcl.NewTypeSpec(TypeLongBlob),
		schemahcl.NewTypeSpec(TypeJSON),
		schemahcl.NewTypeSpec(TypeText, schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
		schemahcl.NewTypeSpec(TypeTinyText),
		schemahcl.NewTypeSpec(TypeMediumText),
		schemahcl.NewTypeSpec(TypeLongText),
		schemahcl.NewTypeSpec(TypeGeometry),
		schemahcl.NewTypeSpec(TypePoint),
		schemahcl.NewTypeSpec(TypeMultiPoint),
		schemahcl.NewTypeSpec(TypeLineString),
		schemahcl.NewTypeSpec(TypeMultiLineString),
		schemahcl.NewTypeSpec(TypePolygon),
		schemahcl.NewTypeSpec(TypeMultiPolygon),
		schemahcl.NewTypeSpec(TypeGeometryCollection),
		schemahcl.NewTypeSpec(TypeInet4),
		schemahcl.NewTypeSpec(TypeInet6),
	),
)

TypeRegistry contains the supported TypeSpecs for the mysql driver.

Functions

func FormatType

func FormatType(t schema.Type) (string, error)

FormatType converts schema type to its column form in the database. An error is returned if the type cannot be recognized.

func Open

Open opens a new MySQL driver.

func ParseType

func ParseType(raw string) (schema.Type, error)

ParseType returns the schema.Type value represented by the given raw type. The raw value is expected to follow the format in MySQL information schema.

Types

type AutoIncrement

type AutoIncrement struct {
	schema.Attr
	V int64
}

AutoIncrement attribute for columns with "AUTO_INCREMENT" as a default. V represent an optional start value for the counter.

type BitType

type BitType struct {
	schema.Type
	T    string
	Size int
}

BitType represents the type bit.

type Codec

type Codec struct {
	State *schemahcl.State
}

Codec for schemahcl.

func (*Codec) Eval

func (c *Codec) Eval(p *hclparse.Parser, v any, input map[string]cty.Value) error

Eval evaluates an Atlas DDL document into v using the input.

func (*Codec) EvalOptions

func (c *Codec) EvalOptions(p *hclparse.Parser, v any, opts *schemahcl.EvalOptions) error

EvalOptions decodes the HCL with the given options.

func (*Codec) MarshalSpec

func (c *Codec) MarshalSpec(v any) ([]byte, error)

MarshalSpec marshals v into an Atlas DDL document using a schemahcl.Marshaler.

type CreateOptions

type CreateOptions struct {
	schema.Attr
	V string
}

CreateOptions attribute for describing extra options used with CREATE TABLE.

type CreateStmt

type CreateStmt struct {
	schema.Attr
	S string
}

CreateStmt describes the SQL statement used to create a table.

type DisplayWidth

type DisplayWidth struct {
	schema.Attr
	N int
}

The DisplayWidth represents a display width of an integer type.

type Driver

type Driver struct {
	schema.Differ
	schema.Inspector
	migrate.PlanApplier
	// contains filtered or unexported fields
}

Driver represents a MySQL driver for introspecting database schemas, generating diff between schema elements and apply migrations changes.

func (*Driver) CheckClean

func (d *Driver) CheckClean(ctx context.Context, revT *migrate.TableIdent) error

CheckClean implements migrate.CleanChecker.

func (*Driver) FormatType

func (*Driver) FormatType(t schema.Type) (string, error)

FormatType converts schema type to its column form in the database.

func (*Driver) Lock

func (d *Driver) Lock(ctx context.Context, name string, timeout time.Duration) (schema.UnlockFunc, error)

Lock implements the schema.Locker interface.

func (*Driver) NormalizeRealm

func (d *Driver) NormalizeRealm(ctx context.Context, r *schema.Realm) (*schema.Realm, error)

NormalizeRealm returns the normal representation of the given database.

func (*Driver) NormalizeSchema

func (d *Driver) NormalizeSchema(ctx context.Context, s *schema.Schema) (*schema.Schema, error)

NormalizeSchema returns the normal representation of the given database.

func (*Driver) ParseType

func (*Driver) ParseType(s string) (schema.Type, error)

ParseType returns the schema.Type value represented by the given string.

func (*Driver) RealmRestoreFunc

func (d *Driver) RealmRestoreFunc(desired *schema.Realm) migrate.RestoreFunc

RealmRestoreFunc returns a function that restores the given realm to its desired state.

func (*Driver) ScanStmts

func (*Driver) ScanStmts(input string) ([]*migrate.Stmt, error)

ScanStmts implements migrate.StmtScanner.

func (*Driver) SchemaRestoreFunc

func (d *Driver) SchemaRestoreFunc(desired *schema.Schema) migrate.RestoreFunc

SchemaRestoreFunc returns a function that restores the given schema to its desired state.

func (*Driver) Snapshot

func (d *Driver) Snapshot(ctx context.Context) (migrate.RestoreFunc, error)

Snapshot implements migrate.Snapshoter.

func (*Driver) StmtBuilder

func (*Driver) StmtBuilder(opts migrate.PlanOptions) *sqlx.Builder

StmtBuilder is a helper method used to build statements with MySQL formatting.

func (*Driver) Version

func (d *Driver) Version() string

Version returns the version of the connected database.

type Enforced

type Enforced struct {
	schema.Attr
	V bool // V indicates if the CHECK is enforced or not.
}

Enforced attribute defines the ENFORCED flag for CHECK constraint.

type Engine

type Engine struct {
	schema.Attr
	V       string // InnoDB, MyISAM, etc.
	Default bool   // The default engine used by the server.
}

Engine attribute describes the storage engine used to create a table.

type IndexParser

type IndexParser struct {
	schema.Attr
	P string // Name of the parser plugin. e.g., ngram or mecab.
}

IndexParser defines the parser plugin used by a FULLTEXT index.

type IndexType

type IndexType struct {
	schema.Attr
	T string // BTREE, HASH, FULLTEXT, SPATIAL, RTREE
}

IndexType represents an index type.

type NetworkType

type NetworkType struct {
	schema.Type
	T string
}

NetworkType stores an IPv4 or IPv6 address.

type OnUpdate

type OnUpdate struct {
	schema.Attr
	A string
}

OnUpdate attribute for columns with "ON UPDATE CURRENT_TIMESTAMP" as a default.

type SetType

type SetType struct {
	schema.Type
	Values []string
}

SetType represents a set type.

type SubPart

type SubPart struct {
	schema.Attr
	Len int
}

SubPart attribute defines an option index prefix length for columns.

type SystemVersioned

type SystemVersioned struct {
	schema.Attr
}

SystemVersioned is an attribute attached to MariaDB tables indicates they are system versioned. See: https://mariadb.com/kb/en/system-versioned-tables

type ZeroFill

type ZeroFill struct {
	schema.Attr
	A string
}

The ZeroFill represents the ZEROFILL attribute which is deprecated for MySQL version >= 8.0.17.

Source Files

convert.go diff_oss.go driver_oss.go inspect_oss.go migrate_oss.go sqlspec_oss.go tidb.go

Directories

PathSynopsis
sql/mysql/internal
sql/mysql/mysqlcheck
Version
v0.32.0 (latest)
Published
Mar 10, 2025
Platform
linux/amd64
Imports
24 packages
Last checked
1 month ago

Tools for package owners.