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 ¶
- Variables
- func Ident(name string) string
- func Simple(c Column) bool
- type Column
- type Constraint
- type Dialect
- func (d Dialect) AddColumn(t Table, c Column) []string
- func (d Dialect) AddConstraint(table string, k Constraint) []string
- func (d Dialect) AlterColumn(t Table, o, c Column) []string
- func (d Dialect) Check(t Table) error
- func (d Dialect) Create(t Table, ifNotExists bool) []string
- func (d Dialect) CreateExtension(name string) []string
- func (d Dialect) CreateIndex(ix Index) []string
- func (d Dialect) DropAll(t Table) []string
- func (d Dialect) DropColumn(table string, c Column) []string
- func (d Dialect) DropConstraint(table string, k Constraint) []string
- func (d Dialect) DropDeclared(table string, ix Index) []string
- func (d Dialect) DropExtension(name string) []string
- func (d Dialect) DropIndex(table, name string) []string
- func (d Dialect) DropTable(name string) []string
- func (d Dialect) Remake(from, to Table) []string
- func (d Dialect) RenameColumn(table, from, to string) []string
- func (d Dialect) RenameTable(from, to string) []string
- type Dialects
- type ForeignKey
- type Index
- type Kind
- type Table
Constants ¶
This section is empty.
Variables ¶
var Names = []string{"postgres", "mysql", "sqlite"}
Names is the dialects, in the order SQL is written for them.
Functions ¶
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.
type Constraint ¶
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 ¶
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 ¶
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 ¶
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 ¶
Check is what keeps t from being made on d: a generated column with no expression for d.
func (Dialect) Create ¶
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 ¶
CreateExtension and DropExtension install and remove a Postgres extension; other dialects have none.
func (Dialect) CreateIndex ¶
CreateIndex is the statement creating a declared index on d, none when d has no SQL for it.
func (Dialect) DropAll ¶
DropAll drops t: first what of it DROP TABLE leaves behind (a declared index's Drop), then the table.
func (Dialect) DropColumn ¶
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 ¶
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 (Dialect) Remake ¶
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 ¶
RenameColumn renames a column of table.
func (Dialect) RenameTable ¶
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.
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 ¶
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 ¶
Sort orders tables so each comes after the tables its foreign keys refer to (a cycle keeps its order).