Documentation
¶
Overview ¶
Package diff defines types for representing schema differences between two database schemas.
Overview ¶
The diff package provides structured representations of schema changes, including additions, removals, and modifications of database objects.
Difference Types ¶
The package defines differences for:
- Tables: Added, removed, renamed
- Columns: Type changes, nullability, defaults
- Indexes: Added, removed, modified
- Foreign keys: Added, removed, modified
- Check constraints: Added, removed, modified
- Views: Definition changes
- Functions/Procedures: Definition changes
- Triggers: Added, removed, modified
- Database-specific objects (sequences, enums, extensions, events)
SchemaDifference Structure ¶
Each difference includes:
- Type: The kind of difference (Added, Removed, Modified)
- Category: The schema object category (Table, Column, Index, etc.)
- Object: The affected object name
- Schema: The schema/namespace (for PostgreSQL)
- OldValue: Previous value (for modifications)
- NewValue: New value (for modifications)
- Message: Human-readable description
- Severity: Impact level (Info, Warning, Critical)
Usage ¶
Differences are typically produced by the comparer package:
diffs, _ := comparer.CompareSchemas(old, new, nil)
for _, diff := range diffs {
fmt.Printf("[%s] %s.%s: %s\n",
diff.Severity, diff.Category, diff.Object, diff.Message)
}
Severity Levels ¶
- Info: Additive changes, no data loss risk
- Warning: Modifications that may require attention
- Critical: Destructive changes with potential data loss
Filtering ¶
Differences can be filtered by:
- Category (e.g., only table changes)
- Severity (e.g., only critical issues)
- Object name patterns (e.g., exclude temp_* tables)
Integration ¶
The diff types are used by:
- Comparer: Produces differences
- Planner: Consumes differences to generate migration plans
- Formatter: Formats differences for human consumption
- Snapshot: Compares snapshots and produces differences
Index ¶
- type CheckConstraintDiff
- type ColumnDiff
- type CommentDiff
- type EnumTypeDiff
- type EventDiff
- type ExtensionDiff
- type ForeignKeyDiff
- type FunctionComparisonResult
- type FunctionDiff
- type IndexDiff
- type MaterializedViewComparisonResult
- type MaterializedViewDiff
- type MetadataDiff
- type MetadataDiffAction
- type PartitionDiff
- type PrimaryKeyDiff
- type ProcedureDiff
- type RuleDiff
- type SchemaDiff
- type SequenceDiff
- type TableDiff
- type TriggerDiff
- type UniqueConstraintDiff
- type ViewComparisonResult
- type ViewDiff
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CheckConstraintDiff ¶
type CheckConstraintDiff struct {
Action MetadataDiffAction
OldConstraint *schemaextract.CheckConstraint
NewConstraint *schemaextract.CheckConstraint
}
CheckConstraintDiff represents changes to a check constraint.
type ColumnDiff ¶
type ColumnDiff struct {
Action MetadataDiffAction
OldColumn *schemaextract.Column
NewColumn *schemaextract.Column
}
ColumnDiff represents changes to a column.
type CommentDiff ¶
type CommentDiff struct {
ObjectType string // TABLE, COLUMN, VIEW, etc.
SchemaName string
ObjectName string
ColumnName string // For column comments
OldComment string
NewComment string
}
CommentDiff represents changes to object comments.
type EnumTypeDiff ¶
type EnumTypeDiff struct {
Action MetadataDiffAction
SchemaName string
EnumName string
OldEnumType *schemaextract.EnumType
NewEnumType *schemaextract.EnumType
// Enum-specific properties
AddedValues []string
RemovedValues []string
OrderChanged bool
}
EnumTypeDiff represents changes to an enum type.
type EventDiff ¶
type EventDiff struct {
Action MetadataDiffAction
EventName string
OldEvent *schemaextract.Event
NewEvent *schemaextract.Event
// Event-specific properties
DefinitionChanged bool
}
EventDiff represents changes to an event (MySQL).
type ExtensionDiff ¶
type ExtensionDiff struct {
Action MetadataDiffAction
SchemaName string
ExtensionName string
OldExtension *schemaextract.Extension
NewExtension *schemaextract.Extension
// Extension-specific properties
VersionChanged bool
}
ExtensionDiff represents changes to an extension (PostgreSQL).
type ForeignKeyDiff ¶
type ForeignKeyDiff struct {
Action MetadataDiffAction
OldForeignKey *schemaextract.ForeignKey
NewForeignKey *schemaextract.ForeignKey
}
ForeignKeyDiff represents changes to a foreign key.
type FunctionComparisonResult ¶
type FunctionComparisonResult struct {
SignatureChanged bool
BodyChanged bool
AttributesChanged bool
CanUseAlterFunction bool
ChangedAttributes []string // List of attribute names that changed
}
FunctionComparisonResult provides detailed information about function changes.
type FunctionDiff ¶
type FunctionDiff struct {
Action MetadataDiffAction
SchemaName string
FunctionName string
OldFunction *schemaextract.Function
NewFunction *schemaextract.Function
// Function-specific properties
SignatureChanged bool // Requires DROP/CREATE
BodyChanged bool // Can use ALTER FUNCTION
AttributesChanged bool // Language, volatility, etc.
CanUseAlterFunction bool // Optimization flag
}
FunctionDiff represents changes to a function.
type IndexDiff ¶
type IndexDiff struct {
Action MetadataDiffAction
OldIndex *schemaextract.Index
NewIndex *schemaextract.Index
}
IndexDiff represents changes to an index.
type MaterializedViewComparisonResult ¶
type MaterializedViewComparisonResult struct {
DefinitionChanged bool
RequiresRecreation bool
ColumnsChanged bool
CommentChanged bool
}
MaterializedViewComparisonResult provides detailed information about materialized view changes.
type MaterializedViewDiff ¶
type MaterializedViewDiff struct {
Action MetadataDiffAction
SchemaName string
ViewName string
OldView *schemaextract.MaterializedView
NewView *schemaextract.MaterializedView
// Materialized view-specific properties
DefinitionChanged bool
}
MaterializedViewDiff represents changes to a materialized view.
type MetadataDiff ¶
type MetadataDiff struct {
DatabaseName string
// Top-level object changes
SchemaChanges []*SchemaDiff
TableChanges []*TableDiff
ViewChanges []*ViewDiff
MaterializedViewChanges []*MaterializedViewDiff
FunctionChanges []*FunctionDiff
ProcedureChanges []*ProcedureDiff
SequenceChanges []*SequenceDiff
EnumTypeChanges []*EnumTypeDiff
EventChanges []*EventDiff
ExtensionChanges []*ExtensionDiff
CommentChanges []*CommentDiff
}
MetadataDiff represents the complete diff between two database schemas.
func (*MetadataDiff) CountChanges ¶
func (d *MetadataDiff) CountChanges() int
CountChanges returns the total number of changes across all categories.
func (*MetadataDiff) IsEmpty ¶
func (d *MetadataDiff) IsEmpty() bool
IsEmpty returns true if there are no differences.
type MetadataDiffAction ¶
type MetadataDiffAction string
MetadataDiffAction represents the type of change.
const ( // MetadataDiffActionCreate indicates an object was created. MetadataDiffActionCreate MetadataDiffAction = "CREATE" // MetadataDiffActionDrop indicates an object was dropped. MetadataDiffActionDrop MetadataDiffAction = "DROP" // MetadataDiffActionAlter indicates an object was altered. MetadataDiffActionAlter MetadataDiffAction = "ALTER" )
type PartitionDiff ¶
type PartitionDiff struct {
Action MetadataDiffAction
OldPartition *schemaextract.Partition
NewPartition *schemaextract.Partition
}
PartitionDiff represents changes to a partition.
type PrimaryKeyDiff ¶
type PrimaryKeyDiff struct {
Action MetadataDiffAction
OldPrimaryKey *schemaextract.Index // Primary key stored as Index with Primary=true
NewPrimaryKey *schemaextract.Index
}
PrimaryKeyDiff represents changes to a primary key.
type ProcedureDiff ¶
type ProcedureDiff struct {
Action MetadataDiffAction
SchemaName string
ProcedureName string
OldProcedure *schemaextract.Procedure
NewProcedure *schemaextract.Procedure
// Procedure-specific properties
SignatureChanged bool
BodyChanged bool
CanUseAlterProcedure bool
}
ProcedureDiff represents changes to a stored procedure.
type RuleDiff ¶
type RuleDiff struct {
Action MetadataDiffAction
OldRule *schemaextract.Rule
NewRule *schemaextract.Rule
}
RuleDiff represents changes to a rule (PostgreSQL).
type SchemaDiff ¶
type SchemaDiff struct {
Action MetadataDiffAction
SchemaName string
OldSchema *schemaextract.Schema
NewSchema *schemaextract.Schema
}
SchemaDiff represents changes to a schema.
type SequenceDiff ¶
type SequenceDiff struct {
Action MetadataDiffAction
SchemaName string
SequenceName string
OldSequence *schemaextract.Sequence
NewSequence *schemaextract.Sequence
// Sequence-specific properties
StartChanged bool
IncrementChanged bool
MinValueChanged bool
MaxValueChanged bool
CacheChanged bool
CycleChanged bool
}
SequenceDiff represents changes to a sequence.
type TableDiff ¶
type TableDiff struct {
Action MetadataDiffAction
SchemaName string
TableName string
OldTable *schemaextract.Table
NewTable *schemaextract.Table
// Sub-object changes (only for ALTER action)
ColumnChanges []*ColumnDiff
IndexChanges []*IndexDiff
PrimaryKeyChanges []*PrimaryKeyDiff
UniqueConstraintChanges []*UniqueConstraintDiff
ForeignKeyChanges []*ForeignKeyDiff
CheckConstraintChanges []*CheckConstraintDiff
PartitionChanges []*PartitionDiff
TriggerChanges []*TriggerDiff
RuleChanges []*RuleDiff
// Property changes
CommentChanged bool
EngineChanged bool
CollationChanged bool
CharsetChanged bool
}
TableDiff represents changes to a table.
func (*TableDiff) HasChanges ¶
HasTableChanges returns true if the table has any changes.
type TriggerDiff ¶
type TriggerDiff struct {
Action MetadataDiffAction
OldTrigger *schemaextract.Trigger
NewTrigger *schemaextract.Trigger
}
TriggerDiff represents changes to a trigger.
type UniqueConstraintDiff ¶
type UniqueConstraintDiff struct {
Action MetadataDiffAction
OldConstraint *schemaextract.Index // Unique constraint stored as Index with Unique=true
NewConstraint *schemaextract.Index
}
UniqueConstraintDiff represents changes to a unique constraint.
type ViewComparisonResult ¶
type ViewComparisonResult struct {
DefinitionChanged bool
RequiresRecreation bool // Some changes require DROP/CREATE instead of ALTER
ColumnsChanged bool
CommentChanged bool
}
ViewComparisonResult provides detailed information about view changes.
type ViewDiff ¶
type ViewDiff struct {
Action MetadataDiffAction
SchemaName string
ViewName string
OldView *schemaextract.View
NewView *schemaextract.View
// View-specific properties
DefinitionChanged bool
}
ViewDiff represents changes to a view.