Documentation ¶
Index ¶
- Variables
- func ApplyChanges(ctx context.Context, changes []schema.Change, p execPlanner, ...) error
- func Charset(attr, parent []schema.Attr) (string, bool)
- func CheckChangesScope(opts migrate.PlanOptions, changes []schema.Change) error
- func CheckDiff(from, to *schema.Table, compare ...func(c1, c2 *schema.Check) bool) []schema.Change
- func Collate(attr, parent []schema.Attr) (string, bool)
- func CommentChange(from, to []schema.Attr) schema.ChangeKind
- func CommentDiff(from, to []schema.Attr) schema.Change
- func DefaultValue(c *schema.Column) (string, bool)
- func DetachCycles(changes []schema.Change) ([]schema.Change, error)
- func ExcludeRealm(r *schema.Realm, patterns []string) (*schema.Realm, error)
- func ExcludeSchema(s *schema.Schema, patterns []string) (*schema.Schema, error)
- func ExprLastIndex(expr string) int
- func Has(elements, target any) bool
- func IsLiteralBool(s string) bool
- func IsLiteralNumber(s string) bool
- func IsQuoted(s string, q ...byte) bool
- func IsUint(s string) bool
- func LinkSchemaTables(schemas []*schema.Schema)
- func MayWrap(s string) string
- func ModeInspectRealm(o *schema.InspectRealmOption) schema.InspectMode
- func ModeInspectSchema(o *schema.InspectOptions) schema.InspectMode
- func P[T any](v T) *T
- func PlanViewChanges(changes []schema.Change) ([]schema.Change, error)
- func ReverseChanges(c []schema.Change)
- func ScanNullBool(rows *sql.Rows) (sql.NullBool, error)
- func ScanOne(rows *sql.Rows, dest ...any) error
- func ScanStrings(rows *sql.Rows) ([]string, error)
- func SchemaFKs(s *schema.Schema, rows *sql.Rows) error
- func SetReversible(p *migrate.Plan) error
- func SingleQuote(s string) (string, error)
- func TrimViewExtra(s string) string
- func TypedSchemaFKs[T ScanStringer](s *schema.Schema, rows *sql.Rows) error
- func Unquote(s string) (string, error)
- func V[T any](p *T) (v T)
- func ValidString(s sql.NullString) bool
- func ValuesEqual(v1, v2 []string) bool
- type ApplyError
- type Builder
- func (b *Builder) Clone() *Builder
- func (b *Builder) Comma() *Builder
- func (b *Builder) Ident(s string) *Builder
- func (b *Builder) IndentIn() *Builder
- func (b *Builder) IndentOut() *Builder
- func (b *Builder) MapComma(x any, f func(i int, b *Builder)) *Builder
- func (b *Builder) MapCommaErr(x any, f func(i int, b *Builder) error) error
- func (b *Builder) MapIndent(x any, f func(i int, b *Builder)) *Builder
- func (b *Builder) MapIndentErr(x any, f func(i int, b *Builder) error) error
- func (b *Builder) NL() *Builder
- func (b *Builder) P(phrases ...string) *Builder
- func (b *Builder) Quote(prefix string, fn func(b *Builder)) *Builder
- func (b *Builder) String() string
- func (b *Builder) Table(t *schema.Table) *Builder
- func (b *Builder) TableResource(t *schema.Table, r any) *Builder
- func (b *Builder) View(v *schema.View) *Builder
- func (b *Builder) Wrap(f func(b *Builder)) *Builder
- func (b *Builder) WrapIndent(f func(b *Builder)) *Builder
- type ChangesAnnotator
- type DevDriver
- type Diff
- func (d *Diff) RealmDiff(from, to *schema.Realm, options ...schema.DiffOption) ([]schema.Change, error)
- func (d *Diff) SchemaDiff(from, to *schema.Schema, options ...schema.DiffOption) ([]schema.Change, error)
- func (d *Diff) TableDiff(from, to *schema.Table, options ...schema.DiffOption) ([]schema.Change, error)
- type DiffDriver
- type ExecQueryCloser
- type Normalizer
- type ScanStringer
- type TableFinder
- type UnsupportedTypeError
Constants ¶
This section is empty.
Variables ¶
var NoRows schema.ExecQuerier = (*noRows)(nil)
NoRows to be used by differs and planners without a connection.
Functions ¶
func ApplyChanges ¶ added in v0.2.0
func ApplyChanges(ctx context.Context, changes []schema.Change, p execPlanner, opts ...migrate.PlanOption) error
ApplyChanges is a helper used by the different drivers to apply changes.
func Charset ¶ added in v0.12.1
Charset reports if the attribute contains the "charset" attribute, and it needs to be defined explicitly on the schema. This is true, in case the element charset is different from its parent charset.
func CheckChangesScope ¶ added in v0.6.0
func CheckChangesScope(opts migrate.PlanOptions, changes []schema.Change) error
CheckChangesScope checks that changes can be applied on a schema scope (connection).
func CheckDiff ¶ added in v0.2.0
CheckDiff computes the change diff between the 2 tables. A compare function is provided to check if a Check object was modified.
func Collate ¶ added in v0.12.1
Collate reports if the attribute contains the "collation"/"collate" attribute, and it needs to be defined explicitly on the schema. This is true, in case the element collation is different from its parent collation.
func CommentChange ¶
func CommentChange(from, to []schema.Attr) schema.ChangeKind
CommentChange reports if the element comment was changed.
func CommentDiff ¶ added in v0.3.0
CommentDiff computes the comment diff between the 2 attribute list. Note that, the implementation relies on the fact that both PostgreSQL and MySQL treat empty comment as "no comment" and a way to clear comments.
func DefaultValue ¶ added in v0.2.0
DefaultValue returns the string represents the DEFAULT of a column.
func DetachCycles ¶
DetachCycles takes a list of schema changes, and detaches references between changes if there is at least one circular reference in the changeset. More explicitly, it postpones fks creation, or deletes fks before deletes their tables.
func ExcludeRealm ¶ added in v0.6.0
ExcludeRealm filters resources in the realm based on the given patterns.
func ExcludeSchema ¶ added in v0.6.0
ExcludeSchema filters resources in the schema based on the given patterns.
func ExprLastIndex ¶ added in v0.3.8
ExprLastIndex scans the first expression in the given string until its end and returns its last index.
func Has ¶
Has finds the first element in the elements list that matches target, and if so, sets target to that attribute value and returns true.
func IsLiteralBool ¶ added in v0.2.0
IsLiteralBool reports if the given string is a valid literal bool.
func IsLiteralNumber ¶ added in v0.2.0
IsLiteralNumber reports if the given string is a literal number.
func IsQuoted ¶ added in v0.2.0
IsQuoted reports if the given string is quoted with one of the given quotes (e.g. ', ", `).
func LinkSchemaTables ¶
LinkSchemaTables links foreign-key stub tables/columns to actual elements.
func MayWrap ¶ added in v0.3.7
MayWrap ensures the given string is wrapped with parentheses. Used by the different drivers to turn strings valid expressions.
func ModeInspectRealm ¶ added in v0.3.8
func ModeInspectRealm(o *schema.InspectRealmOption) schema.InspectMode
ModeInspectRealm returns the InspectMode or its default.
func ModeInspectSchema ¶ added in v0.3.8
func ModeInspectSchema(o *schema.InspectOptions) schema.InspectMode
ModeInspectSchema returns the InspectMode or its default.
func PlanViewChanges ¶ added in v0.12.1
PlanViewChanges (should) plan view changes in the current order they should be applied. It is unimplemented for community version as views are not supported there.
func ReverseChanges ¶ added in v0.4.1
ReverseChanges reverses the order of the changes.
func ScanNullBool ¶ added in v0.3.8
ScanNullBool scans one sql.NullBool record and closes the rows at the end.
func ScanStrings ¶
ScanStrings scans sql.Rows into a slice of strings and closes it at the end.
func SchemaFKs ¶ added in v0.3.7
SchemaFKs scans the rows and adds the foreign-key to the schema table. Reference elements are added as stubs and should be linked manually by the caller.
func SetReversible ¶ added in v0.9.0
SetReversible sets the Reversible field to true if all planned changes are reversible.
func SingleQuote ¶ added in v0.3.0
SingleQuote quotes the given string with single quote.
func TrimViewExtra ¶ added in v0.12.1
TrimViewExtra trims the extra unnecessary characters from the view definition.
func TypedSchemaFKs ¶ added in v0.14.0
TypedSchemaFKs is a version of SchemaFKs that allows to specify the type of used to scan update and delete actions from the database.
func V ¶ added in v0.6.4
func V[T any](p *T) (v T)
V returns the value p is pointing to. If p is nil, the zero value is returned.
func ValidString ¶
func ValidString(s sql.NullString) bool
ValidString reports if the given string is not null and valid.
func ValuesEqual ¶
ValuesEqual checks if the 2 string slices are equal (including their order).
Types ¶
type ApplyError ¶ added in v0.8.3
type ApplyError struct {
// contains filtered or unexported fields
}
ApplyError is an error that exposes an information for getting how any changes were applied before encountering the failure.
func (*ApplyError) Applied ¶ added in v0.8.3
func (e *ApplyError) Applied() int
Applied reports how many changes were applied before getting an error. In case the first change was failed, Applied() returns 0.
func (*ApplyError) Error ¶ added in v0.8.3
func (e *ApplyError) Error() string
Error implements the error interface.
type Builder ¶
type Builder struct { bytes.Buffer QuoteOpening byte // quoting identifiers QuoteClosing byte // quoting identifiers Schema *string // schema qualifier Indent string // indentation string // contains filtered or unexported fields }
A Builder provides a syntactic sugar API for writing SQL statements.
func (*Builder) Comma ¶
Comma writes a comma in case the buffer is not empty, or replaces the last char if it is a whitespace.
func (*Builder) MapComma ¶
MapComma maps the slice x using the function f and joins the result with a comma separating between the written elements.
func (*Builder) MapCommaErr ¶ added in v0.2.0
MapCommaErr is like MapComma, but returns an error if f returns an error.
func (*Builder) MapIndent ¶ added in v0.10.0
MapIndent is like MapComma, but writes a new line before each element.
func (*Builder) MapIndentErr ¶ added in v0.10.0
MapIndentErr is like MapCommaErr, but writes a new line before each element.
func (*Builder) NL ¶ added in v0.10.0
NL adds line break and prefix the new line with indentation in case indentation is enabled.
func (*Builder) P ¶
P writes a list of phrases to the builder separated and suffixed with whitespace.
func (*Builder) Quote ¶ added in v0.12.1
Quote wraps the given function with a single quote and a prefix
func (*Builder) String ¶
String overrides the Buffer.String method and ensure no spaces pad the returned statement.
func (*Builder) Table ¶
Table writes the table identifier to the builder, prefixed with the schema name if exists.
func (*Builder) TableResource ¶ added in v0.12.1
TableResource writes the table's resource identifier to the builder, prefixed with the schema name if exists.
func (*Builder) View ¶ added in v0.12.1
View writes the view identifier to the builder, prefixed with the schema name if exists.
func (*Builder) WrapIndent ¶ added in v0.10.0
WrapIndent is like Wrap but with extra level of indentation.
type ChangesAnnotator ¶ added in v0.11.0
type ChangesAnnotator interface {
AnnotateChanges([]schema.Change, *schema.DiffOptions) error
}
ChangesAnnotator is an optional interface allows DiffDriver to annotate changes with additional driver-specific attributes before they are returned.
type DevDriver ¶ added in v0.3.7
type DevDriver struct { // A Driver connected to the dev database. Driver interface { migrate.Driver migrate.CleanChecker migrate.Snapshoter } // PathObject allows providing a custom function to patch // objects that hold a schema reference. PatchObject func(*schema.Schema, schema.Object) }
DevDriver is a driver that provides additional functionality to interact with the development database.
func (*DevDriver) NormalizeRealm ¶ added in v0.3.7
func (d *DevDriver) NormalizeRealm(ctx context.Context, r *schema.Realm) (nr *schema.Realm, err error)
NormalizeRealm implements the schema.Normalizer interface.
The implementation converts schema objects in "natural form" (e.g. HCL or DSL) to their "normal presentation" in the database, by creating them temporarily in a "dev database", and then inspects them from there.
type Diff ¶
type Diff struct {
DiffDriver
}
A Diff provides a generic schema.Differ for diffing schema elements.
The DiffDriver is required for supporting database/dialect specific diff capabilities, like diffing custom types or attributes.
func (*Diff) RealmDiff ¶ added in v0.2.0
func (d *Diff) RealmDiff(from, to *schema.Realm, options ...schema.DiffOption) ([]schema.Change, error)
RealmDiff implements the schema.Differ for Realm objects and returns a list of changes that need to be applied in order to move a database from the current state to the desired.
func (*Diff) SchemaDiff ¶
func (d *Diff) SchemaDiff(from, to *schema.Schema, options ...schema.DiffOption) ([]schema.Change, error)
SchemaDiff implements the schema.Differ interface and returns a list of changes that need to be applied in order to move from one state to the other.
type DiffDriver ¶
type DiffDriver interface { // SchemaAttrDiff returns a changeset for migrating schema attributes // from one state to the other. For example, changing schema collation. SchemaAttrDiff(from, to *schema.Schema) []schema.Change // SchemaObjectDiff returns a changeset for migrating schema objects from // one state to the other. For example, changing schema custom types. SchemaObjectDiff(from, to *schema.Schema) ([]schema.Change, error) // TableAttrDiff returns a changeset for migrating table attributes from // one state to the other. For example, dropping or adding a `CHECK` constraint. TableAttrDiff(from, to *schema.Table) ([]schema.Change, error) // ViewAttrChanged reports if the view attributes were changed. // For example, a view was changed to a materialized view. ViewAttrChanged(from, to *schema.View) bool // ColumnChange returns the schema changes (if any) for migrating one column to the other. ColumnChange(fromT *schema.Table, from, to *schema.Column) (schema.ChangeKind, error) // IndexAttrChanged reports if the index attributes were changed. // For example, an index type or predicate (for partial indexes). IndexAttrChanged(from, to []schema.Attr) bool // IndexPartAttrChanged reports if the part's attributes at position "i" // were changed. For example, an index-part collation. IndexPartAttrChanged(from, to *schema.Index, i int) bool // IsGeneratedIndexName reports if the index name was generated by the database // for unnamed INDEX or UNIQUE constraints. In such cases, the Differ will look // for unnamed schema.Indexes on the desired state, before tagging the index as // a candidate for deletion. IsGeneratedIndexName(*schema.Table, *schema.Index) bool // ReferenceChanged reports if the foreign key referential action was // changed. For example, action was changed from RESTRICT to CASCADE. ReferenceChanged(from, to schema.ReferenceOption) bool }
A DiffDriver wraps all required methods for diffing elements that may have database-specific diff logic. See sql/schema/mysql/diff.go for an implementation example.
type ExecQueryCloser ¶ added in v0.3.8
type ExecQueryCloser interface { schema.ExecQuerier io.Closer }
ExecQueryCloser is the interface that groups Close with the schema.ExecQuerier methods.
func SingleConn ¶ added in v0.3.8
func SingleConn(ctx context.Context, conn schema.ExecQuerier) (ExecQueryCloser, error)
SingleConn returns a closable single connection from the given ExecQuerier. If the ExecQuerier is already bound to a single connection (e.g. Tx, Conn), the connection will return as-is with a NopCloser.
type Normalizer ¶
A Normalizer wraps the Normalize method for normalizing the from and to tables before running diffing. The "from" usually represents the inspected database state (current), and the second represents the desired state.
If the DiffDriver implements the Normalizer interface, TableDiff normalizes its table inputs before starting the diff process.
type ScanStringer ¶ added in v0.14.0
ScanStringer groups the fmt.Stringer and sql.Scanner interfaces.
type TableFinder ¶ added in v0.9.1
TableFinder wraps the FindTable method, providing more control to the DiffDriver on how tables are matched.
type UnsupportedTypeError ¶
UnsupportedTypeError describes an unsupported type error.
func (UnsupportedTypeError) Error ¶
func (e UnsupportedTypeError) Error() string