Documentation
¶
Index ¶
- type Column
- func (c *Column) Comment(comment string) *Column
- func (c *Column) Default(value any) *Column
- func (c *Column) Length(n int) *Column
- func (c *Column) NotNullable() *Column
- func (c *Column) Nullable() *Column
- func (c *Column) OnDelete(action string) *Column
- func (c *Column) OnUpdate(action string) *Column
- func (c *Column) Precision(p int) *Column
- func (c *Column) Primary() *Column
- func (c *Column) References(table, column string) *Column
- func (c *Column) Scale(s int) *Column
- func (c *Column) Unique() *Column
- func (c *Column) Unsigned() *Column
- type Execer
- type ForeignKeyBuilder
- type IndexBuilder
- type Schema
- func (s *Schema) BeginTx() (*sql.Tx, error)
- func (s *Schema) Close() error
- func (s *Schema) CreateTable(name string, builder func(t *Table)) error
- func (s *Schema) CreateTableIfNotExists(name string, builder func(t *Table)) error
- func (s *Schema) DB() *sql.DB
- func (s *Schema) Dialect() dialect.Dialect
- func (s *Schema) DropTable(name string) error
- func (s *Schema) DropTableIfExists(name string) error
- func (s *Schema) HasColumn(table, column string) bool
- func (s *Schema) HasTable(name string) bool
- func (s *Schema) Open() error
- func (s *Schema) Raw(sql string, args ...any) error
- func (s *Schema) RenameTable(oldName, newName string) error
- func (s *Schema) SchemaName() string
- func (s *Schema) SetDB(db *sql.DB)
- func (s *Schema) Table(name string, builder func(t *Table)) error
- func (s *Schema) WithSchema(schemaName string) *Schema
- func (s *Schema) WithTx(tx *sql.Tx) *Schema
- type Table
- func (t *Table) BigInt(name string) *Column
- func (t *Table) Binary(name string) *Column
- func (t *Table) Boolean(name string) *Column
- func (t *Table) Date(name string) *Column
- func (t *Table) Decimal(name string) *Column
- func (t *Table) Double(name string) *Column
- func (t *Table) DropColumn(name string) *Table
- func (t *Table) DropColumns(names ...string) *Table
- func (t *Table) DropDefault(column string) *Table
- func (t *Table) DropForeign(column string) *Table
- func (t *Table) DropForeignByName(name string) *Table
- func (t *Table) DropIndex(columns ...string) *Table
- func (t *Table) DropIndexByName(name string) *Table
- func (t *Table) DropNullable(column string) *Table
- func (t *Table) DropPrimary() *Table
- func (t *Table) DropPrimaryByName(constraintName string) *Table
- func (t *Table) DropUnique(columns ...string) *Table
- func (t *Table) DropUniqueByName(name string) *Table
- func (t *Table) Float(name string) *Column
- func (t *Table) Foreign(column string) *ForeignKeyBuilder
- func (t *Table) Increments(name string) *Column
- func (t *Table) Index(columns ...string) *IndexBuilder
- func (t *Table) Int(name string) *Column
- func (t *Table) JSON(name string) *Column
- func (t *Table) JSONB(name string) *Column
- func (t *Table) RenameColumn(oldName, newName string) *Table
- func (t *Table) SetDefault(column string, defaultValue any) *Table
- func (t *Table) SetNullable(column string) *Table
- func (t *Table) SmallInt(name string) *Column
- func (t *Table) String(name string) *Column
- func (t *Table) Text(name string) *Column
- func (t *Table) Time(name string) *Column
- func (t *Table) Timestamp(name string) *Column
- func (t *Table) Timestamps()
- func (t *Table) UUID(name string) *Column
- func (t *Table) Unique(columns ...string) *IndexBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Column ¶
Column wraps types.Column and provides modifier methods.
func (*Column) NotNullable ¶
NotNullable marks this column as NOT NULL.
func (*Column) OnDelete ¶
OnDelete sets the ON DELETE action for the foreign key reference. Valid values: CASCADE, SET NULL, RESTRICT, NO ACTION
func (*Column) OnUpdate ¶
OnUpdate sets the ON UPDATE action for the foreign key reference. Valid values: CASCADE, SET NULL, RESTRICT, NO ACTION
func (*Column) Precision ¶
Precision sets the precision for numeric types (e.g., DECIMAL(10,2), FLOAT(53)).
func (*Column) References ¶
References sets up a foreign key reference to another table's column.
type Execer ¶
type Execer interface {
Exec(query string, args ...any) (sql.Result, error)
Query(query string, args ...any) (*sql.Rows, error)
QueryRow(query string, args ...any) *sql.Row
}
Execer is an interface for executing SQL (both *sql.DB and *sql.Tx).
type ForeignKeyBuilder ¶
type ForeignKeyBuilder struct {
// contains filtered or unexported fields
}
ForeignKeyBuilder provides a fluent interface for creating foreign keys.
func (*ForeignKeyBuilder) Name ¶
func (b *ForeignKeyBuilder) Name(n string) *ForeignKeyBuilder
Name sets a custom name for the foreign key constraint.
func (*ForeignKeyBuilder) OnDelete ¶
func (b *ForeignKeyBuilder) OnDelete(action string) *ForeignKeyBuilder
OnDelete sets the ON DELETE action (CASCADE, SET NULL, RESTRICT, NO ACTION).
func (*ForeignKeyBuilder) OnUpdate ¶
func (b *ForeignKeyBuilder) OnUpdate(action string) *ForeignKeyBuilder
OnUpdate sets the ON UPDATE action (CASCADE, SET NULL, RESTRICT, NO ACTION).
func (*ForeignKeyBuilder) References ¶
func (b *ForeignKeyBuilder) References(table, column string) *ForeignKeyBuilder
References sets the referenced table and column.
type IndexBuilder ¶
type IndexBuilder struct {
// contains filtered or unexported fields
}
IndexBuilder provides a fluent interface for creating indexes.
func (*IndexBuilder) Name ¶
func (b *IndexBuilder) Name(n string) *IndexBuilder
Name sets a custom name for the index.
func (*IndexBuilder) Using ¶
func (b *IndexBuilder) Using(method string) *IndexBuilder
Using sets the index method (e.g., btree, hash, gin, gist).
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema provides methods for database schema operations.
func New ¶
New creates a new Schema with the given config. It determines the dialect from the config and can optionally connect to the database.
func (*Schema) CreateTable ¶
CreateTable creates a new table with the given name using the builder function.
func (*Schema) CreateTableIfNotExists ¶
CreateTableIfNotExists creates a new table if it doesn't already exist.
func (*Schema) DropTableIfExists ¶
DropTableIfExists drops a table if it exists.
func (*Schema) Raw ¶
Raw executes a raw SQL statement with optional parameters. Use this for custom DDL, data migrations, or database-specific features.
func (*Schema) RenameTable ¶
RenameTable renames a table from oldName to newName.
func (*Schema) SchemaName ¶
SchemaName returns the current schema name (empty = default).
func (*Schema) WithSchema ¶
WithSchema returns a new Schema that operates on the specified schema.
type Table ¶
Table wraps types.Table and provides builder methods.
func (*Table) DropColumn ¶
DropColumn drops a column.
func (*Table) DropColumns ¶
DropColumns drops multiple columns.
func (*Table) DropDefault ¶
DropDefault drops the default value for a column.
func (*Table) DropForeign ¶
DropForeign drops a foreign key constraint by column (auto-generates the FK name). Uses the same naming convention as Foreign(): fk_tablename_column
func (*Table) DropForeignByName ¶
DropForeignByName drops a foreign key constraint by its explicit name.
func (*Table) DropIndex ¶
DropIndex drops an index by columns (auto-generates the index name). Uses the same naming convention as Index(): idx_tablename_col1_col2
func (*Table) DropIndexByName ¶
DropIndexByName drops an index by its explicit name.
func (*Table) DropNullable ¶
DropNullable sets a column as not nullable.
func (*Table) DropPrimary ¶
DropPrimary drops the primary key constraint from the table. Uses the default PostgreSQL naming convention: tablename_pkey
func (*Table) DropPrimaryByName ¶
DropPrimaryByName drops the primary key constraint by explicit constraint name.
func (*Table) DropUnique ¶
DropUnique drops a unique index by columns (auto-generates the index name). Uses the same naming convention as Unique(): uq_tablename_col1_col2
func (*Table) DropUniqueByName ¶
DropUniqueByName drops a unique index by its explicit name.
func (*Table) Foreign ¶
func (t *Table) Foreign(column string) *ForeignKeyBuilder
Foreign creates a foreign key constraint on the specified column. Returns a ForeignKeyBuilder for chaining (e.g., .References(), .OnDelete()).
func (*Table) Increments ¶
Increments creates an auto-incrementing primary key column.
func (*Table) Index ¶
func (t *Table) Index(columns ...string) *IndexBuilder
Index creates an index on the specified columns. Returns an IndexBuilder for optional chaining (e.g., .Name(), .Using()).
func (*Table) RenameColumn ¶
RenameColumn renames a column.
func (*Table) SetDefault ¶
SetDefault sets the default value for a column.
func (*Table) SetNullable ¶
SetNullable sets a column as nullable.
func (*Table) Timestamps ¶
func (t *Table) Timestamps()
Timestamps creates created_at and updated_at timestamp columns.
func (*Table) Unique ¶
func (t *Table) Unique(columns ...string) *IndexBuilder
Unique creates a unique index on the specified columns. Returns an IndexBuilder for optional chaining (e.g., .Name(), .Using()).