Documentation
¶
Overview ¶
Package migration is the ORM's schema migrations, Django's: Go files orm makemigrations writes into the program (migrations/NNNN_name.go, a package of their own), each registering a Migration of operations from its init. orm.Migrate applies the registered migrations at start up, orm migrate on demand, forwards or back.
import m "github.com/vuka-lang/orm/migration"
func init() {
m.Register(m.Migration{
Name: "0002_users_email",
Dependencies: []string{"0001_initial"},
Operations: []m.Operation{
m.AddField{Table: "users", Name: "email", Field: m.Varchar(255).Unique().Null()},
},
})
}
The schema a database has is the replay of its migrations: no snapshot file is kept, and makemigrations compares the replay with the models the program declares.
Index ¶
- Constants
- func Describe(op Operation) string
- func Leaves(ordered []Migration, db string) []string
- func Register(mg Migration)
- func Source(pkg string, mg Migration, notes []string) ([]byte, error)
- type AddConstraint
- type AddField
- type AddIndex
- type AlterField
- type AlterModelTable
- type Constraint
- type CreateExtension
- type CreateFunction
- type CreateModel
- type DeleteModel
- type Dialects
- type Field
- func Auto() Field
- func BigAuto() Field
- func BigInt() Field
- func Bool() Field
- func Bytes() Field
- func Custom() Field
- func Date() Field
- func Float() Field
- func Float32() Field
- func Int() Field
- func JSON() Field
- func SmallInt() Field
- func TSVector() Field
- func Text() Field
- func Time() Field
- func UUID() Field
- func Varchar(size int) Field
- func Vector(dims int) Field
- func (f Field) Auto() Field
- func (f Field) Collation(name string) Field
- func (f Field) Comment(s string) Field
- func (f Field) Default(v any) Field
- func (f Field) DefaultSQL(expr string) Field
- func (f Field) FK(table, ref string) Field
- func (f Field) Generated(expr Dialects) Field
- func (f Field) Index() Field
- func (f Field) Null() Field
- func (f Field) OnDelete(action string) Field
- func (f Field) PK() Field
- func (f Field) Precision(n int) Field
- func (f Field) Type(sql string) Field
- func (f Field) Types(t Dialects) Field
- func (f Field) Unique() Field
- func (f Field) Unsigned() Field
- type Index
- type Migration
- type NamedField
- type Operation
- type RemoveConstraint
- type RemoveField
- type RemoveIndex
- type RenameField
- type RenameModel
- type RunGo
- type RunSQL
- type State
- type Step
- type Tx
Constants ¶
const ( Cascade = "cascade" SetNull = "set_null" Restrict = "restrict" )
The actions of OnDelete.
const Noop = "-- nothing to undo"
Noop is a RunSQL's ReverseSQL that does nothing.
Variables ¶
This section is empty.
Functions ¶
func Leaves ¶
Leaves is the names of db's migrations none of its others depends on: what the next one depends on.
Types ¶
type AddConstraint ¶
type AddConstraint struct {
Table string
Constraint Constraint
}
AddConstraint adds a unique or check constraint to a table.
type AlterField ¶
AlterField changes a column's definition: its type, nullability, default, key, foreign key, generated expression.
type AlterModelTable ¶
type AlterModelTable = RenameModel
AlterModelTable is RenameModel, by Django's name for changing a model's table: here a model is its table.
type Constraint ¶
type Constraint = schema.Constraint
Constraint is a table's unique constraint (Unique) or check constraint (Check).
func Check ¶
func Check(name, expr string) Constraint
Check is a check constraint: rows must satisfy expr, SQL.
func Unique ¶
func Unique(name string, columns ...string) Constraint
Unique is a unique constraint over columns, Django's unique_together.
type CreateExtension ¶
type CreateExtension struct{ Name string }
CreateExtension installs a Postgres extension ("vector", "pg_trgm"); other databases have none, and it does nothing there.
type CreateFunction ¶
type CreateFunction struct {
Name string
Params, Returns Dialects
Body Dialects
Language string
Volatile bool // the result may differ for the same arguments: not IMMUTABLE / DETERMINISTIC
}
CreateFunction creates a function written in SQL on Postgres and MySQL, for an orm.Func to call; unapplying it drops it. SQLite runs no functions written in SQL — register a Go one with orm/sqlite's Func — and a dialect whose Body is empty gets nothing. Params and Returns are spelt as each database spells them; Body is Postgres's function body in Language (sql when empty: a SELECT or an expression's RETURN) and MySQL's routine body (RETURN …, or BEGIN … END, run whole).
m.CreateFunction{
Name: "initials",
Params: m.Dialects{Postgres: "s text", MySQL: "s VARCHAR(255)"},
Returns: m.Dialects{Postgres: "text", MySQL: "VARCHAR(255)"},
Body: m.Dialects{Postgres: "SELECT upper(left(s, 1))", MySQL: "RETURN UPPER(LEFT(s, 1))"},
}
type CreateModel ¶
type CreateModel struct {
Table string
Fields []NamedField
Constraints []Constraint
Indexes []Index
}
CreateModel creates a table.
type Field ¶
type Field struct {
// contains filtered or unexported fields
}
Field is a column's definition: a kind (BigAuto, Varchar, Time, …) and what its methods add.
func (Field) Default ¶
Default is the value rows written without the column take, and rows that exist when it is added: a string, number, bool or nil.
func (Field) DefaultSQL ¶
DefaultSQL is Default as an SQL expression of your own.
func (Field) Generated ¶
Generated makes the column a stored generated column, computed by the expression of each dialect.
func (Field) OnDelete ¶
OnDelete is what deleting the row a foreign key refers to does: one of Cascade, SetNull, Restrict.
type Index ¶
Index is an index a model declares (GIN, HNSW, IVFFlat, FULLTEXT, a SearchVector's), as the statement making it on each dialect: none where it has no SQL (SQLite makes none of them).
type Migration ¶
type Migration struct {
Name string // NNNN_words, unique within its database
DB string // the database's name (orm.AddDatabase); "" the default
// Dependencies is the migrations applied before it: names of its
// database's, or "<db>:<name>" of another's (":<name>" the default's).
Dependencies []string
Operations []Operation
}
Migration is one step of a database's schema: its operations, applied in order, after the migrations it depends on.
func Find ¶
Find is db's migration among ordered whose name is name, or starts with it (0002 for 0002_users_email) when one does.
func Ordered ¶
Ordered is every registered migration, each after its dependencies (by database and name where none orders them).
type NamedField ¶
NamedField is a column of CreateModel: its name and Field.
type Operation ¶
type Operation interface {
// contains filtered or unexported methods
}
Operation is a change of the schema: what it does to the state, and the SQL doing it, forwards and back.
func Diff ¶
Diff is the operations taking a database from the state from (its migrations replayed) to to (the models the program declares), and a note beside each that needs a person ("" for none). A table or column gone where one alike appeared may have been renamed: ask is asked (Did you rename users.mail to users.email?) and on yes the operation is a rename, else a removal and an addition. A column's default is the model's where it declares one, else the migrations' is kept (the value an AddField gave the rows that existed); a table's check constraints are the migrations' alone.
type RemoveConstraint ¶
type RemoveConstraint struct{ Table, Name string }
RemoveConstraint drops a unique or check constraint of a table.
type RemoveField ¶
type RemoveField struct{ Table, Name string }
RemoveField drops a column of a table, and its data.
type RemoveIndex ¶
type RemoveIndex struct{ Table, Name string }
RemoveIndex drops a declared index.
type RenameField ¶
type RenameField struct{ Table, Old, New string }
RenameField renames a column, its data kept.
type RenameModel ¶
type RenameModel struct{ Old, New string }
RenameModel renames a table, its rows kept: the constraints and indexes keep their names, the foreign keys referring to it follow.
type RunGo ¶
RunGo runs Go code, a data migration, in the migration's transaction: tx runs SQL in it, and the ORM's queries on ctx do too. Backward undoes it; nil, the migration can't be unapplied. It sees the database as the operations before it leave it, so query it in SQL, or with models that match that schema.
type RunSQL ¶
RunSQL runs SQL of your own (statements ending with semicolons), on every dialect or on Dialect's alone. ReverseSQL undoes it: empty, the migration can't be unapplied; Noop, there is nothing to undo.
type State ¶
type State struct {
// contains filtered or unexported fields
}
State is a database's schema as its migrations leave it: its tables and extensions.
type Step ¶
Step is a statement (or several, a RunSQL's) applying an operation, or Go code a RunGo runs. A step with both runs its Go, and shows its SQL.
type Tx ¶
type Tx interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}
Tx is where a RunGo runs: the migration's transaction (MySQL, which can't roll DDL back, its connection).