Documentation
¶
Overview ¶
Package dbschema computes a structured schema diff between two Postgres databases and renders a forward migration as text. It never runs the SQL.
Index ¶
Constants ¶
const ( ChangeAdded = "added" ChangeRemoved = "removed" ChangeChanged = "changed" ObjectSchema = "schema" ObjectTable = "table" ObjectColumn = "column" ObjectIndex = "index" ObjectConstraint = "constraint" ObjectView = "view" ObjectSequence = "sequence" )
Kinds of change and of object, as they appear in the API.
const Header = "-- Generated schema migration. Review before running; Levelrail does not run it."
Header marks the script as text for a human; this package never executes it.
Variables ¶
var ErrUnsupported = errors.New("schema diff supports Postgres only")
ErrUnsupported reports an engine the reader has no catalog queries for.
Functions ¶
This section is empty.
Types ¶
type Change ¶
type Change struct {
Object string `json:"object"`
Kind string `json:"kind"`
Schema string `json:"schema"`
Table string `json:"table,omitempty"`
Name string `json:"name"`
Before string `json:"before,omitempty"`
After string `json:"after,omitempty"`
// BeforeKind and AfterKind name the view kind when it matters, so a kind change is visible.
BeforeKind string `json:"before_kind,omitempty"`
AfterKind string `json:"after_kind,omitempty"`
// Unsupported marks a change the generator cannot turn into a statement; the SQL carries a comment instead.
Unsupported bool `json:"unsupported,omitempty"`
}
Change is one difference. Table is set for columns, indexes and constraints.
type Column ¶
type Column struct {
Name string `json:"name"`
Type string `json:"type"`
Nullable bool `json:"nullable"`
Default string `json:"default,omitempty"`
// Identity is "a" (ALWAYS) or "d" (BY DEFAULT) for an identity column; Generated is "s" for a stored
// generated column, whose expression is carried in Default.
Identity string `json:"identity,omitempty"`
Generated string `json:"generated,omitempty"`
}
Column is one table column as the catalog reports it.
type Constraint ¶
type Constraint struct {
Name string `json:"name"`
Type string `json:"type"`
Definition string `json:"definition"`
}
Constraint is a primary key, unique, check or foreign key constraint (types p, u, c, f).
type Index ¶
Index is a standalone index; indexes that back a constraint are carried by the constraint.
type Result ¶
type Result struct {
Summary Summary `json:"summary"`
Changes []Change `json:"changes"`
SQL string `json:"sql"`
Truncated bool `json:"truncated"`
}
Result is a diff with the forward migration that turns base into head.
type Sequence ¶
type Sequence struct {
Schema string `json:"schema"`
Name string `json:"name"`
Type string `json:"type"`
Start int64 `json:"start"`
Increment int64 `json:"increment"`
Min int64 `json:"min"`
Max int64 `json:"max"`
Cycle bool `json:"cycle"`
}
Sequence is a standalone sequence; sequences owned by a column are left to the column.
type Snapshot ¶
type Snapshot struct {
Schemas []string `json:"schemas"`
Tables []Table `json:"tables"`
Views []View `json:"views"`
Sequences []Sequence `json:"sequences"`
// Truncated reports that the object cap cut the read short.
Truncated bool `json:"truncated"`
}
Snapshot is the schema of one database at one moment.
type Summary ¶
type Summary struct {
Added int `json:"added"`
Removed int `json:"removed"`
Changed int `json:"changed"`
}
Summary counts the changes by kind.