schema

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2021 License: Apache-2.0 Imports: 3 Imported by: 41

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNotExistError

func IsNotExistError(err error) bool

IsNotExistError reports if an error is a NotExistError.

Types

type AddAttr

type AddAttr struct {
	A Attr
}

AddAttr describes an attribute addition.

type AddColumn

type AddColumn struct {
	C *Column
}

AddColumn describes a column creation change.

type AddForeignKey

type AddForeignKey struct {
	F *ForeignKey
}

AddForeignKey describes a foreign-key creation change.

type AddIndex

type AddIndex struct {
	I *Index
}

AddIndex describes an index creation change.

type AddTable

type AddTable struct {
	T     *Table
	Extra []Expr // Extra clauses and options.
}

AddTable describes a table creation change.

type Attr

type Attr interface {
	// contains filtered or unexported methods
}

Attr represents the interface that all attributes implement.

type BinaryType

type BinaryType struct {
	T    string
	Size int
}

BinaryType represents a type that stores a binary data.

type BoolType

type BoolType struct {
	T string
}

BoolType represents a boolean type.

type Change

type Change interface {
	// contains filtered or unexported methods
}

A Change represents a schema change. The types below implement this interface and can be used for describing schema changes.

The Change interface can also be implemented outside this package as follows:

type RenameType struct {
	schema.Change
	From, To string
}

var t schema.Change = &RenameType{From: "old", To: "new"}

type ChangeKind

type ChangeKind uint

A ChangeKind describes a change kind that can be combined using a set of flags. The zero kind is no change.

const (
	// NoChange holds the zero value of a change kind.
	NoChange ChangeKind = 0

	// ChangeAttr describes attributes change of an element.
	// For example, a table CHECK was added or changed.
	ChangeAttr ChangeKind = 1 << (iota - 1)
	// ChangeCharset describes character-set change.
	ChangeCharset
	// ChangeCollation describes collation/encoding change.
	ChangeCollation
	// ChangeComment describes comment chang (of any element).
	ChangeComment

	// ChangeNull describe a change to the NULL constraint.
	ChangeNull
	// ChangeType describe a column type change.
	ChangeType
	// ChangeDefault describe a column default change.
	ChangeDefault

	// ChangeUnique describes a change to the uniqueness constraint.
	// For example, an index was changed from non-unique to unique.
	ChangeUnique
	// ChangeParts describes a change to one or more of the index parts.
	// For example, index keeps its previous name, but the columns order
	// was changed.
	ChangeParts

	// ChangeColumn describes a change to the foreign-key (child) columns.
	ChangeColumn
	// ChangeRefColumn describes a change to the foreign-key (parent) columns.
	ChangeRefColumn
	// ChangeRefTable describes a change to the foreign-key (parent) table.
	ChangeRefTable
	// ChangeUpdateAction describes a change to the foreign-key update action.
	ChangeUpdateAction
	// ChangeDeleteAction describes a change to the foreign-key delete action.
	ChangeDeleteAction
)

func (ChangeKind) Is

func (k ChangeKind) Is(c ChangeKind) bool

Is reports whether c is match the given change kind.

type Charset

type Charset struct {
	V string
}

Charset describes a column or a table character set setting.

type Collation

type Collation struct {
	V string
}

Collation describes a column or a table collation setting.

type Column

type Column struct {
	Name        string
	Type        *ColumnType
	Default     Expr
	Attrs       []Attr
	Indexes     []*Index
	ForeignKeys []*ForeignKey
}

A Column represents a column definition.

type ColumnType

type ColumnType struct {
	Type Type
	Raw  string
	Null bool
}

ColumnType represents a column type that is implemented by the dialect.

type Comment

type Comment struct {
	Text string
}

Comment describes a schema element comment.

type DecimalType

type DecimalType struct {
	T         string
	Precision int
	Scale     int
}

DecimalType represents a fixed-point type that stores exact numeric values.

type Differ

type Differ interface {
	// SchemaDiff returns a diff report for migrating a schema
	// from state "from" to state "to". An error is returned
	// if such step is not possible.
	SchemaDiff(from, to *Schema) ([]Change, error)

	// TableDiff returns a diff report for migrating a table
	// from state "from" to state "to". An error is returned
	// if such step is not possible.
	TableDiff(from, to *Table) ([]Change, error)
}

Differ is the interface implemented by the different drivers for comparing and diffing schema top elements.

type DropAttr

type DropAttr struct {
	A Attr
}

DropAttr describes an attribute removal.

type DropColumn

type DropColumn struct {
	C *Column
}

DropColumn describes a column removal change.

type DropForeignKey

type DropForeignKey struct {
	F *ForeignKey
}

DropForeignKey describes a foreign-key removal change.

type DropIndex

type DropIndex struct {
	I *Index
}

DropIndex describes an index removal change.

type DropTable

type DropTable struct {
	T     *Table
	Extra []Expr // Extra clauses.
}

DropTable describes a table removal change.

type EnumType

type EnumType struct {
	T      string   // Optional type.
	Values []string // Enum values.
}

EnumType represents an enum type.

type ExecQuerier

type ExecQuerier interface {
	QueryRow(query string, args ...interface{}) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

ExecQuerier wraps the standard sql.DB methods.

type Execer

type Execer interface {
	// Exec responsible for executing the given changeset.
	// An error may return from Exec if the driver does not
	// know how to execute a change.
	Exec(ctx context.Context, changes []Change) error
}

Execer is the interface implemented by the different drivers for executing schema changes.

type Expr

type Expr interface {
	// contains filtered or unexported methods
}

Expr defines an SQL expression in schema DDL.

type FloatType

type FloatType struct {
	T         string
	Precision int
}

FloatType represents a floating-point type that stores approximate numeric values.

type ForeignKey

type ForeignKey struct {
	Symbol     string
	Table      *Table
	Columns    []*Column
	RefTable   *Table
	RefColumns []*Column
	OnUpdate   ReferenceOption
	OnDelete   ReferenceOption
}

A ForeignKey represents an index definition.

func (*ForeignKey) Column

func (f *ForeignKey) Column(name string) (*Column, bool)

Column returns the first column that matches the given name.

func (*ForeignKey) RefColumn

func (f *ForeignKey) RefColumn(name string) (*Column, bool)

RefColumn returns the first referenced column that matches the given name.

type Index

type Index struct {
	Name   string
	Unique bool
	Table  *Table
	Attrs  []Attr
	Parts  []*IndexPart
}

An Index represents an index definition.

type IndexPart

type IndexPart struct {
	SeqNo int
	Attrs []Attr
	X     Expr
	C     *Column
}

An IndexPart represents an index part that can be either an expression or a column.

type InspectOptions

type InspectOptions struct {
	// Tables to inspect. Empty means all tables in the schema.
	Tables []string
}

InspectOptions describes options for Inspector.

type InspectRealmOption

type InspectRealmOption struct {
	// Schemas to inspect. Empty means all tables in the schema.
	Schemas []string
}

InspectRealmOption describes options for RealmInspector.

type InspectTableOptions

type InspectTableOptions struct {
	// Schema defines an optional schema to inspect.
	Schema string
}

InspectTableOptions describes options for TableInspector.

type Inspector

type Inspector interface {
	// InspectSchema returns the schema description by its name. A NotExistError
	// error is returned if the schema does not exists in the database.
	InspectSchema(ctx context.Context, name string, opts *InspectOptions) (*Schema, error)
}

Inspector is the interface implemented by the different database drivers for inspecting multiple tables.

type IntegerType

type IntegerType struct {
	T        string
	Unsigned bool
	Attrs    []Attr
}

IntegerType represents an int type.

type JSONType

type JSONType struct {
	T string
}

JSONType represents a JSON type.

type Literal

type Literal struct {
	V string
}

Literal represents a basic literal expression like "1".

type ModifyAttr

type ModifyAttr struct {
	From, To Attr
}

ModifyAttr describes a change that modifies an element attribute.

type ModifyColumn

type ModifyColumn struct {
	From, To *Column
	Change   ChangeKind
}

ModifyColumn describes a change that modifies a column.

type ModifyForeignKey

type ModifyForeignKey struct {
	From, To *ForeignKey
	Change   ChangeKind
}

ModifyForeignKey describes a change that modifies a foreign-key.

type ModifyIndex

type ModifyIndex struct {
	From, To *Index
	Change   ChangeKind
}

ModifyIndex describes an index modification.

type ModifyTable

type ModifyTable struct {
	T       *Table
	Changes []Change
}

ModifyTable describes a table modification change.

type NotExistError

type NotExistError struct {
	Err error
}

A NotExistError wraps another error to retain its original text but makes it possible to the migrator to catch it.

func (NotExistError) Error

func (e NotExistError) Error() string

type RawExpr

type RawExpr struct {
	X string
}

RawExpr represents a raw expression like "uuid()".

type Realm

type Realm struct {
	Schemas []*Schema
	Attrs   []Attr
}

A Realm or a database describes a domain of schema resources that are logically connected and can be accessed and queried in the same connection (e.g. a physical database instance).

type RealmInspector

type RealmInspector interface {
	InspectRealm(ctx context.Context, opts *InspectRealmOption) (*Realm, error)
}

RealmInspector is the interface implemented by the different database drivers for inspecting multiple schemas (realm).

type ReferenceOption

type ReferenceOption string

ReferenceOption for constraint actions.

const (
	NoAction   ReferenceOption = "NO ACTION"
	Restrict   ReferenceOption = "RESTRICT"
	Cascade    ReferenceOption = "CASCADE"
	SetNull    ReferenceOption = "SET NULL"
	SetDefault ReferenceOption = "SET DEFAULT"
)

Reference options (actions) specified by ON UPDATE and ON DELETE subclauses of the FOREIGN KEY clause.

type Schema

type Schema struct {
	Name   string
	Realm  *Realm
	Tables []*Table
	Attrs  []Attr // Attributes and options.
}

A Schema describes a database schema (i.e. named database).

func (*Schema) Table

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

Table returns the first table that matched the given name.

type SpatialType

type SpatialType struct {
	T string
}

SpatialType represents a spatial/geometric type.

type StringType

type StringType struct {
	T    string
	Size int
}

StringType represents a string type.

type Table

type Table struct {
	Name        string
	Schema      *Schema
	Columns     []*Column
	Indexes     []*Index
	PrimaryKey  *Index
	ForeignKeys []*ForeignKey
	Attrs       []Attr // Attributes, constraints and options.
}

A Table represents a table definition.

func (*Table) Column

func (t *Table) Column(name string) (*Column, bool)

Column returns the first column that matched the given name.

func (*Table) ForeignKey

func (t *Table) ForeignKey(symbol string) (*ForeignKey, bool)

ForeignKey returns the first foreign-key that matched the given symbol (constraint name).

func (*Table) Index

func (t *Table) Index(name string) (*Index, bool)

Index returns the first index that matched the given name.

type TableInspector

type TableInspector interface {
	// InspectTable returns the table description by its name. A NotExistError
	// error is returned if the table does not exists in the database.
	InspectTable(ctx context.Context, name string, opts *InspectTableOptions) (*Table, error)
}

TableInspector is the interface implemented by the different database drivers for inspecting their schema tables.

type TimeType

type TimeType struct {
	T string
}

TimeType represents a date/time type.

type Type

type Type interface {
	// contains filtered or unexported methods
}

A Type represents a database type. The types below implements this interface and can be used for describing schemas.

The Type interface can also be implemented outside this package as follows:

type SpatialType struct {
	schema.Type
	T string
}

var t schema.Type = &SpatialType{T: "point"}

type UnsupportedType

type UnsupportedType struct {
	T string
}

UnsupportedType represents a type that is not supported by the drivers.

Jump to

Keyboard shortcuts

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