Help us shape the future of Go! Take the 2026 Go Developer Survey

sqlx

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoChange = schema.Change(nil)

NoChange can be returned by DiffDriver methods to indicate that no change is needed.

Functions

func AttrOr

func AttrOr[T schema.Attr](attrs []schema.Attr, t T) T

AttrOr returns the first attribute of the given type, or the given default value.

func BodyDefChanged

func BodyDefChanged(from, to string) bool

BodyDefChanged reports if the body definition of a function, procedure, view, or trigger has changed. There is a small task here that normalizes the indentation, which might be added during inspection or by the user.

func Charset

func Charset(attr, parent []schema.Attr) (string, bool)

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

func CheckChangesScope(opts migrate.PlanOptions, changes []schema.Change) error

CheckChangesScope checks that changes can be applied on a schema scope (connection).

func CheckDiffMode

func CheckDiffMode(from, to *schema.Table, mode schema.DiffMode, compare ...func(c1, c2 *schema.Check) bool) []schema.Change

CheckDiffMode is like CheckDiff, but compares also expressions if the schema.DiffMode is equal to schema.DiffModeNormalized.

func ChecksDiff

func ChecksDiff(from, to *schema.Table, compare func(c1, c2 *schema.Check) bool) []schema.Change

ChecksDiff computes the change diff between the 2 tables.

func Collate

func Collate(attr, parent []schema.Attr) (string, bool)

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

func CommentDiff(from, to []schema.Attr) schema.Change

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

func DefaultValue(c *schema.Column) (string, bool)

DefaultValue returns the string represents the DEFAULT of a column.

func DetachCycles

func DetachCycles(changes []schema.Change) ([]schema.Change, error)

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 ExprLastIndex

func ExprLastIndex(expr string) int

ExprLastIndex scans the first expression in the given string until its end and returns its last index.

func Has

func Has(elements, target any) bool

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

func IsLiteralBool(s string) bool

IsLiteralBool reports if the given string is a valid literal bool.

func IsLiteralNumber

func IsLiteralNumber(s string) bool

IsLiteralNumber reports if the given string is a literal number.

func IsQuoted

func IsQuoted(s string, q ...byte) bool

IsQuoted reports if the given string is quoted with one of the given quotes (e.g. ', ", `).

func IsUint

func IsUint(s string) bool

IsUint reports whether the string represents an unsigned integer.

func MayWrap

func MayWrap(s string) string

MayWrap ensures the given string is wrapped with parentheses. Used by the different drivers to turn strings valid expressions.

func P

func P[T any](v T) *T

P returns a pointer to v.

func ReverseChanges

func ReverseChanges(c []schema.Change)

ReverseChanges reverses the order of the changes.

func SameSchema

func SameSchema(s1, s2 *schema.Schema) bool

SameSchema reports if the given schemas are the same. Objects can be different as they might reside in two different states (current and desired).

func SameTable

func SameTable(t1, t2 *schema.Table) bool

SameTable reports if the two objects represent the same table.

func SameView

func SameView(v1, v2 *schema.View) bool

SameView reports if the two objects represent the same view.

func SetReversible

func SetReversible(p *migrate.Plan) error

func SingleQuote

func SingleQuote(s string) (string, error)

SingleQuote quotes the given string with single quote.

func SortChanges

func SortChanges(changes []schema.Change, opts *SortOptions) ([]schema.Change, error)

SortChanges is a helper function to sort to level changes based on their priority.

func TrimViewExtra

func TrimViewExtra(s string) string

TrimViewExtra trims the extra unnecessary characters from the view definition.

func Unquote

func Unquote(s string) (string, error)

Unquote single or double quotes.

func V

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 ValuesEqual

func ValuesEqual(v1, v2 []string) bool

Types

type Builder

type Builder struct {
	QuoteOpening byte    // quoting identifiers
	QuoteClosing byte    // quoting identifiers
	Schema       *string // schema qualifier
	Indent       string  // indentation string
	// contains filtered or unexported fields
}

func (*Builder) Byte

func (b *Builder) Byte(value byte)

func (*Builder) Clone

func (b *Builder) Clone() *Builder

Clone returns a duplicate of the builder.

func (*Builder) Comma

func (b *Builder) Comma() *Builder

Comma writes a comma in case the buffer is not empty, or replaces the last char if it is a whitespace.

func (*Builder) Func

func (b *Builder) Func(f *schema.Func) *Builder

Func writes the function identifier to the builder, prefixed with the schema name if exists.

func (*Builder) FuncCall

func (b *Builder) FuncCall(f *schema.Func, args ...string) *Builder

FuncCall writes the function identifier to the builder as a function call,

func (*Builder) Ident

func (b *Builder) Ident(s string) *Builder

Ident writes the given string quoted as an SQL identifier.

func (*Builder) IndentIn

func (b *Builder) IndentIn() *Builder

IndentIn adds one indentation in.

func (*Builder) IndentOut

func (b *Builder) IndentOut() *Builder

IndentOut removed one indentation level.

func (*Builder) Int64

func (b *Builder) Int64(v int64) *Builder

Int64 writes the given value to the builder in base 10.

func (*Builder) Len

func (b *Builder) Len() int

func (*Builder) MapComma

func (b *Builder) MapComma(x any, f func(i int, b *Builder)) *Builder

MapComma maps the slice x using the function f and joins the result with a comma separating between the written elements.

func (*Builder) MapCommaErr

func (b *Builder) MapCommaErr(x any, f func(i int, b *Builder) error) error

MapCommaErr is like MapComma, but returns an error if f returns an error.

func (*Builder) MapIndent

func (b *Builder) MapIndent(x any, f func(i int, b *Builder)) *Builder

MapIndent is like MapComma, but writes a new line before each element.

func (*Builder) MapIndentErr

func (b *Builder) MapIndentErr(x any, f func(i int, b *Builder) error) error

MapIndentErr is like MapCommaErr, but writes a new line before each element.

func (*Builder) NL

func (b *Builder) NL() *Builder

NL adds line break and prefix the new line with indentation in case indentation is enabled.

func (*Builder) P

func (b *Builder) P(phrases ...string) *Builder

P writes a list of phrases to the builder separated and suffixed with whitespace.

func (*Builder) Proc

func (b *Builder) Proc(p *schema.Proc) *Builder

Proc writes the procedure identifier to the builder, prefixed with the schema name if exists.

func (*Builder) ProcCall

func (b *Builder) ProcCall(p *schema.Proc, args ...string) *Builder

ProcCall writes the procedure identifier to the builder as a procedure call,

func (*Builder) Quote

func (b *Builder) Quote(prefix string, fn func(b *Builder)) *Builder

Quote wraps the given function with a single quote and a prefix

func (*Builder) RefTable

func (b *Builder) RefTable(childT, parentT *schema.Table) *Builder

RefTable writes the referenced/parent table identifier to the builder. Unlike the Table method, RefTable prefix the table with its schema qualifier if the "Schema" is set to nil, or if the schema is set to empty (schema-scope), and the parent table is in a different schema. For example:

CREATE TABLE child (
	id INT PRIMARY KEY,
	parent_id1 INT REFERENCES other1.parent(id)
	parent_id2 INT REFERENCES other2.parent(id)
);

This case is possible only if the child table (and its schema) was loaded with baseline schema (contains other1, other2) or with external references to resources not managed by this scope.

func (*Builder) SchemaResource

func (b *Builder) SchemaResource(s *schema.Schema, name string) *Builder

SchemaResource writes the schema resource identifier to the builder, prefixed with the schema name if exists.

func (*Builder) String

func (b *Builder) String() string

String returns the statement without padding.

func (*Builder) Table

func (b *Builder) Table(t *schema.Table) *Builder

Table writes the table identifier to the builder, prefixed with the schema name if exists.

func (*Builder) TableColumn

func (b *Builder) TableColumn(t *schema.Table, c *schema.Column) *Builder

TableColumn writes the table's resource identifier to the builder, prefixed with the schema name if exists.

func (*Builder) TableResource

func (b *Builder) TableResource(t *schema.Table, r any) *Builder

TableResource writes the table resource identifier to the builder, prefixed with the schema name if exists.

func (*Builder) Text

func (b *Builder) Text(value string)

func (*Builder) View

func (b *Builder) View(v *schema.View) *Builder

View writes the view identifier to the builder, prefixed with the schema name if exists.

func (*Builder) ViewResource

func (b *Builder) ViewResource(v *schema.View, r any) *Builder

ViewResource writes the view resource identifier to the builder, prefixed with the schema name if exists.

func (*Builder) Wrap

func (b *Builder) Wrap(f func(b *Builder)) *Builder

Wrap wraps the written string with parentheses.

func (*Builder) WrapErr

func (b *Builder) WrapErr(f func(b *Builder) error) error

WrapErr wraps the written string with parentheses

func (*Builder) WrapIndent

func (b *Builder) WrapIndent(f func(b *Builder)) *Builder

WrapIndent is like Wrap but with extra level of indentation.

func (*Builder) WrapIndentErr

func (b *Builder) WrapIndentErr(f func(b *Builder) error) error

WrapIndentErr is like WrapErr but with extra level of indentation.

type ChangeSupporter

type ChangeSupporter interface {
	// SupportChange can be implemented to tell the Differ if they support
	// a specific change type, or it should avoid suggesting it.
	SupportChange(schema.Change) bool
}

ChangeSupporter wraps the single SupportChange method.

type ChangesAnnotator

type ChangesAnnotator interface {
	AnnotateChanges([]schema.Change, *schema.DiffOptions) ([]schema.Change, error)
}

ChangesAnnotator is an optional interface allows DiffDriver to annotate changes with additional driver-specific attributes before they are returned.

type Depender

type Depender interface {
	DependsOn(change, other schema.Change) bool
	DependencyOf(change, other schema.Change) bool
}

Depender can be implemented by an object to determine if a change to it depends on other change, or if other change depends on it. For example: A table creation depends on type creation, and a type deletion depends on table deletion.

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

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.

func (*Diff) TableDiff

func (d *Diff) TableDiff(from, to *schema.Table, options ...schema.DiffOption) ([]schema.Change, error)

TableDiff implements the schema.TableDiffer 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 {
	// RealmObjectDiff returns a changeset for migrating realm (database) objects
	// from one state to the other. For example, adding extensions or users.
	RealmObjectDiff(from, to *schema.Realm) ([]schema.Change, error)

	// 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.DiffOptions) ([]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.DiffOptions) ([]schema.Change, error)

	// ViewAttrChanges returns the changes between the two view attributes.
	ViewAttrChanges(from, to *schema.View) []schema.Change

	// ColumnChange returns the schema changes (if any) for migrating one column to the other.
	ColumnChange(fromT *schema.Table, from, to *schema.Column, _ *schema.DiffOptions) (schema.Change, 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

	// ForeignKeyAttrChanged reports if any of the foreign-key attributes were changed.
	ForeignKeyAttrChanged(from, to []schema.Attr) 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 DropSchemaChanger

type DropSchemaChanger interface {
	DropSchemaChange(*schema.Schema) []schema.Change
}

DropSchemaChanger is an optional interface allows DiffDriver to drop schema objects before dropping the schema itself.

type Normalizer

type Normalizer interface {
	Normalize(from, to *schema.Table, opts *schema.DiffOptions) error
}

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 ProcFuncsDiffer

type ProcFuncsDiffer interface {
	// ProcFuncsDiff returns a changeset for migrating functions and procedures
	// from one schema state to the other.
	ProcFuncsDiff(from, to *schema.Schema, opts *schema.DiffOptions) ([]schema.Change, error)
}

ProcFuncsDiffer is an optional interface allows DiffDriver to diff functions and procedures.

type RowTyper

type RowTyper interface {
	RowTypeT() *schema.Table
	RowTypeV() *schema.View
}

RowTyper can be implemented by a type to determine if its source is a regular table (e.g., row types).

type SortOptions

type SortOptions struct {
	// FuncDepT reports if a function depends on the given table.
	FuncDepT func(*schema.Func, *schema.Table) bool
	// FuncDepV reports if a function depends on the given view.
	FuncDepV func(*schema.Func, *schema.View) bool
	// FuncDepO reports if a function depends on the given object.
	FuncDepO func(*schema.Func, schema.Object) bool
	// ProcDepT reports if a procedure depends on the given table.
	ProcDepT func(*schema.Proc, *schema.Table) bool
	// ProcDepO reports if a procedure depends on the given object.
	ProcDepO func(*schema.Proc, schema.Object) bool
	// ViewDepT reports if a view depends on the given table.
	ViewDepT func(*schema.View, *schema.Table) bool
	// CompareFuncArgs set to true to compare function arguments.
	CompareFuncArgs bool
	// DefaultSchema defines the default schema (also known as "search_path") that
	// is used by the database to search for objects if no qualifier is provided.
	DefaultSchema string
}

SortOptions allows drivers to customize the behavior of the SortChanges function.

type TableFinder

type TableFinder interface {
	FindTable(*schema.Schema, *schema.Table) (*schema.Table, error)
}

TableFinder wraps the FindTable method, providing more control to the DiffDriver on how tables are matched.

type TriggerDiffer

type TriggerDiffer interface {
	// TriggerDiff returns a changeset for migrating triggers from
	// one state to the other. For example, changing action time.
	TriggerDiff(from, to *schema.Trigger) ([]schema.Change, error)
}

TriggerDiffer is an optional interface allows DiffDriver to diff triggers.

type UnsupportedTypeError

type UnsupportedTypeError struct {
	schema.Type
}

UnsupportedTypeError describes an unsupported type error.

func (UnsupportedTypeError) Error

func (e UnsupportedTypeError) Error() string

Jump to

Keyboard shortcuts

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