Documentation
¶
Overview ¶
Package diff reads nock schema files as Go source, orders their tables by foreign key, keeps the last generated state in snapshot.json, and turns the difference between the two into a SQL migration for SQLite or Postgres.
Index ¶
- Constants
- Variables
- func MarshalSnapshot(s *Schema) ([]byte, error)
- func SaveSnapshot(dir string, s *Schema) error
- func SchemaFiles(root string) ([]string, error)
- func SortTables(tables []schema.TableSpec) ([]schema.TableSpec, error)
- func WriteMigration(dir, name string, now time.Time, res *Result, next *Schema) (string, error)
- type Change
- type Dialect
- type Module
- type Options
- type Rename
- type Result
- type Schema
Constants ¶
const ( CreateTable = "create_table" DropTable = "drop_table" AddColumn = "add_column" DropColumn = "drop_column" RenameColumn = "rename_column" AlterColumn = "alter_column" CreateIndex = "create_index" DropIndex = "drop_index" RebuildTable = "rebuild_table" // CreateExtension is CREATE EXTENSION IF NOT EXISTS, on Postgres. CreateExtension = "create_extension" )
The Change kinds.
const SchemaImport = "github.com/fletchhq/fletch/nock/schema"
SchemaImport is the import path schema files use.
const SnapshotFile = "snapshot.json"
SnapshotFile is the file in the migrations directory that records the schema the last generated migration produced.
Variables ¶
var ErrFKCycle = errors.New("diff: foreign key cycle")
ErrFKCycle is returned by SortTables when foreign keys form a cycle.
Functions ¶
func MarshalSnapshot ¶
MarshalSnapshot renders s as canonical JSON: tables sorted by name, columns in declared order, indexes sorted by name, two-space indent.
func SaveSnapshot ¶
SaveSnapshot writes dir/snapshot.json.
func SchemaFiles ¶
SchemaFiles lists the schema files under root, sorted: modules/<name>/schema/schema.go and modules/<name>/schema.go, then database/schema.go.
func SortTables ¶
SortTables orders tables so that every table comes after the tables it references (self-references aside), breaking ties by name. It fails on a foreign key cycle and names the tables in it.
func WriteMigration ¶
WriteMigration writes res as dir/<version>_<name>.sql and saves next as the snapshot. The version is now as YYYYMMDDHHMMSS (UTC), raised past the newest existing migration so files stay in order. It never overwrites a file, and touches no other migration.
Types ¶
type Change ¶
type Change struct {
Kind string `json:"kind"`
Table string `json:"table"`
Column string `json:"column,omitempty"`
Index string `json:"index,omitempty"`
Detail string `json:"detail,omitempty"`
Destructive bool `json:"destructive,omitempty"`
}
Change is one schema change, for dry runs and --json output.
type Module ¶
type Module struct {
// Dir is the slash path from the project root: "modules/listings" or
// "database". It owns the module's tables.
Dir string
// SchemaDir is set when schema.go sits in a sub-package of Dir, such as
// "modules/tours/schema", so the module's other packages can import the
// generated tables. Generated code goes next to it.
SchemaDir string
// Package is the Go package name of schema.go.
Package string
// Declared lists the top-level names of the package's files other than
// schema_gen.go, so generated code can avoid them.
Declared []string
// DeclaredIn maps each name in Declared to the file, relative to the
// directory of schema.go, that declares it. ParseSource fills it for
// schema.go; ParseProject adds the package's other files.
DeclaredIn map[string]string
}
Module is a directory whose schema.go declares tables.
type Options ¶
type Options struct {
Dialect Dialect
// Renames are confirmed column renames. A column that disappears while
// a column of the same type appears is otherwise a drop and an add, and
// is reported in RenameCandidates.
Renames []Rename
}
Options configure Generate.
type Result ¶
type Result struct {
Dialect Dialect `json:"dialect"`
Changes []Change `json:"changes"`
Warnings []string `json:"warnings,omitempty"`
RenameCandidates []Rename `json:"rename_candidates,omitempty"`
Up string `json:"up"`
Down string `json:"down"`
NoTransaction bool `json:"no_transaction,omitempty"`
}
Result is a generated migration.
func Generate ¶
Generate diffs prev (the snapshot; nil for none) against next and returns the up and down SQL for opts.Dialect.
func (*Result) Destructive ¶
Destructive reports whether a change drops a table or a column.
type Schema ¶
Schema is every table of a project.
func LoadSnapshot ¶
LoadSnapshot reads dir/snapshot.json. A missing file is an empty schema.
func ParseProject ¶
ParseProject reads the files SchemaFiles lists under root. Nothing is compiled or run. Tables are sorted by name and validated: unique names, typed columns, known foreign key targets, and indexes on existing columns. A table's Module is the module that owns it, "modules/tours", even when its schema.go is in modules/tours/schema. A module may declare tables in only one of the two.
func UnmarshalSnapshot ¶
UnmarshalSnapshot reads a snapshot written by MarshalSnapshot.