Documentation
¶
Overview ¶
Package schema provides a fluent table-builder DSL (Blueprint) and a driver-agnostic Builder that compiles blueprints into SQL via a Grammar implementation provided by the database driver.
Use the package as follows:
schema.Create("users", func(t *schema.Blueprint) {
t.ID()
t.String("name")
t.String("email").Unique()
t.Timestamps()
})
schema.Create returns a *Definition that the migration engine compiles to SQL using the connection's Grammar.
Index ¶
- type Blueprint
- func (b *Blueprint) AddIndex(cols ...string) *Index
- func (b *Blueprint) BigIncrements(name string) *Column
- func (b *Blueprint) BigInteger(name string) *Column
- func (b *Blueprint) Binary(name string) *Column
- func (b *Blueprint) Boolean(name string) *Column
- func (b *Blueprint) Char(name string, length ...int) *Column
- func (b *Blueprint) Charset(name string) *Blueprint
- func (b *Blueprint) CharsetName() string
- func (b *Blueprint) Collation(name string) *Blueprint
- func (b *Blueprint) CollationName() string
- func (b *Blueprint) Columns() []*Column
- func (b *Blueprint) Commands() []*Command
- func (b *Blueprint) Comment(s string) *Blueprint
- func (b *Blueprint) CommentText() string
- func (b *Blueprint) Date(name string) *Column
- func (b *Blueprint) DateTime(name string) *Column
- func (b *Blueprint) Decimal(name string, precisionScale ...int) *Column
- func (b *Blueprint) Double(name string) *Column
- func (b *Blueprint) DropColumn(cols ...string)
- func (b *Blueprint) DropForeign(name string)
- func (b *Blueprint) DropIndex(name string)
- func (b *Blueprint) Engine(name string) *Blueprint
- func (b *Blueprint) EngineName() string
- func (b *Blueprint) Enum(name string, allowed ...string) *Column
- func (b *Blueprint) Float(name string) *Column
- func (b *Blueprint) Foreign(cols ...string) *Foreign
- func (b *Blueprint) ForeignId(name string) *ForeignId
- func (b *Blueprint) Foreigns() []*Foreign
- func (b *Blueprint) ID(name ...string) *Column
- func (b *Blueprint) Increments(name string) *Column
- func (b *Blueprint) Indexes() []*Index
- func (b *Blueprint) Integer(name string) *Column
- func (b *Blueprint) IsCreate() bool
- func (b *Blueprint) IsTemporary() bool
- func (b *Blueprint) JSON(name string) *Column
- func (b *Blueprint) JSONB(name string) *Column
- func (b *Blueprint) LongText(name string) *Column
- func (b *Blueprint) MediumText(name string) *Column
- func (b *Blueprint) Primary(cols ...string) *Index
- func (b *Blueprint) RenameColumn(from, to string)
- func (b *Blueprint) RenameTo(name string)
- func (b *Blueprint) Set(name string, allowed ...string) *Column
- func (b *Blueprint) SmallInteger(name string) *Column
- func (b *Blueprint) SoftDeletes() *Column
- func (b *Blueprint) String(name string, length ...int) *Column
- func (b *Blueprint) Table() string
- func (b *Blueprint) Temporary() *Blueprint
- func (b *Blueprint) Text(name string) *Column
- func (b *Blueprint) Time(name string) *Column
- func (b *Blueprint) Timestamp(name string) *Column
- func (b *Blueprint) TimestampTz(name string) *Column
- func (b *Blueprint) Timestamps()
- func (b *Blueprint) TimestampsTz()
- func (b *Blueprint) TinyInteger(name string) *Column
- func (b *Blueprint) UUID(name string) *Column
- func (b *Blueprint) Unique(cols ...string) *Index
- func (b *Blueprint) UnsignedBigInteger(name string) *Column
- func (b *Blueprint) UnsignedInteger(name string) *Column
- type Column
- func (c *Column) After(name string) *Column
- func (c *Column) Allow(values ...string) *Column
- func (c *Column) AutoIncrement() *Column
- func (c *Column) Comment(s string) *Column
- func (c *Column) Default(v any) *Column
- func (c *Column) Equal(o *Column) bool
- func (c *Column) First() *Column
- func (c *Column) Index() *Column
- func (c *Column) NotNull() *Column
- func (c *Column) Nullable() *Column
- func (c *Column) Primary() *Column
- func (c *Column) Unique() *Column
- func (c *Column) Unsigned() *Column
- func (c *Column) UseCurrentDefault() *Column
- type ColumnKind
- type Command
- type CommandType
- type Compiler
- type Definition
- func Create(table string, fn func(t *Blueprint)) *Definition
- func CreateIfNotExists(table string, fn func(t *Blueprint)) *Definition
- func Drop(table string) *Definition
- func DropIfExists(table string) *Definition
- func Rename(from, to string) *Definition
- func Table(table string, fn func(t *Blueprint)) *Definition
- type Foreign
- func (f *Foreign) CascadeOnDelete() *Foreign
- func (f *Foreign) NullOnDelete() *Foreign
- func (f *Foreign) On(table string) *Foreign
- func (f *Foreign) OnDelete(action string) *Foreign
- func (f *Foreign) OnUpdate(action string) *Foreign
- func (f *Foreign) References(cols ...string) *Foreign
- func (f *Foreign) RestrictOnDelete() *Foreign
- type ForeignId
- type Index
- type IndexType
- type Operation
- type Statement
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Blueprint ¶
type Blueprint struct {
// contains filtered or unexported fields
}
Blueprint is the fluent table builder. Migrations construct it via schema.Create / schema.Table; users never instantiate it directly. The methods mutate the blueprint and return Column / Foreign pointers for chaining (Eloquent-style).
func NewBlueprint ¶
NewBlueprint returns an empty blueprint targeting the given table.
func (*Blueprint) BigIncrements ¶
BigIncrements adds a big auto-increment primary key.
func (*Blueprint) BigInteger ¶
func (*Blueprint) CharsetName ¶
CharsetName returns the configured charset.
func (*Blueprint) CollationName ¶
CollationName returns the configured collation.
func (*Blueprint) CommentText ¶
CommentText returns the configured comment.
func (*Blueprint) DropColumn ¶
DropColumn schedules columns for removal.
func (*Blueprint) DropForeign ¶
DropForeign drops the named foreign key.
func (*Blueprint) EngineName ¶
EngineName returns the configured engine, if any.
func (*Blueprint) ForeignId ¶
ForeignId adds an UNSIGNED BIG INTEGER column intended to reference another table's primary key. Use the returned *ForeignId to chain Constrained()/ References()/OnDelete()/OnUpdate().
func (*Blueprint) Increments ¶
Increments adds a 32-bit auto-increment primary key.
func (*Blueprint) IsTemporary ¶
IsTemporary reports whether the blueprint is temporary.
func (*Blueprint) MediumText ¶
func (*Blueprint) RenameColumn ¶
RenameColumn renames a column.
func (*Blueprint) SmallInteger ¶
func (*Blueprint) SoftDeletes ¶
SoftDeletes adds a nullable deleted_at column conventionally used by the ORM's soft-delete behavior.
func (*Blueprint) TimestampTz ¶
func (*Blueprint) Timestamps ¶
func (b *Blueprint) Timestamps()
Timestamps adds nullable created_at and updated_at columns.
func (*Blueprint) TimestampsTz ¶
func (b *Blueprint) TimestampsTz()
TimestampsTz adds the same as Timestamps but with TZ.
func (*Blueprint) TinyInteger ¶
func (*Blueprint) UnsignedBigInteger ¶
func (*Blueprint) UnsignedInteger ¶
type Column ¶
type Column struct {
Name string
Kind ColumnKind
Length int
Precision int
Scale int
NullableV bool
HasDefault bool
DefaultV any
IsUnique bool
IsIndex bool
IsPrimary bool
UnsignedV bool
AutoInc bool
CommentV string
AfterV string
FirstV bool
Allowed []string // for ENUM/SET
Change bool // true when used inside Table() to alter
Renamed bool
OldName string // when renaming
Drop bool
UseCurrent bool
}
Column is the fluent builder returned by Blueprint methods. The fluent methods set modifiers and return the same pointer for chaining.
func (*Column) AutoIncrement ¶
AutoIncrement marks the column auto-incrementing.
func (*Column) UseCurrentDefault ¶
UseCurrent uses CURRENT_TIMESTAMP as the default (timestamp columns).
type ColumnKind ¶
type ColumnKind string
ColumnKind enumerates portable column kinds. Drivers translate kinds to dialect-specific SQL via database.Grammar.CompileType.
const ( KindBigIncrements ColumnKind = "bigIncrements" KindIncrements ColumnKind = "increments" KindBigInteger ColumnKind = "bigInteger" KindInteger ColumnKind = "integer" KindSmallInteger ColumnKind = "smallInteger" KindTinyInteger ColumnKind = "tinyInteger" KindUnsignedBig ColumnKind = "unsignedBigInteger" KindUnsignedInt ColumnKind = "unsignedInteger" KindFloat ColumnKind = "float" KindDouble ColumnKind = "double" KindDecimal ColumnKind = "decimal" KindBoolean ColumnKind = "boolean" KindString ColumnKind = "string" KindChar ColumnKind = "char" KindText ColumnKind = "text" KindMediumText ColumnKind = "mediumText" KindLongText ColumnKind = "longText" KindBinary ColumnKind = "binary" KindUUID ColumnKind = "uuid" KindJSON ColumnKind = "json" KindJSONB ColumnKind = "jsonb" KindDate ColumnKind = "date" KindDateTime ColumnKind = "dateTime" KindTime ColumnKind = "time" KindTimestamp ColumnKind = "timestamp" KindTimestampTZ ColumnKind = "timestampTz" KindEnum ColumnKind = "enum" KindSet ColumnKind = "set" KindIPAddress ColumnKind = "ipAddress" KindMACAddress ColumnKind = "macAddress" )
type Command ¶
type Command struct {
Type CommandType
Args []string
}
Command is a deferred ALTER operation.
type CommandType ¶
type CommandType string
CommandType enumerates deferred blueprint commands.
const ( CmdDropColumn CommandType = "dropColumn" CmdRenameColumn CommandType = "renameColumn" CmdDropIndex CommandType = "dropIndex" CmdDropUnique CommandType = "dropUnique" CmdDropForeign CommandType = "dropForeign" CmdDropPrimary CommandType = "dropPrimary" CmdRenameTable CommandType = "renameTable" )
type Compiler ¶
type Compiler struct {
// contains filtered or unexported fields
}
Compiler compiles a blueprint into one or more Statements. It is a thin orchestrator on top of database.Grammar — the grammar handles type rendering and identifier quoting; the compiler walks the blueprint and assembles DDL.
func NewCompiler ¶
NewCompiler builds a compiler bound to the given grammar.
type Definition ¶
Definition pairs a blueprint with the kind of statement it represents.
func Create ¶
func Create(table string, fn func(t *Blueprint)) *Definition
Create builds a CREATE TABLE definition.
func CreateIfNotExists ¶
func CreateIfNotExists(table string, fn func(t *Blueprint)) *Definition
CreateIfNotExists is the IF NOT EXISTS variant.
func DropIfExists ¶
func DropIfExists(table string) *Definition
DropIfExists schedules DROP TABLE IF EXISTS.
func Table ¶
func Table(table string, fn func(t *Blueprint)) *Definition
Table builds an ALTER TABLE definition.
type Foreign ¶
type Foreign struct {
Name string
Columns []string
ReferencesTable string
ReferencesCols []string
OnDeleteAction string
OnUpdateAction string
Drop bool
}
Foreign describes a foreign-key constraint.
func (*Foreign) CascadeOnDelete ¶
CascadeOnDelete is sugar for OnDelete("cascade").
func (*Foreign) NullOnDelete ¶
NullOnDelete is sugar for OnDelete("set null").
func (*Foreign) OnDelete ¶
OnDelete sets ON DELETE behavior ("cascade", "restrict", "set null", "no action").
func (*Foreign) References ¶
References sets the referenced columns.
func (*Foreign) RestrictOnDelete ¶
RestrictOnDelete is sugar for OnDelete("restrict").
type ForeignId ¶
type ForeignId struct {
*Column
// contains filtered or unexported fields
}
ForeignId is the fluent chain used in Laravel-style declarations:
table.ForeignId("user_id").Constrained("users").OnDelete("cascade")
func (*ForeignId) Constrained ¶
Constrained creates a FK on this column referencing the given table's "id". If table is empty it is inferred from the column name ("user_id" → "users").
func (*ForeignId) References ¶
References starts a FK targeting the named referenced columns. Pair with .On().
type Index ¶
type Index struct {
Name string
Type IndexType
Columns []string
Algo string // optional: btree/hash/gin/gist (Postgres)
Drop bool
}
Index describes a table index.