Documentation
¶
Index ¶
- type Blueprint
- func (bp *Blueprint) BigInteger(name string) *ColumnDefinition
- func (bp *Blueprint) Binary(name string) *ColumnDefinition
- func (bp *Blueprint) Boolean(name string) *ColumnDefinition
- func (bp *Blueprint) Char(name string, length int) *ColumnDefinition
- func (bp *Blueprint) Columns() []ColumnDefinition
- func (bp *Blueprint) Commands() []Command
- func (bp *Blueprint) Date(name string) *ColumnDefinition
- func (bp *Blueprint) Decimal(name string, precision, scale int) *ColumnDefinition
- func (bp *Blueprint) DropColumn(name string)
- func (bp *Blueprint) DropForeign(name string)
- func (bp *Blueprint) DropIndex(name string)
- func (bp *Blueprint) Enum(name string, allowed []string) *ColumnDefinition
- func (bp *Blueprint) Float(name string) *ColumnDefinition
- func (bp *Blueprint) Foreign(column string) *ForeignKeyDefinition
- func (bp *Blueprint) ForeignKeys() []ForeignKeyDefinition
- func (bp *Blueprint) FulltextIndex(columns ...string) *IndexDefinition
- func (bp *Blueprint) ID() *ColumnDefinition
- func (bp *Blueprint) Index(columns ...string) *IndexDefinition
- func (bp *Blueprint) Indexes() []IndexDefinition
- func (bp *Blueprint) Integer(name string) *ColumnDefinition
- func (bp *Blueprint) JSON(name string) *ColumnDefinition
- func (bp *Blueprint) LongText(name string) *ColumnDefinition
- func (bp *Blueprint) MediumText(name string) *ColumnDefinition
- func (bp *Blueprint) RenameColumn(from, to string)
- func (bp *Blueprint) SmallInt(name string) *ColumnDefinition
- func (bp *Blueprint) SoftDeletes()
- func (bp *Blueprint) SpatialIndex(columns ...string) *IndexDefinition
- func (bp *Blueprint) String(name string, length int) *ColumnDefinition
- func (bp *Blueprint) Table() string
- func (bp *Blueprint) Text(name string) *ColumnDefinition
- func (bp *Blueprint) Timestamp(name string) *ColumnDefinition
- func (bp *Blueprint) Timestamps()
- func (bp *Blueprint) TinyInt(name string) *ColumnDefinition
- func (bp *Blueprint) UUID(name string) *ColumnDefinition
- func (bp *Blueprint) UniqueIndex(columns ...string) *IndexDefinition
- type Builder
- func (b *Builder) Alter(table string, fn func(*Blueprint)) error
- func (b *Builder) Create(table string, fn func(*Blueprint)) error
- func (b *Builder) Drop(table string) error
- func (b *Builder) DropIfExists(table string) error
- func (b *Builder) HasColumn(table, column string) (bool, error)
- func (b *Builder) HasTable(table string) (bool, error)
- func (b *Builder) RawExec(statements ...string) error
- func (b *Builder) Rename(from, to string) error
- type ColumnDefinition
- func (cd *ColumnDefinition) AutoIncrement() *ColumnDefinition
- func (cd *ColumnDefinition) Default(value any) *ColumnDefinition
- func (cd *ColumnDefinition) DefaultRaw(expression string) *ColumnDefinition
- func (cd *ColumnDefinition) Nullable() *ColumnDefinition
- func (cd *ColumnDefinition) Primary() *ColumnDefinition
- func (cd *ColumnDefinition) Unique() *ColumnDefinition
- func (cd *ColumnDefinition) Unsigned() *ColumnDefinition
- type ColumnType
- type Command
- type CommandType
- type DryRunExecutor
- type Executor
- type ForeignKeyDefinition
- func (fk *ForeignKeyDefinition) On(table string) *ForeignKeyDefinition
- func (fk *ForeignKeyDefinition) OnDeleteAction(action string) *ForeignKeyDefinition
- func (fk *ForeignKeyDefinition) OnUpdateAction(action string) *ForeignKeyDefinition
- func (fk *ForeignKeyDefinition) References(column string) *ForeignKeyDefinition
- type Grammar
- type IndexDefinition
- type IndexType
- type RawExpression
- type RecordingExecutor
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 ¶
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) 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 ¶
DropColumn adds a command to drop a column.
func (*Blueprint) DropForeign ¶
DropForeign adds a command to drop a foreign key constraint.
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 ¶
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) 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 ¶
NewBuilder creates a new Builder with the given executor and grammar. The executor can be a *sql.DB or *sql.Tx.
func (*Builder) Alter ¶
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 ¶
Create creates a new table by building a Blueprint via the callback, compiling it through the Grammar, and executing the resulting SQL.
func (*Builder) DropIfExists ¶
DropIfExists drops the given table if it exists.
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 ¶
DryRunExecutor implements Executor but writes SQL to a Writer instead of executing it against a database.
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 ¶
func (fk *ForeignKeyDefinition) On(table string) *ForeignKeyDefinition
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 ¶
IndexDefinition represents an index on a database table.
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.
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.