schema

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {
	*types.Column
}

Column wraps types.Column and provides modifier methods.

func (*Column) Comment

func (c *Column) Comment(comment string) *Column

Comment sets a comment/description for the column.

func (*Column) Default

func (c *Column) Default(value any) *Column

Default sets a default value for this column.

func (*Column) Length

func (c *Column) Length(n int) *Column

Length sets the length for string/binary types (e.g., VARCHAR(100)).

func (*Column) NotNullable

func (c *Column) NotNullable() *Column

NotNullable marks this column as NOT NULL.

func (*Column) Nullable

func (c *Column) Nullable() *Column

Nullable marks this column as nullable (removes NOT NULL constraint).

func (*Column) OnDelete

func (c *Column) OnDelete(action string) *Column

OnDelete sets the ON DELETE action for the foreign key reference. Valid values: CASCADE, SET NULL, RESTRICT, NO ACTION

func (*Column) OnUpdate

func (c *Column) OnUpdate(action string) *Column

OnUpdate sets the ON UPDATE action for the foreign key reference. Valid values: CASCADE, SET NULL, RESTRICT, NO ACTION

func (*Column) Precision

func (c *Column) Precision(p int) *Column

Precision sets the precision for numeric types (e.g., DECIMAL(10,2), FLOAT(53)).

func (*Column) Primary

func (c *Column) Primary() *Column

Primary marks this column as a primary key.

func (*Column) References

func (c *Column) References(table, column string) *Column

References sets up a foreign key reference to another table's column.

func (*Column) Scale

func (c *Column) Scale(s int) *Column

Scale sets the scale for numeric types (e.g., DECIMAL(10,2)).

func (*Column) Unique

func (c *Column) Unique() *Column

Unique adds a UNIQUE constraint to this column.

func (*Column) Unsigned

func (c *Column) Unsigned() *Column

Unsigned marks this column as unsigned (for numeric types).

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

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

func New(cfg *config.Config) *Schema

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) BeginTx

func (s *Schema) BeginTx() (*sql.Tx, error)

BeginTx starts a new transaction and returns it.

func (*Schema) Close

func (s *Schema) Close() error

Close closes the database connection.

func (*Schema) CreateTable

func (s *Schema) CreateTable(name string, builder func(t *Table)) error

CreateTable creates a new table with the given name using the builder function.

func (*Schema) CreateTableIfNotExists

func (s *Schema) CreateTableIfNotExists(name string, builder func(t *Table)) error

CreateTableIfNotExists creates a new table if it doesn't already exist.

func (*Schema) DB

func (s *Schema) DB() *sql.DB

DB returns the database connection, if set.

func (*Schema) Dialect

func (s *Schema) Dialect() dialect.Dialect

Dialect returns the current dialect.

func (*Schema) DropTable

func (s *Schema) DropTable(name string) error

DropTable drops a table by name.

func (*Schema) DropTableIfExists

func (s *Schema) DropTableIfExists(name string) error

DropTableIfExists drops a table if it exists.

func (*Schema) HasColumn

func (s *Schema) HasColumn(table, column string) bool

HasColumn checks if a column exists in a table.

func (*Schema) HasTable

func (s *Schema) HasTable(name string) bool

HasTable checks if a table exists.

func (*Schema) Open

func (s *Schema) Open() error

Open opens a database connection using the config.

func (*Schema) Raw

func (s *Schema) Raw(sql string, args ...any) error

Raw executes a raw SQL statement with optional parameters. Use this for custom DDL, data migrations, or database-specific features.

func (*Schema) RenameTable

func (s *Schema) RenameTable(oldName, newName string) error

RenameTable renames a table from oldName to newName.

func (*Schema) SchemaName

func (s *Schema) SchemaName() string

SchemaName returns the current schema name (empty = default).

func (*Schema) SetDB

func (s *Schema) SetDB(db *sql.DB)

SetDB sets the database connection.

func (*Schema) Table

func (s *Schema) Table(name string, builder func(t *Table)) error

func (*Schema) WithSchema

func (s *Schema) WithSchema(schemaName string) *Schema

WithSchema returns a new Schema that operates on the specified schema.

func (*Schema) WithTx

func (s *Schema) WithTx(tx *sql.Tx) *Schema

WithTx returns a new Schema that uses the given transaction.

type Table

type Table struct {
	*types.Table
}

Table wraps types.Table and provides builder methods.

func NewTable

func NewTable(name string) *Table

NewTable creates a new Table with the given name.

func (*Table) BigInt

func (t *Table) BigInt(name string) *Column

BigInt creates a BIGINT column.

func (*Table) Binary

func (t *Table) Binary(name string) *Column

Binary creates a BINARY/BLOB column.

func (*Table) Boolean

func (t *Table) Boolean(name string) *Column

Boolean creates a BOOLEAN column.

func (*Table) Date

func (t *Table) Date(name string) *Column

Date creates a DATE column.

func (*Table) Decimal

func (t *Table) Decimal(name string) *Column

Decimal creates a DECIMAL column.

func (*Table) Double

func (t *Table) Double(name string) *Column

Double creates a DOUBLE PRECISION column.

func (*Table) DropColumn

func (t *Table) DropColumn(name string) *Table

DropColumn drops a column.

func (*Table) DropColumns

func (t *Table) DropColumns(names ...string) *Table

DropColumns drops multiple columns.

func (*Table) DropDefault

func (t *Table) DropDefault(column string) *Table

DropDefault drops the default value for a column.

func (*Table) DropForeign

func (t *Table) DropForeign(column string) *Table

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

func (t *Table) DropForeignByName(name string) *Table

DropForeignByName drops a foreign key constraint by its explicit name.

func (*Table) DropIndex

func (t *Table) DropIndex(columns ...string) *Table

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

func (t *Table) DropIndexByName(name string) *Table

DropIndexByName drops an index by its explicit name.

func (*Table) DropNullable

func (t *Table) DropNullable(column string) *Table

DropNullable sets a column as not nullable.

func (*Table) DropPrimary

func (t *Table) DropPrimary() *Table

DropPrimary drops the primary key constraint from the table. Uses the default PostgreSQL naming convention: tablename_pkey

func (*Table) DropPrimaryByName

func (t *Table) DropPrimaryByName(constraintName string) *Table

DropPrimaryByName drops the primary key constraint by explicit constraint name.

func (*Table) DropUnique

func (t *Table) DropUnique(columns ...string) *Table

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

func (t *Table) DropUniqueByName(name string) *Table

DropUniqueByName drops a unique index by its explicit name.

func (*Table) Float

func (t *Table) Float(name string) *Column

Float creates a FLOAT column.

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

func (t *Table) Increments(name string) *Column

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) Int

func (t *Table) Int(name string) *Column

Int creates an INT column.

func (*Table) JSON

func (t *Table) JSON(name string) *Column

JSON creates a JSON column.

func (*Table) JSONB

func (t *Table) JSONB(name string) *Column

JSONB creates a JSONB column (PostgreSQL-specific, falls back to JSON).

func (*Table) RenameColumn

func (t *Table) RenameColumn(oldName, newName string) *Table

RenameColumn renames a column.

func (*Table) SetDefault

func (t *Table) SetDefault(column string, defaultValue any) *Table

SetDefault sets the default value for a column.

func (*Table) SetNullable

func (t *Table) SetNullable(column string) *Table

SetNullable sets a column as nullable.

func (*Table) SmallInt

func (t *Table) SmallInt(name string) *Column

SmallInt creates a SMALLINT column.

func (*Table) String

func (t *Table) String(name string) *Column

String creates a VARCHAR column.

func (*Table) Text

func (t *Table) Text(name string) *Column

Text creates a TEXT column (for longer strings).

func (*Table) Time

func (t *Table) Time(name string) *Column

Time creates a TIME column.

func (*Table) Timestamp

func (t *Table) Timestamp(name string) *Column

Timestamp creates a TIMESTAMP column.

func (*Table) Timestamps

func (t *Table) Timestamps()

Timestamps creates created_at and updated_at timestamp columns.

func (*Table) UUID

func (t *Table) UUID(name string) *Column

UUID creates a UUID column.

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()).

Jump to

Keyboard shortcuts

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