Schema Builder
Complete package reference for the Schema Builder, Blueprint, and Grammar — all public methods with signatures, parameters, return types, and usage examples.
Schema Builder API
The Schema Builder provides a fluent API for creating, altering, and inspecting database tables. It consists of three main components: the Builder, the Blueprint, and the Grammar.
Import path: github.com/gopackx/go-migration/schema
For conceptual documentation, see Schema Builder.
Builder Methods
The *schema.Builder is passed to your migration's Up and Down methods. It provides table-level operations.
s.Create()
Creates a new table with columns defined in the Blueprint callback.
func (s *Builder) Create(table string, callback func(bp *Blueprint))| Parameter | Type | Description |
|---|---|---|
table | string | Name of the table to create |
callback | func(bp *Blueprint) | Function that defines columns, indexes, and constraints |
s.Create("users", func(bp *schema.Blueprint) {
bp.ID("id")
bp.String("name", 255)
bp.String("email", 255).Unique()
bp.Timestamp("created_at").Nullable()
})See Creating Tables.
s.Alter()
Modifies an existing table — add columns, drop columns, or add constraints.
func (s *Builder) Alter(table string, callback func(bp *Blueprint))| Parameter | Type | Description |
|---|---|---|
table | string | Name of the table to alter |
callback | func(bp *Blueprint) | Function that defines modifications |
s.Alter("users", func(bp *schema.Blueprint) {
bp.String("phone", 20).Nullable()
})See Altering Tables.
s.Drop()
Drops a table. Errors if the table does not exist.
func (s *Builder) Drop(table string)| Parameter | Type | Description |
|---|---|---|
table | string | Name of the table to drop |
s.Drop("posts")s.DropIfExists()
Drops a table only if it exists. Safe to use in Down methods.
func (s *Builder) DropIfExists(table string)s.DropIfExists("posts")s.Rename()
Renames an existing table.
func (s *Builder) Rename(from string, to string)| Parameter | Type | Description |
|---|---|---|
from | string | Current table name |
to | string | New table name |
s.Rename("posts", "articles")s.HasTable()
Checks if a table exists in the database.
func (s *Builder) HasTable(table string) boolReturns: bool
if s.HasTable("users") {
// Table exists
}s.HasColumn()
Checks if a column exists in a table.
func (s *Builder) HasColumn(table string, column string) bool| Parameter | Type | Description |
|---|---|---|
table | string | Table name |
column | string | Column name |
Returns: bool
if !s.HasColumn("users", "phone") {
s.Alter("users", func(bp *schema.Blueprint) {
bp.String("phone", 20).Nullable()
})
}See Utility Operations for more examples.
s.RawExec()
Executes one or more raw SQL statements directly, bypassing the fluent API. Useful for materialized views, custom DDL, extensions, triggers, or anything not covered by the Builder.
func (s *Builder) RawExec(statements ...string)| Parameter | Type | Description |
|---|---|---|
statements | ...string | One or more raw SQL statements to execute sequentially |
s.RawExec(
"CREATE EXTENSION IF NOT EXISTS pgcrypto",
`CREATE MATERIALIZED VIEW monthly_sales AS
SELECT date_trunc('month', created_at) AS month, SUM(amount) AS total
FROM orders GROUP BY month`,
)See Utility Operations for more examples.
Blueprint Methods — Column Types
The *schema.Blueprint is received inside Create and Alter callbacks. Column methods return a column definition that supports chaining with modifiers.
| Method | Signature | Description |
|---|---|---|
ID | bp.ID(name string) | Auto-incrementing unsigned big integer primary key |
String | bp.String(name string, length int) | Variable-length string (VARCHAR) |
Text | bp.Text(name string) | Unlimited-length text |
Integer | bp.Integer(name string) | 32-bit integer |
BigInteger | bp.BigInteger(name string) | 64-bit integer |
Boolean | bp.Boolean(name string) | Boolean true/false |
Timestamp | bp.Timestamp(name string) | Timestamp with date and time |
Date | bp.Date(name string) | Date without time |
Decimal | bp.Decimal(name string, precision int, scale int) | Fixed-point decimal |
Float | bp.Float(name string) | Floating-point number |
UUID | bp.UUID(name string) | UUID string |
JSON | bp.JSON(name string) | JSON data |
Binary | bp.Binary(name string) | Binary/blob data |
s.Create("products", func(bp *schema.Blueprint) {
bp.ID("id")
bp.String("name", 255)
bp.Decimal("price", 10, 2)
bp.Integer("stock").Default(0)
bp.Boolean("active").Default(true)
bp.JSON("metadata").Nullable()
bp.Timestamp("created_at").Nullable()
})See Column Types for detailed usage of each type.
Blueprint Methods — Column Modifiers
Modifiers are chainable methods called on the column definition returned by any column type method.
| Modifier | Signature | Description |
|---|---|---|
Nullable | .Nullable() | Allow NULL values |
Default | .Default(value interface{}) | Set a default value |
Primary | .Primary() | Set as primary key |
Unique | .Unique() | Add a unique constraint |
Unsigned | .Unsigned() | Make numeric column unsigned |
AutoIncrement | .AutoIncrement() | Enable auto-increment |
bp.String("email", 255).Unique()
bp.Integer("age").Unsigned().Nullable()
bp.Boolean("active").Default(true)
bp.BigInteger("id").AutoIncrement().Primary()See Column Modifiers.
Blueprint Methods — Drop Column
bp.DropColumn()
Removes a column from the table (used inside Alter callbacks).
func (bp *Blueprint) DropColumn(name string)s.Alter("users", func(bp *schema.Blueprint) {
bp.DropColumn("phone")
})Blueprint Methods — Indexes
bp.Index()
Creates an index on one or more columns.
func (bp *Blueprint) Index(name string, columns ...string)| Parameter | Type | Description |
|---|---|---|
name | string | Index name |
columns | ...string | One or more column names |
// Single-column index
bp.Index("idx_users_email", "email")
// Composite index
bp.Index("idx_posts_user_status", "user_id", "status")bp.UniqueIndex()
Creates a unique index on one or more columns.
func (bp *Blueprint) UniqueIndex(name string, columns ...string)bp.UniqueIndex("idx_users_email_unique", "email")
bp.UniqueIndex("idx_subscriptions_user_plan", "user_id", "plan_id")See Indexes.
Blueprint Methods — Foreign Keys
bp.Foreign()
Starts a foreign key constraint definition. Returns a chainable builder.
func (bp *Blueprint) Foreign(column string) *ForeignKeyBuilderChain methods:
| Method | Signature | Description |
|---|---|---|
References | .References(column string) | The referenced column |
On | .On(table string) | The referenced table |
OnDelete | .OnDelete(action string) | Action on parent deletion |
OnDelete actions: "CASCADE", "SET NULL", "RESTRICT", "NO ACTION"
bp.Foreign("user_id").References("id").On("users").OnDelete("CASCADE")
bp.Foreign("category_id").References("id").On("categories").OnDelete("SET NULL")See Foreign Keys.
Grammar Interface
Grammars translate Blueprint definitions into database-specific SQL. Three built-in grammars are available.
Import path: github.com/gopackx/go-migration/grammar
| Function | Description |
|---|---|
grammar.PostgreSQL() | PostgreSQL grammar (default) |
grammar.MySQL() | MySQL grammar |
grammar.SQLite() | SQLite grammar |
Pass a grammar to the migrator using migrator.WithGrammar():
import (
"github.com/gopackx/go-migration/grammar"
"github.com/gopackx/go-migration/migrator"
)
// MySQL
m := migrator.New(db, migrator.WithGrammar(grammar.MySQL()))
// SQLite
m := migrator.New(db, migrator.WithGrammar(grammar.SQLite()))See Database Grammars for type mapping details across all three grammars.
Quick Reference — Builder
| Method | Signature | Description |
|---|---|---|
Create | Create(table, callback) | Create a new table |
Alter | Alter(table, callback) | Modify an existing table |
Drop | Drop(table) | Drop a table |
DropIfExists | DropIfExists(table) | Drop a table if it exists |
Rename | Rename(from, to) | Rename a table |
HasTable | HasTable(table) bool | Check if a table exists |
HasColumn | HasColumn(table, column) bool | Check if a column exists |
RawExec | RawExec(statements...) | Execute raw SQL statements directly |
Quick Reference — Blueprint
| Method | Description |
|---|---|
ID, String, Text, Integer, BigInteger | Column types |
Boolean, Timestamp, Date, Decimal, Float | Column types |
UUID, JSON, Binary | Column types |
.Nullable(), .Default(), .Primary() | Column modifiers |
.Unique(), .Unsigned(), .AutoIncrement() | Column modifiers |
DropColumn(name) | Remove a column |
Index(name, columns...) | Create an index |
UniqueIndex(name, columns...) | Create a unique index |
Foreign(column) | Start a foreign key definition |
Migrator
Complete package reference for the go-migration Migrator — all public methods with signatures, parameters, return types, and usage examples.
Seeder
Complete package reference for the go-migration Seeder interface, Runner, and dependency system — all public methods with signatures, parameters, return types, and usage examples.