go-migrationgo-migration
Database Grammars
Documentation

Database Grammars

Learn how go-migration generates SQL for PostgreSQL, MySQL, and SQLite using database grammars.

Database Grammars

go-migration uses grammars to translate its fluent schema builder API into database-specific SQL. Each grammar knows how to map column types, modifiers, and operations to the correct syntax for its target database engine.

Three grammars are supported out of the box:

  • PostgreSQL (default)
  • MySQL
  • SQLite

Selecting a Grammar

When using migrator.Run(), the grammar is auto-resolved from the driver field in your go-migration.yaml config file. No manual grammar selection is needed:

DriverGrammar
postgresPostgreSQL
mysqlMySQL
sqliteSQLite

For manual setup, pass a grammar to migrator.New() using the migrator.WithGrammar() option. If you don't specify one, PostgreSQL is used by default.

PostgreSQL is the default grammar — no extra configuration needed.

main.go
package main

import (
    "database/sql"
    "log"

    _ "github.com/lib/pq"

    "github.com/gopackx/go-migration/migrator"
)

func main() {
    db, err := sql.Open("postgres", "postgres://user:password@localhost:5432/mydb?sslmode=disable")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    // PostgreSQL grammar is used by default
    m := migrator.New(db)

    // Register migrations and run...
}

Use grammar.MySQL() to generate MySQL-compatible SQL.

main.go
package main

import (
    "database/sql"
    "log"

    _ "github.com/go-sql-driver/mysql"

    "github.com/gopackx/go-migration/grammar"
    "github.com/gopackx/go-migration/migrator"
)

func main() {
    db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/mydb")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    m := migrator.New(db, migrator.WithGrammar(grammar.MySQL()))

    // Register migrations and run...
}

Use grammar.SQLite() to generate SQLite-compatible SQL.

main.go
package main

import (
    "database/sql"
    "log"

    _ "github.com/mattn/go-sqlite3"

    "github.com/gopackx/go-migration/grammar"
    "github.com/gopackx/go-migration/migrator"
)

func main() {
    db, err := sql.Open("sqlite3", "./app.db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    m := migrator.New(db, migrator.WithGrammar(grammar.SQLite()))

    // Register migrations and run...
}

The grammar only affects SQL generation. Your migration code and schema builder calls stay exactly the same regardless of which database you target.

Type Mapping Table

The table below shows how each go-migration column type maps to native SQL types across all three grammars.

go-migration MethodPostgreSQLMySQLSQLite
bp.ID(name)BIGSERIAL PRIMARY KEYBIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEYINTEGER PRIMARY KEY AUTOINCREMENT
bp.String(name, n)VARCHAR(n)VARCHAR(n)VARCHAR(n)
bp.Text(name)TEXTTEXTTEXT
bp.Integer(name)INTEGERINTINTEGER
bp.BigInteger(name)BIGINTBIGINTINTEGER
bp.Boolean(name)BOOLEANTINYINT(1)INTEGER
bp.Timestamp(name)TIMESTAMPTIMESTAMPDATETIME
bp.Date(name)DATEDATEDATE
bp.Decimal(name, p, s)DECIMAL(p,s)DECIMAL(p,s)DECIMAL(p,s)
bp.Float(name)DOUBLE PRECISIONDOUBLEREAL
bp.UUID(name)UUIDCHAR(36)VARCHAR(36)
bp.JSON(name)JSONBJSONTEXT
bp.Binary(name)BYTEABLOBBLOB

SQLite has a limited type system. Types like BOOLEAN and BigInteger map to INTEGER, and JSON is stored as TEXT. Your application code should handle any necessary type conversions.

Key Differences Between Grammars

PostgreSQL

  • Uses BIGSERIAL for auto-incrementing IDs
  • Has native UUID and JSONB types
  • Uses BYTEA for binary data
  • BOOLEAN is a true boolean type

MySQL

  • Uses BIGINT UNSIGNED AUTO_INCREMENT for IDs
  • UUIDs are stored as CHAR(36)
  • Has native JSON type (MySQL 5.7+)
  • Booleans use TINYINT(1) (0 or 1)

SQLite

  • Uses INTEGER PRIMARY KEY AUTOINCREMENT for IDs
  • Most types map to SQLite's core affinities (INTEGER, TEXT, REAL, BLOB)
  • No native UUID or JSON types — stored as text
  • No native boolean — stored as integer (0 or 1)

What's Next?

  • Seeders — populate your database with test data
  • Column Types — full reference of all schema builder column methods
  • Connections — manage multiple database connections