schema

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AlterTable

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

AlterTable represents an ALTER TABLE operation.

func NewAlterTable

func NewAlterTable(tableName string) *AlterTable

NewAlterTable creates a new AlterTable.

func (*AlterTable) AddColumn

func (a *AlterTable) AddColumn(col ColumnDefinition) *AlterTable

AddColumn adds a new column.

func (*AlterTable) AddForeignKey

func (a *AlterTable) AddForeignKey(fk SchemaForeignKey) *AlterTable

AddForeignKey adds a foreign key constraint.

func (*AlterTable) AddIndex

func (a *AlterTable) AddIndex(idx SchemaIndex) *AlterTable

AddIndex adds a new index.

func (*AlterTable) DropColumn

func (a *AlterTable) DropColumn(name string) *AlterTable

DropColumn drops a column.

func (*AlterTable) DropForeignKey

func (a *AlterTable) DropForeignKey(name string) *AlterTable

DropForeignKey drops a foreign key constraint.

func (*AlterTable) DropIndex

func (a *AlterTable) DropIndex(name string) *AlterTable

DropIndex drops an index.

func (*AlterTable) ModifyColumn

func (a *AlterTable) ModifyColumn(name, newType string) *AlterTable

ModifyColumn modifies a column type.

func (*AlterTable) RenameColumn

func (a *AlterTable) RenameColumn(from, to string) *AlterTable

RenameColumn renames a column.

func (*AlterTable) ToSQL

func (a *AlterTable) ToSQL(dialect string) string

ToSQL generates the ALTER TABLE SQL.

type Blueprint

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

Blueprint represents the schema definition for a table.

func NewBlueprint

func NewBlueprint(table string) *Blueprint

NewBlueprint creates a new table blueprint.

func (*Blueprint) Boolean

func (b *Blueprint) Boolean(name string) *Blueprint

Boolean creates a BOOLEAN column.

func (*Blueprint) Columns

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

Columns returns the defined columns.

func (*Blueprint) Default

func (b *Blueprint) Default(val any) *Blueprint

Default sets a default value for the last defined column.

func (*Blueprint) DropColumn

func (b *Blueprint) DropColumn(name string) *Blueprint

DropColumn adds a column to be dropped.

func (*Blueprint) Foreign

func (b *Blueprint) Foreign(column string) *ForeignKeyBuilder

Foreign starts a foreign key definition on the given column.

func (*Blueprint) ForeignKeys

func (b *Blueprint) ForeignKeys() []ForeignKey

ForeignKeys returns the defined foreign keys.

func (*Blueprint) ID

func (b *Blueprint) ID() *Blueprint

ID creates an auto-incrementing primary key "id" column.

func (*Blueprint) Index

func (b *Blueprint) Index(columns ...string) *Blueprint

Index marks the last defined column for indexing, or adds a table-level index. No args: modifies the last column. With args: adds a table-level index on the given columns.

func (*Blueprint) Indexes

func (b *Blueprint) Indexes() []string

Indexes returns the defined index column names.

func (*Blueprint) Integer

func (b *Blueprint) Integer(name string) *Blueprint

Integer creates an INT column.

func (*Blueprint) Nullable

func (b *Blueprint) Nullable() *Blueprint

Nullable makes the last defined column nullable.

func (*Blueprint) Primary

func (b *Blueprint) Primary(columns ...string) *Blueprint

Primary makes the last defined column a primary key, or sets a composite primary key. No args: modifies the last column. With args: sets a table-level composite primary key.

func (*Blueprint) PrimaryKeyColumns

func (b *Blueprint) PrimaryKeyColumns() []string

PrimaryKeyColumns returns the composite primary key columns.

func (*Blueprint) SoftDeletes

func (b *Blueprint) SoftDeletes() *Blueprint

SoftDeletes creates a deleted_at column.

func (*Blueprint) String

func (b *Blueprint) String(name string, length int) *Blueprint

String creates a VARCHAR column.

func (*Blueprint) Table

func (b *Blueprint) Table() string

Table returns the table name.

func (*Blueprint) Text

func (b *Blueprint) Text(name string) *Blueprint

Text creates a TEXT column.

func (*Blueprint) Timestamp

func (b *Blueprint) Timestamp(name string) *Blueprint

Timestamp creates a TIMESTAMP column.

func (*Blueprint) Timestamps

func (b *Blueprint) Timestamps() *Blueprint

Timestamps creates created_at and updated_at columns.

func (*Blueprint) Unique

func (b *Blueprint) Unique(columns ...string) *Blueprint

Unique makes the last defined column unique, or adds a composite unique constraint. No args: modifies the last column. With args: adds a table-level unique constraint.

func (*Blueprint) UnsignedBigInteger

func (b *Blueprint) UnsignedBigInteger(name string) *Blueprint

UnsignedBigInteger creates an UNSIGNED BIGINT column.

type Builder

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

Builder handles DDL execution against the database.

func NewBuilder

func NewBuilder(conn *sql.DB, d dialect.Dialect) *Builder

NewBuilder creates a new Schema Builder.

func (*Builder) Create

func (s *Builder) Create(table string, callback func(*Blueprint)) error

Create executes a CREATE TABLE statement, then creates any indexes separately.

func (*Builder) Drop

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

Drop executes a DROP TABLE statement.

func (*Builder) DropIfExists

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

DropIfExists executes a DROP TABLE IF EXISTS statement.

type ColumnDefinition

type ColumnDefinition struct {
	Name          string
	Type          string
	Length        int
	Nullable      bool
	Unique        bool
	Primary       bool
	AutoIncrement bool
	Default       any
}

ColumnDefinition represents a database column.

type ColumnInspector

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

ColumnInspector provides methods to inspect existing table columns.

func NewColumnInspector

func NewColumnInspector(db query.QueryExecer, dialect string) *ColumnInspector

NewColumnInspector creates a new ColumnInspector.

func (*ColumnInspector) GetColumns

func (ci *ColumnInspector) GetColumns(tableName string) ([]ColumnDefinition, error)

GetColumns returns the columns of a table.

func (*ColumnInspector) GetCurrentTimestamp

func (ci *ColumnInspector) GetCurrentTimestamp() time.Time

GetCurrentTimestamp returns the current database timestamp.

func (*ColumnInspector) GetIndexes

func (ci *ColumnInspector) GetIndexes(tableName string) ([]SchemaIndex, error)

GetIndexes returns the indexes of a table.

func (*ColumnInspector) HasColumn

func (ci *ColumnInspector) HasColumn(tableName, columnName string) bool

HasColumn checks if a column exists in a table.

func (*ColumnInspector) HasTable

func (ci *ColumnInspector) HasTable(tableName string) bool

HasTable checks if a table exists.

type ForeignKey

type ForeignKey struct {
	Column           string
	ReferencedTable  string
	ReferencedColumn string
	OnDelete         string
	OnUpdate         string
}

ForeignKey represents a foreign key constraint.

type ForeignKeyBuilder

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

ForeignKeyBuilder is a fluent builder for foreign key constraints.

func (*ForeignKeyBuilder) References

func (f *ForeignKeyBuilder) References(column string) *ForeignKeyOnBuilder

References sets the referenced column.

type ForeignKeyFinalBuilder

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

ForeignKeyFinalBuilder allows setting OnDelete/OnUpdate on the stored FK.

func (*ForeignKeyFinalBuilder) OnDelete

OnDelete sets the ON DELETE action.

func (*ForeignKeyFinalBuilder) OnUpdate

OnUpdate sets the ON UPDATE action.

type ForeignKeyOnBuilder

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

ForeignKeyOnBuilder holds the FK state before On() is called.

func (*ForeignKeyOnBuilder) On

On sets the referenced table and stores the FK in the blueprint. Returns a *ForeignKeyFinalBuilder for optional OnDelete/OnUpdate chaining.

type ModifyColumnOp

type ModifyColumnOp struct {
	Name string
	Type string
}

ModifyColumnOp represents a modify column operation.

type RenameColumnOp

type RenameColumnOp struct {
	From string
	To   string
}

RenameColumnOp represents a rename column operation.

type SchemaForeignKey

type SchemaForeignKey struct {
	Name              string
	Columns           []string
	ReferencedTable   string
	ReferencedColumns []string
	OnDelete          string
	OnUpdate          string
}

SchemaForeignKey represents a foreign key constraint.

type SchemaIndex

type SchemaIndex struct {
	Name    string
	Columns []string
	Unique  bool
	Type    string
}

SchemaIndex represents an index on a table.

type SchemaTable

type SchemaTable struct {
	Name        string
	Columns     []ColumnDefinition
	Indexes     []SchemaIndex
	ForeignKeys []SchemaForeignKey
	Comment     string
}

SchemaTable represents a database table schema.

func (*SchemaTable) ColumnCount

func (t *SchemaTable) ColumnCount() int

ColumnCount returns the number of columns.

func (*SchemaTable) ColumnNames

func (t *SchemaTable) ColumnNames() []string

ColumnNames returns all column names.

func (*SchemaTable) GetColumn

func (t *SchemaTable) GetColumn(name string) *ColumnDefinition

GetColumn returns a column by name.

func (*SchemaTable) HasColumn

func (t *SchemaTable) HasColumn(name string) bool

HasColumn checks if a column exists.

func (*SchemaTable) HasForeignKey

func (t *SchemaTable) HasForeignKey(name string) bool

HasForeignKey checks if a foreign key exists.

func (*SchemaTable) HasIndex

func (t *SchemaTable) HasIndex(name string) bool

HasIndex checks if an index exists.

Jump to

Keyboard shortcuts

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