schema

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

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 represents a table definition with columns, indexes, foreign keys, and commands.

func NewBlueprint

func NewBlueprint(table string) *Blueprint

NewBlueprint creates a new Blueprint for the given table.

func (*Blueprint) BigInteger

func (bp *Blueprint) BigInteger(name string) *ColumnDefinition

BigInteger adds a BIGINT column.

func (*Blueprint) Binary

func (bp *Blueprint) Binary(name string) *ColumnDefinition

Binary adds a BINARY/BLOB column.

func (*Blueprint) Boolean

func (bp *Blueprint) Boolean(name string) *ColumnDefinition

Boolean adds a BOOLEAN column.

func (*Blueprint) Char

func (bp *Blueprint) Char(name string, length int) *ColumnDefinition

Char adds a fixed-length CHAR column with the given length.

func (*Blueprint) Columns

func (bp *Blueprint) Columns() []ColumnDefinition

Columns returns a copy of the column definitions.

func (*Blueprint) Commands

func (bp *Blueprint) Commands() []Command

Commands returns a copy of the alter commands.

func (*Blueprint) Date

func (bp *Blueprint) Date(name string) *ColumnDefinition

Date adds a DATE column.

func (*Blueprint) Decimal

func (bp *Blueprint) Decimal(name string, precision, scale int) *ColumnDefinition

Decimal adds a DECIMAL column with the given precision and scale.

func (*Blueprint) DropColumn

func (bp *Blueprint) DropColumn(name string)

DropColumn adds a command to drop a column.

func (*Blueprint) DropForeign

func (bp *Blueprint) DropForeign(name string)

DropForeign adds a command to drop a foreign key constraint.

func (*Blueprint) DropIndex

func (bp *Blueprint) DropIndex(name string)

DropIndex adds a command to drop an index.

func (*Blueprint) Enum

func (bp *Blueprint) Enum(name string, allowed []string) *ColumnDefinition

Enum adds an ENUM column with the given allowed values.

func (*Blueprint) Float

func (bp *Blueprint) Float(name string) *ColumnDefinition

Float adds a FLOAT column.

func (*Blueprint) Foreign

func (bp *Blueprint) Foreign(column string) *ForeignKeyDefinition

Foreign adds a foreign key constraint on the given column.

func (*Blueprint) ForeignKeys

func (bp *Blueprint) ForeignKeys() []ForeignKeyDefinition

ForeignKeys returns a copy of the foreign key definitions.

func (*Blueprint) FulltextIndex

func (bp *Blueprint) FulltextIndex(columns ...string) *IndexDefinition

FulltextIndex adds a fulltext index on the given columns.

func (*Blueprint) ID

func (bp *Blueprint) ID() *ColumnDefinition

ID adds an auto-incrementing big integer primary key column named "id".

func (*Blueprint) Index

func (bp *Blueprint) Index(columns ...string) *IndexDefinition

Index adds a composite index on the given columns.

func (*Blueprint) Indexes

func (bp *Blueprint) Indexes() []IndexDefinition

Indexes returns a copy of the index definitions.

func (*Blueprint) Integer

func (bp *Blueprint) Integer(name string) *ColumnDefinition

Integer adds an INTEGER column.

func (*Blueprint) JSON

func (bp *Blueprint) JSON(name string) *ColumnDefinition

JSON adds a JSON column.

func (*Blueprint) LongText

func (bp *Blueprint) LongText(name string) *ColumnDefinition

LongText adds a LONGTEXT column.

func (*Blueprint) MediumText

func (bp *Blueprint) MediumText(name string) *ColumnDefinition

MediumText adds a MEDIUMTEXT column.

func (*Blueprint) RenameColumn

func (bp *Blueprint) RenameColumn(from, to string)

RenameColumn adds a command to rename a column.

func (*Blueprint) SmallInt

func (bp *Blueprint) SmallInt(name string) *ColumnDefinition

SmallInt adds a SMALLINT column.

func (*Blueprint) SoftDeletes

func (bp *Blueprint) SoftDeletes()

SoftDeletes adds a deleted_at nullable timestamp column.

func (*Blueprint) SpatialIndex

func (bp *Blueprint) SpatialIndex(columns ...string) *IndexDefinition

SpatialIndex adds a spatial index on the given columns.

func (*Blueprint) String

func (bp *Blueprint) String(name string, length int) *ColumnDefinition

String adds a VARCHAR column with the given name and length.

func (*Blueprint) Table

func (bp *Blueprint) Table() string

Table returns the table name.

func (*Blueprint) Text

func (bp *Blueprint) Text(name string) *ColumnDefinition

Text adds a TEXT column.

func (*Blueprint) Timestamp

func (bp *Blueprint) Timestamp(name string) *ColumnDefinition

Timestamp adds a TIMESTAMP column.

func (*Blueprint) Timestamps

func (bp *Blueprint) Timestamps()

Timestamps adds created_at and updated_at nullable timestamp columns.

func (*Blueprint) TinyInt

func (bp *Blueprint) TinyInt(name string) *ColumnDefinition

TinyInt adds a TINYINT column.

func (*Blueprint) UUID

func (bp *Blueprint) UUID(name string) *ColumnDefinition

UUID adds a UUID column.

func (*Blueprint) UniqueIndex

func (bp *Blueprint) UniqueIndex(columns ...string) *IndexDefinition

UniqueIndex adds a unique composite index on the given columns.

type Builder

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

Builder provides a fluent API for defining database schema changes. It compiles Blueprint definitions through a Grammar and executes the resulting SQL against the database.

func NewBuilder

func NewBuilder(executor Executor, grammar Grammar) *Builder

NewBuilder creates a new Builder with the given executor and grammar. The executor can be a *sql.DB or *sql.Tx.

func (*Builder) Alter

func (b *Builder) Alter(table string, fn func(*Blueprint)) error

Alter modifies an existing table by building a Blueprint via the callback, compiling it through the Grammar, and executing each resulting SQL statement.

func (*Builder) Create

func (b *Builder) Create(table string, fn func(*Blueprint)) error

Create creates a new table by building a Blueprint via the callback, compiling it through the Grammar, and executing the resulting SQL.

func (*Builder) Drop

func (b *Builder) Drop(table string) error

Drop drops the given table.

func (*Builder) DropIfExists

func (b *Builder) DropIfExists(table string) error

DropIfExists drops the given table if it exists.

func (*Builder) HasColumn

func (b *Builder) HasColumn(table, column string) (bool, error)

HasColumn checks whether the given column exists in the given table.

func (*Builder) HasTable

func (b *Builder) HasTable(table string) (bool, error)

HasTable checks whether the given table exists in the database.

func (*Builder) RawExec

func (b *Builder) RawExec(statements ...string) error

RawExec executes one or more raw SQL statements directly against the database. Use this for operations not covered by the fluent API, such as creating materialized views, custom indexes, or database-specific DDL.

func (*Builder) Rename

func (b *Builder) Rename(from, to string) error

Rename renames a table from one name to another.

type ColumnDefinition

type ColumnDefinition struct {
	Name            string
	Type            ColumnType
	Length          int
	Precision       int
	Scale           int
	IsNullable      bool
	DefaultValue    any
	IsPrimary       bool
	IsUnique        bool
	IsUnsigned      bool
	IsAutoIncrement bool
	AllowedValues   []string
}

ColumnDefinition represents a column in a database table.

func (*ColumnDefinition) AutoIncrement

func (cd *ColumnDefinition) AutoIncrement() *ColumnDefinition

AutoIncrement marks the column as auto-incrementing.

func (*ColumnDefinition) Default

func (cd *ColumnDefinition) Default(value any) *ColumnDefinition

Default sets the default value for the column.

func (*ColumnDefinition) DefaultRaw

func (cd *ColumnDefinition) DefaultRaw(expression string) *ColumnDefinition

DefaultRaw sets the default value for the column to a raw SQL expression. If expression is empty, the default value is not changed.

func (*ColumnDefinition) Nullable

func (cd *ColumnDefinition) Nullable() *ColumnDefinition

Nullable marks the column as nullable.

func (*ColumnDefinition) Primary

func (cd *ColumnDefinition) Primary() *ColumnDefinition

Primary marks the column as a primary key.

func (*ColumnDefinition) Unique

func (cd *ColumnDefinition) Unique() *ColumnDefinition

Unique marks the column as unique.

func (*ColumnDefinition) Unsigned

func (cd *ColumnDefinition) Unsigned() *ColumnDefinition

Unsigned marks the column as unsigned.

type ColumnType

type ColumnType int

ColumnType represents the type of a database column.

const (
	TypeString     ColumnType = iota // VARCHAR
	TypeText                         // TEXT
	TypeInteger                      // INTEGER
	TypeBigInteger                   // BIGINT
	TypeBoolean                      // BOOLEAN
	TypeTimestamp                    // TIMESTAMP
	TypeDate                         // DATE
	TypeDecimal                      // DECIMAL
	TypeFloat                        // FLOAT
	TypeUUID                         // UUID
	TypeJSON                         // JSON
	TypeBinary                       // BINARY/BLOB
	TypeEnum                         // ENUM
	TypeChar                         // CHAR(n)
	TypeLongText                     // LONGTEXT
	TypeMediumText                   // MEDIUMTEXT
	TypeTinyInt                      // TINYINT
	TypeSmallInt                     // SMALLINT
)

func (ColumnType) String

func (ct ColumnType) String() string

String returns the string representation of a ColumnType.

type Command

type Command struct {
	Type CommandType
	Name string // column/index/foreign key name to drop, or "from" name for rename
	To   string // "to" name for rename
}

Command represents an alter-table command.

type CommandType

type CommandType int

CommandType represents the type of alter command.

const (
	CommandDropColumn CommandType = iota
	CommandRenameColumn
	CommandDropIndex
	CommandDropForeign
)

type DryRunExecutor

type DryRunExecutor struct {
	Writer io.Writer
}

DryRunExecutor implements Executor but writes SQL to a Writer instead of executing it against a database.

func (*DryRunExecutor) Exec

func (d *DryRunExecutor) Exec(query string, args ...any) (sql.Result, error)

func (*DryRunExecutor) QueryRow

func (d *DryRunExecutor) QueryRow(query string, args ...any) *sql.Row

type Executor

type Executor interface {
	Exec(query string, args ...any) (sql.Result, error)
	QueryRow(query string, args ...any) *sql.Row
}

Executor abstracts database execution so that both *sql.DB and *sql.Tx can be used interchangeably with the Builder.

type ForeignKeyDefinition

type ForeignKeyDefinition struct {
	Column    string
	RefTable  string
	RefColumn string
	OnDelete  string
	OnUpdate  string
	Name      string
}

ForeignKeyDefinition represents a foreign key constraint on a database table.

func (*ForeignKeyDefinition) On

On sets the referenced table.

func (*ForeignKeyDefinition) OnDeleteAction

func (fk *ForeignKeyDefinition) OnDeleteAction(action string) *ForeignKeyDefinition

OnDeleteAction sets the ON DELETE action.

func (*ForeignKeyDefinition) OnUpdateAction

func (fk *ForeignKeyDefinition) OnUpdateAction(action string) *ForeignKeyDefinition

OnUpdateAction sets the ON UPDATE action.

func (*ForeignKeyDefinition) References

func (fk *ForeignKeyDefinition) References(column string) *ForeignKeyDefinition

References sets the referenced column.

type Grammar

type Grammar interface {
	// CompileCreate generates a CREATE TABLE statement from the given Blueprint.
	CompileCreate(blueprint *Blueprint) (string, error)

	// CompileAlter generates ALTER TABLE statements from the given Blueprint.
	// Multiple statements may be returned (e.g., one per column add/drop).
	CompileAlter(blueprint *Blueprint) ([]string, error)

	// CompileDrop generates a DROP TABLE statement for the given table.
	CompileDrop(table string) string

	// CompileDropIfExists generates a DROP TABLE IF EXISTS statement for the given table.
	CompileDropIfExists(table string) string

	// CompileRename generates a RENAME TABLE statement.
	CompileRename(from, to string) string

	// CompileHasTable generates a query to check if a table exists.
	CompileHasTable(table string) string

	// CompileHasColumn generates a query to check if a column exists in a table.
	CompileHasColumn(table, column string) string

	// CompileDropAllTables generates a statement to drop all tables in the database.
	CompileDropAllTables() string

	// CompileColumnType returns the database-specific SQL type string for a column.
	CompileColumnType(col ColumnDefinition) (string, error)
}

Grammar defines the contract for compiling Blueprint definitions into database-specific SQL statements. Each supported database engine (PostgreSQL, MySQL, SQLite) provides its own Grammar implementation.

type IndexDefinition

type IndexDefinition struct {
	Name    string
	Columns []string
	Type    IndexType
}

IndexDefinition represents an index on a database table.

type IndexType

type IndexType int

IndexType represents the type of a database index.

const (
	IndexRegular  IndexType = iota // Regular (non-unique) index
	IndexUnique                    // Unique index
	IndexFulltext                  // Fulltext index
	IndexSpatial                   // Spatial index
)

type RawExpression

type RawExpression struct {
	Expression string
}

RawExpression membungkus string ekspresi SQL mentah. Ketika digunakan sebagai DefaultValue pada ColumnDefinition, Grammar akan menyisipkan ekspresi langsung tanpa tanda kutip.

func Raw

func Raw(expression string) RawExpression

Raw membuat RawExpression baru.

type RecordingExecutor

type RecordingExecutor struct {
	LastSQL string
	// contains filtered or unexported fields
}

RecordingExecutor wraps an inner Executor, recording the last SQL query executed. This is used by the Runner to capture the failing SQL statement when wrapping errors in MigrationError.

func NewRecordingExecutor

func NewRecordingExecutor(inner Executor) *RecordingExecutor

NewRecordingExecutor creates a RecordingExecutor wrapping the given executor.

func (*RecordingExecutor) Exec

func (r *RecordingExecutor) Exec(query string, args ...any) (sql.Result, error)

func (*RecordingExecutor) QueryRow

func (r *RecordingExecutor) QueryRow(query string, args ...any) *sql.Row

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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