diff

package
v1.0.36 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package diff provides utilities for comparing database schemas and identifying differences.

Overview

The diff package compares two database models at various granularity levels (database, schema, table, column) and produces detailed reports of differences including:

  • Missing items (present in source but not in target)
  • Extra items (present in target but not in source)
  • Modified items (present in both but with different properties)

Usage

Compare two databases and format the output:

result := diff.CompareDatabases(sourceDB, targetDB)
err := diff.FormatDiff(result, diff.OutputFormatText, os.Stdout)

Output Formats

The package supports multiple output formats:

  • OutputFormatText: Human-readable text format
  • OutputFormatJSON: Structured JSON output
  • OutputFormatYAML: Structured YAML output

Comparison Scope

The comparison covers:

  • Schemas: Name, description, and contents
  • Tables: Name, description, and all sub-elements
  • Columns: Type, nullability, defaults, constraints
  • Indexes: Columns, uniqueness, type
  • Constraints: Type, columns, references
  • Relationships: Type, from/to tables and columns
  • Views: Definition and columns
  • Sequences: Start value, increment, min/max values

Use Cases

  • Schema migration planning
  • Database synchronization verification
  • Change tracking and auditing
  • CI/CD pipeline validation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatDiff

func FormatDiff(result *DiffResult, format OutputFormat, w io.Writer) error

FormatDiff formats the diff result according to the specified format

Types

type ColumnChange

type ColumnChange struct {
	Name    string         `json:"name"`
	Source  *models.Column `json:"source"`
	Target  *models.Column `json:"target"`
	Changes map[string]any `json:"changes"` // Map of field name to what changed
}

ColumnChange represents a modified column

type ColumnDiff

type ColumnDiff struct {
	Missing  []*models.Column `json:"missing"`  // Columns in source but not in target
	Extra    []*models.Column `json:"extra"`    // Columns in target but not in source
	Modified []*ColumnChange  `json:"modified"` // Columns that exist in both but differ
}

ColumnDiff represents differences in columns

type ColumnSummary

type ColumnSummary struct {
	Missing  int `json:"missing"`
	Extra    int `json:"extra"`
	Modified int `json:"modified"`
}

type ConstraintChange

type ConstraintChange struct {
	Name    string             `json:"name"`
	Source  *models.Constraint `json:"source"`
	Target  *models.Constraint `json:"target"`
	Changes map[string]any     `json:"changes"`
}

ConstraintChange represents a modified constraint

type ConstraintDiff

type ConstraintDiff struct {
	Missing  []*models.Constraint `json:"missing"`  // Constraints in source but not in target
	Extra    []*models.Constraint `json:"extra"`    // Constraints in target but not in source
	Modified []*ConstraintChange  `json:"modified"` // Constraints that exist in both but differ
}

ConstraintDiff represents differences in constraints

type ConstraintSummary

type ConstraintSummary struct {
	Missing  int `json:"missing"`
	Extra    int `json:"extra"`
	Modified int `json:"modified"`
}

type DiffResult

type DiffResult struct {
	Source  string      `json:"source"`
	Target  string      `json:"target"`
	Schemas *SchemaDiff `json:"schemas"`
}

DiffResult represents the complete difference analysis between two databases

func CompareDatabases

func CompareDatabases(source, target *models.Database) *DiffResult

CompareDatabases compares two database models and returns the differences

type IndexChange

type IndexChange struct {
	Name    string         `json:"name"`
	Source  *models.Index  `json:"source"`
	Target  *models.Index  `json:"target"`
	Changes map[string]any `json:"changes"`
}

IndexChange represents a modified index

type IndexDiff

type IndexDiff struct {
	Missing  []*models.Index `json:"missing"`  // Indexes in source but not in target
	Extra    []*models.Index `json:"extra"`    // Indexes in target but not in source
	Modified []*IndexChange  `json:"modified"` // Indexes that exist in both but differ
}

IndexDiff represents differences in indexes

type IndexSummary

type IndexSummary struct {
	Missing  int `json:"missing"`
	Extra    int `json:"extra"`
	Modified int `json:"modified"`
}

type OutputFormat

type OutputFormat string

OutputFormat represents the output format for diff results

const (
	FormatSummary OutputFormat = "summary"
	FormatJSON    OutputFormat = "json"
	FormatHTML    OutputFormat = "html"
)

type RelationshipChange

type RelationshipChange struct {
	Name    string               `json:"name"`
	Source  *models.Relationship `json:"source"`
	Target  *models.Relationship `json:"target"`
	Changes map[string]any       `json:"changes"`
}

RelationshipChange represents a modified relationship

type RelationshipDiff

type RelationshipDiff struct {
	Missing  []*models.Relationship `json:"missing"`  // Relationships in source but not in target
	Extra    []*models.Relationship `json:"extra"`    // Relationships in target but not in source
	Modified []*RelationshipChange  `json:"modified"` // Relationships that exist in both but differ
}

RelationshipDiff represents differences in relationships

type RelationshipSummary

type RelationshipSummary struct {
	Missing  int `json:"missing"`
	Extra    int `json:"extra"`
	Modified int `json:"modified"`
}

type SchemaChange

type SchemaChange struct {
	Name      string        `json:"name"`
	Tables    *TableDiff    `json:"tables,omitempty"`
	Views     *ViewDiff     `json:"views,omitempty"`
	Sequences *SequenceDiff `json:"sequences,omitempty"`
}

SchemaChange represents changes within a schema

type SchemaDiff

type SchemaDiff struct {
	Missing  []*models.Schema `json:"missing"`  // Schemas in source but not in target
	Extra    []*models.Schema `json:"extra"`    // Schemas in target but not in source
	Modified []*SchemaChange  `json:"modified"` // Schemas that exist in both but differ
}

SchemaDiff represents differences at the schema level

type SchemaSummary

type SchemaSummary struct {
	Missing  int `json:"missing"`
	Extra    int `json:"extra"`
	Modified int `json:"modified"`
}

type SequenceChange

type SequenceChange struct {
	Name    string           `json:"name"`
	Source  *models.Sequence `json:"source"`
	Target  *models.Sequence `json:"target"`
	Changes map[string]any   `json:"changes"`
}

SequenceChange represents a modified sequence

type SequenceDiff

type SequenceDiff struct {
	Missing  []*models.Sequence `json:"missing"`  // Sequences in source but not in target
	Extra    []*models.Sequence `json:"extra"`    // Sequences in target but not in source
	Modified []*SequenceChange  `json:"modified"` // Sequences that exist in both but differ
}

SequenceDiff represents differences in sequences

type SequenceSummary

type SequenceSummary struct {
	Missing  int `json:"missing"`
	Extra    int `json:"extra"`
	Modified int `json:"modified"`
}

type Summary

type Summary struct {
	Schemas       SchemaSummary       `json:"schemas"`
	Tables        TableSummary        `json:"tables"`
	Columns       ColumnSummary       `json:"columns"`
	Indexes       IndexSummary        `json:"indexes"`
	Constraints   ConstraintSummary   `json:"constraints"`
	Relationships RelationshipSummary `json:"relationships"`
	Views         ViewSummary         `json:"views"`
	Sequences     SequenceSummary     `json:"sequences"`
}

Summary provides counts for quick overview

func ComputeSummary

func ComputeSummary(result *DiffResult) *Summary

ComputeSummary generates a summary with counts from a DiffResult

type TableChange

type TableChange struct {
	Name          string            `json:"name"`
	Schema        string            `json:"schema"`
	Columns       *ColumnDiff       `json:"columns,omitempty"`
	Indexes       *IndexDiff        `json:"indexes,omitempty"`
	Constraints   *ConstraintDiff   `json:"constraints,omitempty"`
	Relationships *RelationshipDiff `json:"relationships,omitempty"`
}

TableChange represents changes within a table

type TableDiff

type TableDiff struct {
	Missing  []*models.Table `json:"missing"`  // Tables in source but not in target
	Extra    []*models.Table `json:"extra"`    // Tables in target but not in source
	Modified []*TableChange  `json:"modified"` // Tables that exist in both but differ
}

TableDiff represents differences in tables

type TableSummary

type TableSummary struct {
	Missing  int `json:"missing"`
	Extra    int `json:"extra"`
	Modified int `json:"modified"`
}

type ViewChange

type ViewChange struct {
	Name    string         `json:"name"`
	Source  *models.View   `json:"source"`
	Target  *models.View   `json:"target"`
	Changes map[string]any `json:"changes"`
}

ViewChange represents a modified view

type ViewDiff

type ViewDiff struct {
	Missing  []*models.View `json:"missing"`  // Views in source but not in target
	Extra    []*models.View `json:"extra"`    // Views in target but not in source
	Modified []*ViewChange  `json:"modified"` // Views that exist in both but differ
}

ViewDiff represents differences in views

type ViewSummary

type ViewSummary struct {
	Missing  int `json:"missing"`
	Extra    int `json:"extra"`
	Modified int `json:"modified"`
}

Jump to

Keyboard shortcuts

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