diff

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package diff compares two IR catalogs and produces an ordered migration Plan whose forward (UpSQL) and inverse (DownSQL) DDL can be rendered.

The diff is DB-free: it operates purely on *irv1.Catalog values (typically produced by internal/parse + internal/catalog.Build) and emits PostgreSQL DDL strings. This file defines the Change interface, the Plan container and the concrete change types for the object kinds handled in this task (schemas, types, sequences, tables, columns, constraints, indexes, foreign keys, views, materialized views, functions, procedures and triggers).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AddColumn

type AddColumn struct {
	Table  *irv1.Table
	Column *irv1.Column
}

AddColumn adds a column to a table.

func (*AddColumn) DownSQL

func (c *AddColumn) DownSQL() string

func (*AddColumn) UpSQL

func (c *AddColumn) UpSQL() string

type AddConstraint

type AddConstraint struct {
	Table      *irv1.Table
	Constraint *irv1.Constraint
}

AddConstraint adds a table constraint (PK/UNIQUE/FK/CHECK/EXCLUSION).

func (*AddConstraint) DownSQL

func (c *AddConstraint) DownSQL() string

func (*AddConstraint) UpSQL

func (c *AddConstraint) UpSQL() string

type AddEnumValue

type AddEnumValue struct {
	Enum  *irv1.EnumType
	Value string
	// After is the label this value should be inserted after; empty appends.
	After string
}

AddEnumValue adds a single label to an existing enum (irreversible in PG).

func (*AddEnumValue) DownSQL

func (c *AddEnumValue) DownSQL() string

DownSQL is a no-op with a warning: PostgreSQL cannot drop an enum label.

func (*AddEnumValue) UpSQL

func (c *AddEnumValue) UpSQL() string

type AlterColumnType

type AlterColumnType struct {
	Table  *irv1.Table
	Column string
	From   *irv1.TypeRef
	To     *irv1.TypeRef
}

AlterColumnType changes a column's type using a USING cast.

func (*AlterColumnType) DownSQL

func (c *AlterColumnType) DownSQL() string

func (*AlterColumnType) UpSQL

func (c *AlterColumnType) UpSQL() string

type Change

type Change interface {
	// UpSQL renders the forward DDL for this change.
	UpSQL() string
	// DownSQL renders the inverse DDL for this change.
	DownSQL() string
	// contains filtered or unexported methods
}

Change is a single reversible schema change.

type CreateComposite

type CreateComposite struct{ Composite *irv1.CompositeType }

CreateComposite creates a composite type.

func (*CreateComposite) DownSQL

func (c *CreateComposite) DownSQL() string

func (*CreateComposite) UpSQL

func (c *CreateComposite) UpSQL() string

type CreateDomain

type CreateDomain struct{ Domain *irv1.DomainType }

CreateDomain creates a domain type.

func (*CreateDomain) DownSQL

func (c *CreateDomain) DownSQL() string

func (*CreateDomain) UpSQL

func (c *CreateDomain) UpSQL() string

type CreateEnum

type CreateEnum struct{ Enum *irv1.EnumType }

CreateEnum creates an enum type.

func (*CreateEnum) DownSQL

func (c *CreateEnum) DownSQL() string

func (*CreateEnum) UpSQL

func (c *CreateEnum) UpSQL() string

type CreateFunction

type CreateFunction struct{ Function *irv1.Function }

CreateFunction creates a function.

func (*CreateFunction) DownSQL

func (c *CreateFunction) DownSQL() string

func (*CreateFunction) UpSQL

func (c *CreateFunction) UpSQL() string

type CreateIndex

type CreateIndex struct {
	Table  *irv1.Table
	Index  *irv1.Index
	Schema string
}

CreateIndex creates an index on a table.

func (*CreateIndex) DownSQL

func (c *CreateIndex) DownSQL() string

func (*CreateIndex) UpSQL

func (c *CreateIndex) UpSQL() string

type CreateMatView

type CreateMatView struct{ View *irv1.MaterializedView }

CreateMatView creates a materialized view.

func (*CreateMatView) DownSQL

func (c *CreateMatView) DownSQL() string

func (*CreateMatView) UpSQL

func (c *CreateMatView) UpSQL() string

type CreateProcedure

type CreateProcedure struct{ Procedure *irv1.Procedure }

CreateProcedure creates a procedure.

func (*CreateProcedure) DownSQL

func (c *CreateProcedure) DownSQL() string

func (*CreateProcedure) UpSQL

func (c *CreateProcedure) UpSQL() string

type CreateRange

type CreateRange struct{ Range *irv1.RangeType }

CreateRange creates a range type.

func (*CreateRange) DownSQL

func (c *CreateRange) DownSQL() string

func (*CreateRange) UpSQL

func (c *CreateRange) UpSQL() string

type CreateSchema

type CreateSchema struct{ Schema *irv1.Schema }

CreateSchema creates a schema.

func (*CreateSchema) DownSQL

func (c *CreateSchema) DownSQL() string

func (*CreateSchema) UpSQL

func (c *CreateSchema) UpSQL() string

type CreateSequence

type CreateSequence struct{ Sequence *irv1.Sequence }

CreateSequence creates a sequence.

func (*CreateSequence) DownSQL

func (c *CreateSequence) DownSQL() string

func (*CreateSequence) UpSQL

func (c *CreateSequence) UpSQL() string

type CreateTable

type CreateTable struct{ Table *irv1.Table }

CreateTable creates a table with its columns and inline primary key.

func (*CreateTable) DownSQL

func (c *CreateTable) DownSQL() string

func (*CreateTable) UpSQL

func (c *CreateTable) UpSQL() string

type CreateTrigger

type CreateTrigger struct{ Trigger *irv1.Trigger }

CreateTrigger creates a trigger.

func (*CreateTrigger) DownSQL

func (c *CreateTrigger) DownSQL() string

func (*CreateTrigger) UpSQL

func (c *CreateTrigger) UpSQL() string

type CreateView

type CreateView struct{ View *irv1.View }

CreateView creates a view.

func (*CreateView) DownSQL

func (c *CreateView) DownSQL() string

func (*CreateView) UpSQL

func (c *CreateView) UpSQL() string

type DropColumn

type DropColumn struct {
	Table  *irv1.Table
	Column *irv1.Column
}

DropColumn drops a column. The inverse re-adds it best-effort with a data-loss warning.

func (*DropColumn) DownSQL

func (c *DropColumn) DownSQL() string

func (*DropColumn) UpSQL

func (c *DropColumn) UpSQL() string

type DropComposite

type DropComposite struct{ Composite *irv1.CompositeType }

DropComposite drops a composite type.

func (*DropComposite) DownSQL

func (c *DropComposite) DownSQL() string

func (*DropComposite) UpSQL

func (c *DropComposite) UpSQL() string

type DropConstraint

type DropConstraint struct {
	Table      *irv1.Table
	Constraint *irv1.Constraint
}

DropConstraint drops a table constraint. The inverse re-adds it.

func (*DropConstraint) DownSQL

func (c *DropConstraint) DownSQL() string

func (*DropConstraint) UpSQL

func (c *DropConstraint) UpSQL() string

type DropDefault

type DropDefault struct {
	Table  *irv1.Table
	Column string
	Old    *irv1.Expr // previous default, for DownSQL
}

DropDefault removes a column default.

func (*DropDefault) DownSQL

func (c *DropDefault) DownSQL() string

func (*DropDefault) UpSQL

func (c *DropDefault) UpSQL() string

type DropDomain

type DropDomain struct{ Domain *irv1.DomainType }

DropDomain drops a domain type.

func (*DropDomain) DownSQL

func (c *DropDomain) DownSQL() string

func (*DropDomain) UpSQL

func (c *DropDomain) UpSQL() string

type DropEnum

type DropEnum struct{ Enum *irv1.EnumType }

DropEnum drops an enum type.

func (*DropEnum) DownSQL

func (c *DropEnum) DownSQL() string

func (*DropEnum) UpSQL

func (c *DropEnum) UpSQL() string

type DropFunction

type DropFunction struct{ Function *irv1.Function }

DropFunction drops a function. The inverse re-creates it from its body.

func (*DropFunction) DownSQL

func (c *DropFunction) DownSQL() string

func (*DropFunction) UpSQL

func (c *DropFunction) UpSQL() string

type DropIndex

type DropIndex struct {
	Table  *irv1.Table
	Index  *irv1.Index
	Schema string
}

DropIndex drops an index. The inverse re-creates it (non-lossy).

func (*DropIndex) DownSQL

func (c *DropIndex) DownSQL() string

func (*DropIndex) UpSQL

func (c *DropIndex) UpSQL() string

type DropMatView

type DropMatView struct{ View *irv1.MaterializedView }

DropMatView drops a materialized view. The inverse re-creates it (WITH NO DATA on the catalog form; data is not part of the schema).

func (*DropMatView) DownSQL

func (c *DropMatView) DownSQL() string

func (*DropMatView) UpSQL

func (c *DropMatView) UpSQL() string

type DropNotNull

type DropNotNull struct {
	Table  *irv1.Table
	Column string
}

DropNotNull removes a NOT NULL constraint (nullable false -> true).

func (*DropNotNull) DownSQL

func (c *DropNotNull) DownSQL() string

func (*DropNotNull) UpSQL

func (c *DropNotNull) UpSQL() string

type DropProcedure

type DropProcedure struct{ Procedure *irv1.Procedure }

DropProcedure drops a procedure. The inverse re-creates it from its body.

func (*DropProcedure) DownSQL

func (c *DropProcedure) DownSQL() string

func (*DropProcedure) UpSQL

func (c *DropProcedure) UpSQL() string

type DropRange

type DropRange struct{ Range *irv1.RangeType }

DropRange drops a range type.

func (*DropRange) DownSQL

func (c *DropRange) DownSQL() string

func (*DropRange) UpSQL

func (c *DropRange) UpSQL() string

type DropSchema

type DropSchema struct{ Schema *irv1.Schema }

DropSchema drops a schema.

func (*DropSchema) DownSQL

func (c *DropSchema) DownSQL() string

func (*DropSchema) UpSQL

func (c *DropSchema) UpSQL() string

type DropSequence

type DropSequence struct{ Sequence *irv1.Sequence }

DropSequence drops a sequence.

func (*DropSequence) DownSQL

func (c *DropSequence) DownSQL() string

func (*DropSequence) UpSQL

func (c *DropSequence) UpSQL() string

type DropTable

type DropTable struct{ Table *irv1.Table }

DropTable drops a table. The inverse re-creates it best-effort with a data-loss warning.

func (*DropTable) DownSQL

func (c *DropTable) DownSQL() string

func (*DropTable) UpSQL

func (c *DropTable) UpSQL() string

type DropTrigger

type DropTrigger struct{ Trigger *irv1.Trigger }

DropTrigger drops a trigger. The inverse re-creates it from its definition.

func (*DropTrigger) DownSQL

func (c *DropTrigger) DownSQL() string

func (*DropTrigger) UpSQL

func (c *DropTrigger) UpSQL() string

type DropView

type DropView struct{ View *irv1.View }

DropView drops a view. The inverse re-creates it from its definition.

func (*DropView) DownSQL

func (c *DropView) DownSQL() string

func (*DropView) UpSQL

func (c *DropView) UpSQL() string

type Plan

type Plan struct {
	Changes []Change
}

Plan is an ordered set of schema changes.

func Diff

func Diff(from, to *irv1.Catalog) (*Plan, error)

Diff compares two catalogs and produces an ordered Plan of changes that turns `from` into `to`. It handles schemas, types (enum/domain/composite/range), sequences, tables, columns, constraints, indexes, foreign keys, views, materialized views, functions, procedures and triggers.

Iteration is deterministic: object ids are sorted before processing, so the resulting plan is stable for a given input pair.

func (*Plan) DownSQL

func (p *Plan) DownSQL() string

DownSQL renders the inverse migration: the exact reverse of the up order, emitting each change's DownSQL. Reversing the up order (rather than re-sorting descending) is required for correctness when a single logical change expands to an ordered pair in the same bucket — e.g. a changed constraint emits [Drop(old), Add(new)] on up, whose correct inverse is [Drop(new).Down=Add(new)... ] reversed to [Add(new).Down=Drop(new), Drop(old).Down=Add(old)], i.e. drop the new then re-add the old.

func (*Plan) Empty

func (p *Plan) Empty() bool

Empty reports whether the plan has no changes.

func (*Plan) UpSQL

func (p *Plan) UpSQL() string

UpSQL renders the forward migration: changes in ascending sortKey order, with each non-empty UpSQL joined by newlines.

type ReplaceFunction

type ReplaceFunction struct {
	From *irv1.Function
	To   *irv1.Function
}

ReplaceFunction replaces a function via CREATE OR REPLACE FUNCTION. The inverse restores the previous definition the same way.

func (*ReplaceFunction) DownSQL

func (c *ReplaceFunction) DownSQL() string

func (*ReplaceFunction) UpSQL

func (c *ReplaceFunction) UpSQL() string

type ReplaceProcedure

type ReplaceProcedure struct {
	From *irv1.Procedure
	To   *irv1.Procedure
}

ReplaceProcedure replaces a procedure via CREATE OR REPLACE PROCEDURE.

func (*ReplaceProcedure) DownSQL

func (c *ReplaceProcedure) DownSQL() string

func (*ReplaceProcedure) UpSQL

func (c *ReplaceProcedure) UpSQL() string

type ReplaceView

type ReplaceView struct {
	From *irv1.View
	To   *irv1.View
}

ReplaceView replaces a view's definition via CREATE OR REPLACE VIEW. The inverse restores the previous definition the same way.

func (*ReplaceView) DownSQL

func (c *ReplaceView) DownSQL() string

func (*ReplaceView) UpSQL

func (c *ReplaceView) UpSQL() string

type SetDefault

type SetDefault struct {
	Table   *irv1.Table
	Column  string
	Default *irv1.Expr // new default
	Old     *irv1.Expr // previous default, for DownSQL (nil => DROP DEFAULT)
}

SetDefault sets (or changes) a column default.

func (*SetDefault) DownSQL

func (c *SetDefault) DownSQL() string

func (*SetDefault) UpSQL

func (c *SetDefault) UpSQL() string

type SetNotNull

type SetNotNull struct {
	Table  *irv1.Table
	Column string
}

SetNotNull adds a NOT NULL constraint to a column (nullable true -> false).

func (*SetNotNull) DownSQL

func (c *SetNotNull) DownSQL() string

func (*SetNotNull) UpSQL

func (c *SetNotNull) UpSQL() string

Jump to

Keyboard shortcuts

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