Documentation
¶
Overview ¶
Package schema is a Laravel-style schema builder: you describe tables with a Blueprint, and a Grammar turns that description into DDL for the target database. Application code never writes SQL.
Index ¶
- Constants
- Variables
- func ToAlterSQL(g Grammar, table string, fn func(t *Blueprint)) ([]string, error)
- func ToSQL(g Grammar, table string, fn func(t *Blueprint)) ([]string, error)
- type Blueprint
- 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) 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(names ...string)
- func (b *Blueprint) DropForeign(cols ...string)
- func (b *Blueprint) DropForeignNamed(name string)
- func (b *Blueprint) DropIndex(cols ...string)
- func (b *Blueprint) DropIndexNamed(name string)
- func (b *Blueprint) DropUnique(cols ...string)
- func (b *Blueprint) Enum(name string, allowed ...string) *Column
- func (b *Blueprint) Float(name string) *Column
- func (b *Blueprint) Foreign(cols ...string) *ForeignKey
- func (b *Blueprint) ForeignID(name string) *ForeignIDColumn
- func (b *Blueprint) ID(name ...string) *Column
- func (b *Blueprint) Increments(name string) *Column
- func (b *Blueprint) Index(cols ...string) *Index
- func (b *Blueprint) Integer(name string) *Column
- func (b *Blueprint) JSON(name string) *Column
- func (b *Blueprint) Primary(cols ...string)
- func (b *Blueprint) RememberToken() *Column
- func (b *Blueprint) RenameColumn(from, to string)
- 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) Text(name string) *Column
- func (b *Blueprint) Timestamp(name string) *Column
- func (b *Blueprint) Timestamps()
- func (b *Blueprint) UUID(name string) *Column
- func (b *Blueprint) Unique(cols ...string) *Index
- type Builder
- func (b *Builder) Context() context.Context
- func (b *Builder) Create(table string, fn func(t *Blueprint)) error
- func (b *Builder) Drop(table string) error
- func (b *Builder) DropAllTables() error
- func (b *Builder) DropIfExists(table string) error
- func (b *Builder) Grammar() Grammar
- func (b *Builder) HasColumn(table, column string) (bool, error)
- func (b *Builder) HasTable(table string) (bool, error)
- func (b *Builder) Rename(from, to string) error
- func (b *Builder) Table(table string, fn func(t *Blueprint)) error
- type Column
- func (c *Column) AutoIncrement() *Column
- func (c *Column) Comment(s string) *Column
- func (c *Column) Default(v any) *Column
- func (c *Column) Index() *Column
- func (c *Column) Name() string
- func (c *Column) Nullable(v ...bool) *Column
- func (c *Column) Primary() *Column
- func (c *Column) Unique() *Column
- func (c *Column) Unsigned() *Column
- func (c *Column) UseCurrent() *Column
- type Conn
- type ForeignIDColumn
- type ForeignKey
- func (f *ForeignKey) CascadeOnDelete() *ForeignKey
- func (f *ForeignKey) Named(name string) *ForeignKey
- func (f *ForeignKey) NullOnDelete() *ForeignKey
- func (f *ForeignKey) On(table string) *ForeignKey
- func (f *ForeignKey) OnDelete(action string) *ForeignKey
- func (f *ForeignKey) OnUpdate(action string) *ForeignKey
- func (f *ForeignKey) References(cols ...string) *ForeignKey
- func (f *ForeignKey) RestrictOnDelete() *ForeignKey
- type Grammar
- type Index
- type MySQL
- func (g MySQL) CompileAlter(bp *Blueprint) ([]string, error)
- func (g MySQL) CompileCreate(bp *Blueprint) ([]string, error)
- func (MySQL) CompileDrop(table string) string
- func (MySQL) CompileDropAll(tables []string) []string
- func (MySQL) CompileDropIfExists(table string) string
- func (MySQL) CompileHasColumn(table, column string) (string, []any)
- func (MySQL) CompileHasTable(table string) (string, []any)
- func (MySQL) CompileListTables() string
- func (MySQL) CompileRename(from, to string) string
- func (MySQL) Name() string
- type Postgres
- func (g Postgres) CompileAlter(bp *Blueprint) ([]string, error)
- func (g Postgres) CompileCreate(bp *Blueprint) ([]string, error)
- func (Postgres) CompileDrop(table string) string
- func (Postgres) CompileDropAll(tables []string) []string
- func (Postgres) CompileDropIfExists(table string) string
- func (Postgres) CompileHasColumn(table, column string) (string, []any)
- func (Postgres) CompileHasTable(table string) (string, []any)
- func (Postgres) CompileListTables() string
- func (Postgres) CompileRename(from, to string) string
- func (Postgres) Name() string
- type Raw
- type SQLite
- func (g SQLite) CompileAlter(bp *Blueprint) ([]string, error)
- func (g SQLite) CompileCreate(bp *Blueprint) ([]string, error)
- func (SQLite) CompileDrop(table string) string
- func (SQLite) CompileDropAll(tables []string) []string
- func (SQLite) CompileDropIfExists(table string) string
- func (SQLite) CompileHasColumn(table, column string) (string, []any)
- func (SQLite) CompileHasTable(table string) (string, []any)
- func (SQLite) CompileListTables() string
- func (SQLite) CompileRename(from, to string) string
- func (SQLite) Name() string
Constants ¶
const ( TypeBigInteger = "bigInteger" TypeInteger = "integer" TypeSmallInteger = "smallInteger" TypeString = "string" TypeText = "text" TypeBoolean = "boolean" TypeDecimal = "decimal" TypeFloat = "float" TypeDouble = "double" TypeDate = "date" TypeDateTime = "dateTime" TypeTimestamp = "timestamp" TypeJSON = "json" TypeUUID = "uuid" TypeBinary = "binary" TypeEnum = "enum" )
Column type names.
Variables ¶
var ErrUnsupported = errors.New("schema: operation not supported by this database")
ErrUnsupported is returned when the target database cannot perform an operation (for example dropping a foreign key on SQLite).
Functions ¶
func ToAlterSQL ¶
ToAlterSQL compiles a Table (alter) blueprint without executing it.
Types ¶
type Blueprint ¶
type Blueprint struct {
// contains filtered or unexported fields
}
Blueprint collects the definition of one table.
func (*Blueprint) BigIncrements ¶
BigIncrements adds an auto-incrementing BIGINT primary key.
func (*Blueprint) BigInteger ¶
BigInteger adds a BIGINT column.
func (*Blueprint) Decimal ¶
Decimal adds a DECIMAL column. Optional arguments: precision, scale (default 8, 2).
func (*Blueprint) DropColumn ¶
DropColumn drops columns.
func (*Blueprint) DropForeign ¶
DropForeign drops the foreign key created over cols.
func (*Blueprint) DropForeignNamed ¶
DropForeignNamed drops a foreign key by its exact name.
func (*Blueprint) DropIndexNamed ¶
DropIndexNamed drops an index by its exact name.
func (*Blueprint) DropUnique ¶
DropUnique drops the unique index created over cols.
func (*Blueprint) Foreign ¶
func (b *Blueprint) Foreign(cols ...string) *ForeignKey
Foreign starts a foreign-key definition on cols.
func (*Blueprint) ForeignID ¶
func (b *Blueprint) ForeignID(name string) *ForeignIDColumn
ForeignID adds a BIGINT UNSIGNED column intended as a foreign key.
t.ForeignID("user_id").Constrained().CascadeOnDelete()
func (*Blueprint) ID ¶
ID adds an auto-incrementing BIGINT UNSIGNED primary key (default name "id").
func (*Blueprint) Increments ¶
Increments adds an auto-incrementing INTEGER primary key.
func (*Blueprint) RememberToken ¶
RememberToken adds a nullable remember_token VARCHAR(100).
func (*Blueprint) RenameColumn ¶
RenameColumn renames a column.
func (*Blueprint) SmallInteger ¶
SmallInteger adds a SMALLINT column.
func (*Blueprint) SoftDeletes ¶
SoftDeletes adds a nullable deleted_at column.
func (*Blueprint) Timestamps ¶
func (b *Blueprint) Timestamps()
Timestamps adds nullable created_at and updated_at columns.
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is what migrations receive. It runs the DDL produced by a Grammar.
s.Create("users", func(t *schema.Blueprint) { t.ID(); t.String("email").Unique() })
func NewBuilder ¶
NewBuilder creates a Builder that executes on conn.
func NewPretendBuilder ¶
NewPretendBuilder creates a Builder that never touches a database: every statement it would run is passed to log instead. Used by "migrate --pretend".
func (*Builder) DropAllTables ¶
DropAllTables drops every table in the current database. Pass a *sql.Conn (not a pooled *sql.DB) so that session settings such as FOREIGN_KEY_CHECKS apply to all statements.
func (*Builder) DropIfExists ¶
DropIfExists drops a table if it exists.
func (*Builder) HasColumn ¶
HasColumn reports whether the column exists. In pretend mode it returns false.
type Column ¶
type Column struct {
// contains filtered or unexported fields
}
Column describes one column. Instances are created by Blueprint methods and refined with the fluent modifiers below.
func (*Column) AutoIncrement ¶
AutoIncrement makes an integer column auto-increment.
func (*Column) Default ¶
Default sets the default value. Accepts strings, numbers, bools, nil, time.Time and schema.Raw.
func (*Column) Nullable ¶
Nullable allows NULL. Columns are NOT NULL by default. Nullable(false) restores the default.
func (*Column) Unsigned ¶
Unsigned marks an integer column unsigned (MySQL only; ignored elsewhere).
func (*Column) UseCurrent ¶
UseCurrent makes the default the current timestamp.
type Conn ¶
type Conn interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}
Conn is the subset of *sql.DB, *sql.Tx and *sql.Conn that Builder needs.
type ForeignIDColumn ¶
type ForeignIDColumn struct {
*Column
// contains filtered or unexported fields
}
ForeignIDColumn is a BIGINT UNSIGNED column that can be turned into a foreign key in one call.
func (*ForeignIDColumn) Constrained ¶
func (c *ForeignIDColumn) Constrained(table ...string) *ForeignKey
Constrained adds a foreign key to table (default: the column name without "_id", plus "s", so user_id -> users) referencing its id column.
func (*ForeignIDColumn) Nullable ¶
func (c *ForeignIDColumn) Nullable(v ...bool) *ForeignIDColumn
Nullable allows NULL and keeps the chain on ForeignIDColumn.
type ForeignKey ¶
type ForeignKey struct {
// contains filtered or unexported fields
}
ForeignKey describes a foreign-key constraint.
func (*ForeignKey) CascadeOnDelete ¶
func (f *ForeignKey) CascadeOnDelete() *ForeignKey
CascadeOnDelete is shorthand for OnDelete("cascade").
func (*ForeignKey) Named ¶
func (f *ForeignKey) Named(name string) *ForeignKey
Named overrides the generated constraint name.
func (*ForeignKey) NullOnDelete ¶
func (f *ForeignKey) NullOnDelete() *ForeignKey
NullOnDelete is shorthand for OnDelete("set null").
func (*ForeignKey) On ¶
func (f *ForeignKey) On(table string) *ForeignKey
On sets the referenced table.
func (*ForeignKey) OnDelete ¶
func (f *ForeignKey) OnDelete(action string) *ForeignKey
OnDelete sets the ON DELETE action: cascade, restrict, set null, no action or set default.
func (*ForeignKey) OnUpdate ¶
func (f *ForeignKey) OnUpdate(action string) *ForeignKey
OnUpdate sets the ON UPDATE action (same values as OnDelete).
func (*ForeignKey) References ¶
func (f *ForeignKey) References(cols ...string) *ForeignKey
References sets the referenced columns (default: id).
func (*ForeignKey) RestrictOnDelete ¶
func (f *ForeignKey) RestrictOnDelete() *ForeignKey
RestrictOnDelete is shorthand for OnDelete("restrict").
type Grammar ¶
type Grammar interface {
// Name is the canonical driver name: postgres, mysql or sqlite.
Name() string
CompileCreate(bp *Blueprint) ([]string, error)
CompileAlter(bp *Blueprint) ([]string, error)
CompileDrop(table string) string
CompileDropIfExists(table string) string
CompileRename(from, to string) string
CompileHasTable(table string) (string, []any)
CompileHasColumn(table, column string) (string, []any)
CompileListTables() string
CompileDropAll(tables []string) []string
}
Grammar turns a Blueprint into DDL for one database. It is the only place in dbkit where SQL text is produced. Implement it to add a new database.
func GrammarFor ¶
GrammarFor returns the Grammar for a driver name. Aliases such as pgx, postgresql, mariadb and sqlite3 are accepted.
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index is a (non-primary) index or a primary-key declaration.
type MySQL ¶
type MySQL struct{}
MySQL compiles DDL for MySQL 8+ and MariaDB.
func (MySQL) CompileDrop ¶
func (MySQL) CompileDropAll ¶
func (MySQL) CompileDropIfExists ¶
func (MySQL) CompileListTables ¶
func (MySQL) CompileRename ¶
type Postgres ¶
type Postgres struct{}
Postgres compiles DDL for PostgreSQL.
func (Postgres) CompileDrop ¶
func (Postgres) CompileDropAll ¶
func (Postgres) CompileDropIfExists ¶
func (Postgres) CompileHasColumn ¶
func (Postgres) CompileListTables ¶
func (Postgres) CompileRename ¶
type Raw ¶
type Raw string
Raw is a value written into the DDL verbatim (for example a function call used as a column default). Use it sparingly and never with user input.
t.Timestamp("seen_at").Default(schema.Raw("CURRENT_TIMESTAMP"))
type SQLite ¶
type SQLite struct{}
SQLite compiles DDL for SQLite 3.35+.
SQLite cannot add or drop foreign keys or primary keys on an existing table; those operations return ErrUnsupported. Declare them in Create.