schema

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package schema is the ORM's tables as the database needs them, and the SQL that creates and changes them on each dialect. A table is described once for every dialect: what differs (a generated column's expression, a declared index) is held per dialect. The ORM builds it from its models (orm.CreateTables, the migration autodetector), and migrations replay their operations into it.

Index

Constants

This section is empty.

Variables

View Source
var Names = []string{"postgres", "mysql", "sqlite"}

Names is the dialects, in the order SQL is written for them.

Functions

func Ident

func Ident(name string) string

Ident is name as every dialect takes an identifier: at most 63 bytes, a longer one cut and told apart by a hash of it.

func Simple

func Simple(c Column) bool

Simple is whether SQLite adds (or drops) c with ALTER TABLE: no key, constraint, foreign key or generated expression, and a value for rows that exist. Others rebuild the table (Remake).

Types

type Column

type Column struct {
	Name     string
	Kind     Kind
	Nullable bool
	PK       bool
	Auto     bool // an integer key the database counts
	Unique   bool
	Index    bool
	Size     int
	Type     string // a database type of the model's own
	Vector   int    // a pgvector column of so many dimensions (-1: any)
	TSVector bool   // a Postgres tsvector column
	// Default is the SQL literal a row written without the column takes.
	// CURRENT_TIMESTAMP and CURRENT_DATE are written as each dialect
	// needs them (MySQL's of the column's precision, its date in
	// parentheses).
	Default string
	// Types is the column's type on each dialect, its Go type's
	// (ColumnType): where one is given it wins over the kind's.
	Types Dialects
	// Comment is the column's comment (Postgres, MySQL; SQLite has none);
	// Collation its collation; Precision a timestamp's fractional digits
	// (0: the dialect's default); Unsigned an integer UNSIGNED on MySQL.
	Comment   string
	Collation string
	Precision int
	Unsigned  bool
	// Generated is the expression a stored generated column is computed
	// by, on each dialect.
	Generated Dialects
	FK        *ForeignKey

	// The names of the column's unique constraint and index, given when
	// the column comes to have them (see Table.Named).
	UniqueName, IndexName string
}

Column is a table's column.

func (*Column) Named

func (c *Column) Named(table string)

Named names c's unique constraint, index and foreign key, where it has them unnamed, after table; and drops the names of those it hasn't.

func (Column) Same

func (c Column) Same(o Column) bool

Same is whether c and o define a column alike, their names and their constraints' aside.

type Constraint

type Constraint struct {
	Name    string
	Columns []string
	Check   string
}

Constraint is a table's unique constraint over columns, or, with Check, its check constraint.

type Dialect

type Dialect struct {
	Name  string // postgres, mysql, sqlite
	Quote func(string) string
	// Vectors is whether a MySQL server has the VECTOR type (MySQL 9,
	// MariaDB 11.7): without it a vector is a VARBINARY of the same bytes.
	Vectors bool
}

Dialect is how a database writes the schema.

func For

func For(name string) Dialect

For is the dialect named (postgres, mysql, sqlite), quoting as the ORM does. A MySQL server's version may follow a slash, as VERSION() gives it (mysql/9.3.0, mysql/11.7.2-MariaDB): it says whether the server has the VECTOR type.

func (Dialect) AddColumn

func (d Dialect) AddColumn(t Table, c Column) []string

AddColumn adds c to the table t (t as it is with c), and its constraint, foreign key and index. Not SQLite's: see Simple.

func (Dialect) AddConstraint

func (d Dialect) AddConstraint(table string, k Constraint) []string

AddConstraint adds a unique or check constraint to table. Not SQLite's: it rebuilds the table.

func (Dialect) AlterColumn

func (d Dialect) AlterColumn(t Table, o, c Column) []string

AlterColumn changes column o of the table t (t as it is after) to c: its type, nullability, default, unique constraint, index, foreign key and generated expression. Not SQLite's: it rebuilds the table.

func (Dialect) Check

func (d Dialect) Check(t Table) error

Check is what keeps t from being made on d: a generated column with no expression for d.

func (Dialect) Create

func (d Dialect) Create(t Table, ifNotExists bool) []string

Create is the statements creating t, its names given (Named): the table, its constraints and foreign keys inside it, then its indexes. ifNotExists leaves a table that is there alone.

func (Dialect) CreateExtension

func (d Dialect) CreateExtension(name string) []string

CreateExtension and DropExtension install and remove a Postgres extension; other dialects have none.

func (Dialect) CreateIndex

func (d Dialect) CreateIndex(ix Index) []string

CreateIndex is the statement creating a declared index on d, none when d has no SQL for it.

func (Dialect) DropAll

func (d Dialect) DropAll(t Table) []string

DropAll drops t: first what of it DROP TABLE leaves behind (a declared index's Drop), then the table.

func (Dialect) DropColumn

func (d Dialect) DropColumn(table string, c Column) []string

DropColumn drops c from table: its foreign key first on MySQL, which won't drop a column one holds.

func (Dialect) DropConstraint

func (d Dialect) DropConstraint(table string, k Constraint) []string

DropConstraint drops a unique or check constraint of table. Not SQLite's: it rebuilds the table.

func (Dialect) DropDeclared

func (d Dialect) DropDeclared(table string, ix Index) []string

DropDeclared drops the declared index ix of table: by its Drop where it has one, else DROP INDEX; nothing where d has no SQL making it.

func (Dialect) DropExtension

func (d Dialect) DropExtension(name string) []string

func (Dialect) DropIndex

func (d Dialect) DropIndex(table, name string) []string

DropIndex drops the index named of table.

func (Dialect) DropTable

func (d Dialect) DropTable(name string) []string

DropTable drops a table.

func (Dialect) Remake

func (d Dialect) Remake(from, to Table) []string

Remake is SQLite's change of a table it can't ALTER: the table made again as to is, the rows of from copied over (columns of the same name, generated ones aside), from dropped and the new table renamed to it, then to's indexes. The connection must have foreign keys off.

func (Dialect) RenameColumn

func (d Dialect) RenameColumn(table, from, to string) []string

RenameColumn renames a column of table.

func (Dialect) RenameTable

func (d Dialect) RenameTable(from, to string) []string

RenameTable renames a table: the constraints and indexes keep their names, and the foreign keys referring to it follow.

type Dialects

type Dialects struct {
	Postgres, MySQL, SQLite string
}

Dialects is SQL written for each dialect: "" where a dialect has none.

func (Dialects) For

func (x Dialects) For(dialect string) string

For is the SQL of the dialect named.

func (Dialects) Set

func (x Dialects) Set(dialect, s string) Dialects

Set is x with the dialect's SQL set to s.

func (Dialects) Zero

func (x Dialects) Zero() bool

type ForeignKey

type ForeignKey struct {
	Table    string
	Ref      string
	OnDelete string // cascade, set_null, restrict
	Name     string
}

ForeignKey is a column referring to another table's key.

type Index

type Index struct {
	Name string
	SQL  Dialects
	Drop Dialects
}

Index is an index a model declares (GIN, HNSW, FULLTEXT, …), as the statement creating it on each dialect: none where it has no SQL. Drop is the statements dropping it where DROP INDEX doesn't: SQLite's full-text table and the triggers keeping it.

type Kind

type Kind string

Kind is what a column holds, as the database types it.

const (
	Bool    Kind = "bool"
	Int     Kind = "int" // 64-bit
	Int32   Kind = "int32"
	Int16   Kind = "int16" // a SMALLINT
	Float   Kind = "float"
	Float32 Kind = "float32"
	String  Kind = "string"
	Time    Kind = "time"
	Date    Kind = "date" // a DATE: the day alone
	UUID    Kind = "uuid" // uuid on Postgres, CHAR(36) elsewhere
	Bytes   Kind = "bytes"
	JSON    Kind = "json"
	Custom  Kind = "custom" // a Scanner/Valuer: TEXT unless typed
)

type Table

type Table struct {
	Name        string
	Columns     []Column
	Constraints []Constraint
	Indexes     []Index
}

Table is a table: its columns in order, its constraints, its declared indexes.

func Sort

func Sort(tables []Table) []Table

Sort orders tables so each comes after the tables its foreign keys refer to (a cycle keeps its order).

func (*Table) Column

func (t *Table) Column(name string) (*Column, bool)

Column is t's column named name.

func (Table) Named

func (t Table) Named() Table

Named is t with the constraints and indexes its columns hold named where they aren't: after the table and column, as the table is named now. Names stay with a renamed table or column.

Jump to

Keyboard shortcuts

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