diff

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package diff compares GORM models with a live database and writes the difference as migration files (the same .up.sql / .down.sql format migrate.FromFS reads).

It detects new tables, added/removed/renamed columns and added/removed indexes. It deliberately does NOT detect column type or nullability changes (type names differ per database and would cause false positives); those are reported as warnings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TypeName

func TypeName(name string) string

TypeName converts snake_case ("create_users_table") to the Go type name ("CreateUsersTable").

Types

type ColumnDef

type ColumnDef struct {
	Name       string
	Kind       dialect.Kind
	Length     int
	Precision  int
	Scale      int
	Unsigned   bool
	NotNull    bool
	Primary    bool
	Auto       bool
	Big        bool
	HasDefault bool
	Default    any
}

ColumnDef is a database-agnostic column description.

type IndexDef

type IndexDef struct {
	Name    string
	Columns []string
	Unique  bool
}

type Op

type Op struct {
	Kind   OpKind
	Table  string
	Def    *TableDef  // CreateTable
	Column *ColumnDef // AddColumn
	From   string     // RenameColumn old name; DropColumn name
	To     string     // RenameColumn new name
	Index  *IndexDef  // AddIndex / DropIndex
	Note   string     // human hint, rendered as a SQL comment in the down file
}

Op is one planned change.

type OpKind

type OpKind string
const (
	CreateTable  OpKind = "create_table"
	AddColumn    OpKind = "add_column"
	DropColumn   OpKind = "drop_column"
	RenameColumn OpKind = "rename_column"
	AddIndex     OpKind = "add_index"
	DropIndex    OpKind = "drop_index"
)

type Options

type Options struct {
	// AllowDestructive includes DropColumn and DropIndex in Ops.
	AllowDestructive bool
	// Renames maps "table.old_column" to "new_column" so a rename is not planned as drop+add.
	Renames map[string]string
}

Options tune Generate.

type Plan

type Plan struct {
	Ops []Op
	// Blocked holds destructive ops (drops) that were not applied because AllowDestructive is false.
	Blocked  []Op
	Warnings []string
	// contains filtered or unexported fields
}

Plan is the result of Generate.

func Generate

func Generate(ctx context.Context, db *migro.DB, opts Options, models ...any) (Plan, error)

Generate computes the plan for models (pass pointers or values, e.g. &User{}).

func (Plan) Empty

func (p Plan) Empty() bool

Empty reports whether there is nothing to apply.

func (Plan) GoSource

func (p Plan) GoSource(pkg, typeName string) (string, error)

GoSource renders the plan as a Go migration file body for package pkg.

func (Plan) Render

func (p Plan) Render() (up, down []string, err error)

Render returns the up and down SQL. Down statements are in reverse order; changes that cannot be reversed (dropped columns) appear as SQL comments.

func (Plan) WriteFiles

func (p Plan) WriteFiles(dir, name string) ([]string, error)

WriteFiles writes <timestamp>_<name>.up.sql and .down.sql into dir and returns their paths. An empty plan writes nothing and returns (nil, nil).

func (Plan) WriteGoFile

func (p Plan) WriteGoFile(dir, pkg, name string, now time.Time) (string, error)

WriteGoFile writes <timestamp>_<name>.go into dir (package pkg) and returns its path. An empty plan writes nothing and returns "".

type TableDef

type TableDef struct {
	Name    string
	Columns []ColumnDef
	Primary []string // composite primary key columns (len > 1)
	Uniques [][]string
	Indexes []IndexDef
}

Jump to

Keyboard shortcuts

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