schema

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package schema is a Knex-style DDL builder compiled per dialect.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlterSQL

func AlterSQL(d dialect.Dialect, name string, fn func(*Table)) ([]string, error)

AlterSQL compiles ALTER TABLE statements without executing.

func CreateSQL

func CreateSQL(d dialect.Dialect, name string, fn func(*Table)) ([]string, error)

CreateSQL compiles CREATE TABLE (+ index statements) without executing.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder executes DDL against a migro.DB (or a transaction-bound one).

func New

func New(db *migro.DB) *Builder

func (*Builder) AlterTable

func (b *Builder) AlterTable(ctx context.Context, name string, fn func(*Table)) error

func (*Builder) CreateTable

func (b *Builder) CreateTable(ctx context.Context, name string, fn func(*Table)) error

func (*Builder) CreateTableIfNotExists

func (b *Builder) CreateTableIfNotExists(ctx context.Context, name string, fn func(*Table)) error

CreateTableIfNotExists skips creation when the table exists.

func (*Builder) DB

func (b *Builder) DB() *migro.DB

DB returns the underlying handle, e.g. for raw statements inside a migration.

func (*Builder) DropTable

func (b *Builder) DropTable(ctx context.Context, name string) error

func (*Builder) DropTableIfExists

func (b *Builder) DropTableIfExists(ctx context.Context, name string) error

DropTableIfExists checks first because Oracle and SQL Server lack portable IF EXISTS.

func (*Builder) HasColumn

func (b *Builder) HasColumn(ctx context.Context, table, column string) (bool, error)

func (*Builder) HasTable

func (b *Builder) HasTable(ctx context.Context, name string) (bool, error)

func (*Builder) Raw

func (b *Builder) Raw(ctx context.Context, sql string, args ...any) error

Raw runs an arbitrary statement.

func (*Builder) RenameTable

func (b *Builder) RenameTable(ctx context.Context, from, to string) error

type Column

type Column struct {
	// contains filtered or unexported fields
}

Column is a single column definition. Chain modifiers on it.

func (*Column) Constrained

func (c *Column) Constrained(table ...string) *Foreign

Constrained adds a foreign key to table(id). With no argument the table is inferred from the column name (user_id -> users), like Laravel. It returns the Foreign for .OnDelete()/.OnUpdate().

func (*Column) Default

func (c *Column) Default(v any) *Column

Default sets a literal default (string, ints, floats, bool, nil).

func (*Column) DefaultNow

func (c *Column) DefaultNow() *Column

DefaultNow defaults to CURRENT_TIMESTAMP.

func (*Column) DefaultRaw

func (c *Column) DefaultRaw(expr string) *Column

DefaultRaw sets a raw SQL default expression. The caller must trust it.

func (*Column) Index

func (c *Column) Index() *Column

Index adds an index on this column.

func (*Column) NotNull

func (c *Column) NotNull() *Column

func (*Column) Nullable

func (c *Column) Nullable() *Column

func (*Column) Primary

func (c *Column) Primary() *Column

Primary marks this single column as the primary key.

func (*Column) References

func (c *Column) References(table, column string) *Foreign

References adds a foreign key from this column to table(column) and returns it for chaining: t.Integer("user_id").Unsigned().References("users", "id").OnDelete("CASCADE").

func (*Column) Unique

func (c *Column) Unique() *Column

Unique adds a unique constraint on this column.

func (*Column) Unsigned

func (c *Column) Unsigned() *Column

type Foreign

type Foreign struct {
	// contains filtered or unexported fields
}

Foreign is a foreign key definition.

func (*Foreign) Named

func (f *Foreign) Named(name string) *Foreign

Named overrides the generated constraint name.

func (*Foreign) OnDelete

func (f *Foreign) OnDelete(action string) *Foreign

OnDelete sets CASCADE, SET NULL, RESTRICT, NO ACTION or SET DEFAULT.

func (*Foreign) OnUpdate

func (f *Foreign) OnUpdate(action string) *Foreign

OnUpdate sets CASCADE, SET NULL, RESTRICT, NO ACTION or SET DEFAULT.

func (*Foreign) References

func (f *Foreign) References(table string, cols ...string) *Foreign

References sets the referenced table and columns.

type Index

type Index struct {
	// contains filtered or unexported fields
}

Index is a (possibly unique) index definition.

func (*Index) Named

func (i *Index) Named(name string) *Index

Named overrides the generated index name.

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table collects column and constraint definitions for CreateTable / AlterTable.

func (*Table) BigIncrements

func (t *Table) BigIncrements(name string) *Column

func (*Table) BigInteger

func (t *Table) BigInteger(name string) *Column

func (*Table) Binary

func (t *Table) Binary(name string) *Column

func (*Table) Boolean

func (t *Table) Boolean(name string) *Column

func (*Table) Date

func (t *Table) Date(name string) *Column

func (*Table) DateTime

func (t *Table) DateTime(name string) *Column

func (*Table) Decimal

func (t *Table) Decimal(name string, precision, scale int) *Column

Decimal defaults to (8,2) when precision is 0.

func (*Table) Double

func (t *Table) Double(name string) *Column

func (*Table) DropColumn

func (t *Table) DropColumn(names ...string)

AlterTable-only operations.

func (*Table) DropForeign

func (t *Table) DropForeign(name string)

func (*Table) DropIndex

func (t *Table) DropIndex(name string)

func (*Table) DropUnique

func (t *Table) DropUnique(name string)

func (*Table) Enum

func (t *Table) Enum(name string, values ...string) *Column

Enum is native on MySQL/MariaDB and varchar+CHECK elsewhere.

func (*Table) Float

func (t *Table) Float(name string) *Column

func (*Table) Foreign

func (t *Table) Foreign(cols ...string) *Foreign

func (*Table) ForeignID

func (t *Table) ForeignID(name string) *Column

ForeignID adds an unsigned, NOT NULL bigint meant to reference a BigIncrements key (Laravel's foreignId). Chain .Constrained() to add the foreign key or .Nullable() to allow NULL.

func (*Table) FullText

func (t *Table) FullText(cols ...string) *Index

FullText adds a full-text index over cols, for Query.WhereFullText. MySQL gets a FULLTEXT index and PostgreSQL a GIN index on to_tsvector. SQLite, SQL Server and Oracle fail with UNSUPPORTED_FEATURE: they need a virtual table or a catalog that a column list cannot describe.

func (*Table) Increments

func (t *Table) Increments(name string) *Column

func (*Table) Index

func (t *Table) Index(cols ...string) *Index

func (*Table) Integer

func (t *Table) Integer(name string) *Column

func (*Table) JSON

func (t *Table) JSON(name string) *Column

func (*Table) Morphs

func (t *Table) Morphs(name string)

Morphs adds <name>_type and <name>_id (NOT NULL) plus a composite index, for polymorphic relations.

func (*Table) NullableMorphs

func (t *Table) NullableMorphs(name string)

NullableMorphs is Morphs with nullable columns.

func (*Table) Primary

func (t *Table) Primary(cols ...string)

Primary sets a (composite) primary key.

func (*Table) RenameColumn

func (t *Table) RenameColumn(from, to string)

func (*Table) SmallInteger

func (t *Table) SmallInteger(name string) *Column

func (*Table) SoftDeletes

func (t *Table) SoftDeletes()

SoftDeletes adds an indexed, nullable deleted_at (matches gorm.DeletedAt).

func (*Table) String

func (t *Table) String(name string, length ...int) *Column

String is a varchar; length defaults to 255.

func (*Table) Text

func (t *Table) Text(name string) *Column

func (*Table) Time

func (t *Table) Time(name string) *Column

func (*Table) Timestamp

func (t *Table) Timestamp(name string) *Column

func (*Table) Timestamps

func (t *Table) Timestamps()

Timestamps adds nullable created_at and updated_at.

func (*Table) UUID

func (t *Table) UUID(name string) *Column

func (*Table) Unique

func (t *Table) Unique(cols ...string) *Index

Jump to

Keyboard shortcuts

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