Documentation
¶
Overview ¶
Package migrate turns the difference between two schemas into DDL, and checks DDL by its end state: applied to a PostgreSQL holding the current schema, does the database read back as the target?
Both schemas should be canonical (dump.Canonical / dump.Load): the target's statement texts are then PostgreSQL's own, fully qualified, and its objects come in dependency order, which the plan follows for additions. Whole objects are created from the text that declared them (Relation.Definition and friends); parts (columns, constraints, labels) are rendered from the model.
Seeded tables (schema.Relation.Seed) are brought to their declared rows last, with a MERGE per table whose content differs (seed.go).
The plan is a proposal: it does not know how to rename, which value an enum label should map to when it goes, or what to backfill a new NOT NULL column with. Those show up as statements PostgreSQL will refuse or as a leftover difference in Verify, and are the intent declarations' job to settle.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Plan ¶
Plan lists the statements that turn from into to: drops (dependents first), the declared renames, then alterations, then additions in the target's order, then the rows of the seeded tables (a MERGE per table whose content differs). The intents (ParseIntents of the target's source) decide what the diff cannot: a table or column that disappears must be declared dropped or renamed, an enum label that disappears must say which label its values become, and backfills fill new columns before they turn NOT NULL. The error lists every declaration the schemas do not bear out and every change no declaration explains; the DDL is still returned for reading.
func Verify ¶
func Verify(ctx context.Context, c dump.Canonicalizer, currentSQL, ddl string, target *schema.Schema) (changes, notes []diff.Change, err error)
Verify applies ddl to a fresh database holding currentSQL and lists what still differs from target. An error is a statement PostgreSQL refused (or the server failing); no changes and no error means the DDL reaches the target. Column order is tolerated (diff.Change.OrderOnly) and returned separately as notes.
Types ¶
type Intent ¶
type Intent struct {
Kind IntentKind
// Rename / Drop: dotted names (schema.table.column, table.column or table; two parts
// are a table in a non-public schema when one exists, else a public table's column).
From, To string
// EnumDrop: the enum type, the label removed and the label its values become.
Enum, Label, Using string
// Backfill: the column assigned, the SQL expression and the optional WHERE predicate.
Table, Column, Expr, Where string
Line int
}
Intent is one `-- @migrate` declaration in the schema source: what a diff of two schemas cannot decide on its own. Declarations describe the step from the database's current state to schema.sql, and go away once applied: a declaration the diff no longer bears out is an error, so stale ones cannot linger.
-- @migrate rename orders.state -> orders.status column (or table: rename old -> new);
the left side names things as they are
now, the right as they will be
-- @migrate drop orders.legacy a column or table may go (data loss accepted)
-- @migrate enum order_status: drop 'canceled' using 'cancelled'
-- @migrate backfill orders.status = 'pending' where status is null
func ParseIntents ¶
ParseIntents reads the `-- @migrate` lines of schema source text.