diff

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2026 License: Apache-2.0, MIT Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
const SchemaImport = "github.com/fletchhq/fletch/nock/schema"

SchemaImport is the import path schema files use.

View Source
const SnapshotFile = "snapshot.json"

SnapshotFile is the file in the migrations directory that records the schema the last generated migration produced.

Variables

View Source
var ErrFKCycle = errors.New("diff: foreign key cycle")

ErrFKCycle is returned by SortTables when foreign keys form a cycle.

Functions

func MarshalSnapshot

func MarshalSnapshot(s *Schema) ([]byte, error)

MarshalSnapshot renders s as canonical JSON: tables sorted by name, columns in declared order, indexes sorted by name, two-space indent.

func SaveSnapshot

func SaveSnapshot(dir string, s *Schema) error

SaveSnapshot writes dir/snapshot.json.

func SchemaFiles

func SchemaFiles(root string) ([]string, error)

SchemaFiles lists the schema files under root, sorted: modules/<name>/schema/schema.go and modules/<name>/schema.go, then database/schema.go.

func SortTables

func SortTables(tables []schema.TableSpec) ([]schema.TableSpec, error)

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

func WriteMigration(dir, name string, now time.Time, res *Result, next *Schema) (string, error)

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 Dialect

type Dialect string

Dialect is the SQL flavor of a generated migration.

const (
	Postgres Dialect = "postgres"
	SQLite   Dialect = "sqlite"
)

The SQL dialects a migration can be generated for.

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.

func ParseSource

func ParseSource(filename string, src []byte, module string) ([]schema.TableSpec, Module, error)

ParseSource reads the tables declared in one schema file. filename is used in error positions; module is recorded as each table's owner.

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 Rename

type Rename struct {
	Table string `json:"table"`
	From  string `json:"from"`
	To    string `json:"to"`
}

Rename says that column From of Table is now called To.

func ParseRename

func ParseRename(s string) (Rename, error)

ParseRename reads "table.old:new".

func (Rename) String

func (r Rename) String() string

String formats r as table.from:to, the form ParseRename reads.

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

func Generate(prev, next *Schema, opts Options) (*Result, error)

Generate diffs prev (the snapshot; nil for none) against next and returns the up and down SQL for opts.Dialect.

func (*Result) Destructive

func (r *Result) Destructive() bool

Destructive reports whether a change drops a table or a column.

func (*Result) Empty

func (r *Result) Empty() bool

Empty reports whether there is nothing to migrate.

func (*Result) File

func (r *Result) File() []byte

File renders the migration file with its +fletch directives.

type Schema

type Schema struct {
	Tables  []schema.TableSpec `json:"tables"`
	Modules []Module           `json:"-"`
}

Schema is every table of a project.

func LoadSnapshot

func LoadSnapshot(dir string) (*Schema, error)

LoadSnapshot reads dir/snapshot.json. A missing file is an empty schema.

func ParseProject

func ParseProject(root string) (*Schema, error)

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

func UnmarshalSnapshot(b []byte) (*Schema, error)

UnmarshalSnapshot reads a snapshot written by MarshalSnapshot.

func (*Schema) Table

func (s *Schema) Table(name string) (*schema.TableSpec, bool)

Table returns the table called name.

func (*Schema) Validate

func (s *Schema) Validate() error

Validate checks table and column names, types, foreign keys, and indexes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL