engine

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package engine provides database-specific comparison engines for MySQL and PostgreSQL schema comparison.

Overview

This package defines the ComparisonEngine interface and provides concrete implementations for MySQL and PostgreSQL. Each engine understands the specific nuances of its target database system.

Architecture

The engine package follows a pluggable architecture:

  • Interfaces define the contract for comparison engines
  • Base package provides shared comparison utilities
  • MySQL and PostgreSQL packages provide database-specific implementations
  • Registry manages engine selection based on database type

Creating Custom Engines

To create a custom comparison engine:

type CustomEngine struct {
    // Implement ComparisonEngine interface
}

func (e *CustomEngine) CompareSchemas(old, new *schemaextract.DatabaseSchema) ([]*diff.SchemaDifference, error) {
    // Custom comparison logic
}

// Register the engine
engine.RegisterEngine("custom", &CustomEngine{})

Database-Specific Handling

Each engine handles database-specific features:

  • MySQL: ENGINE, AUTO_INCREMENT, FULLTEXT indexes, events
  • PostgreSQL: Extensions, sequences, enum types, materialized views

Thread Safety

Engine instances are safe for concurrent use. The registry is thread-safe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Has

func Has(engine Engine) bool

Has checks if a comparer is registered in the global registry.

func Register

func Register(engine Engine, comparer Comparer)

Register registers a comparer in the global registry.

func Unregister

func Unregister(engine Engine)

Unregister removes a comparer from the global registry.

Types

type ColumnComparer

type ColumnComparer interface {
	// CompareColumns compares two columns and returns detailed differences.
	CompareColumns(oldCol, newCol *schemaextract.Column) (*diff.ColumnDiff, error)

	// IsEquivalentType checks if two column types are semantically equivalent.
	// For example, in PostgreSQL: INTEGER = INT, CHARACTER VARYING = VARCHAR.
	IsEquivalentType(type1, type2 string) bool

	// NormalizeType normalizes a column type to a standard form.
	NormalizeType(colType string) string
}

ColumnComparer provides engine-specific column comparison logic.

type Comparer

type Comparer interface {
	// GetEngine returns the engine type this comparer is for.
	GetEngine() Engine

	// Expression returns the expression comparer.
	Expression() ExpressionComparer

	// Index returns the index comparer.
	Index() IndexComparer

	// Function returns the function comparer.
	Function() FunctionComparer

	// View returns the view comparer.
	View() ViewComparer

	// Column returns the column comparer.
	Column() ColumnComparer

	// Procedure returns the procedure comparer.
	Procedure() ProcedureComparer

	// Sequence returns the sequence comparer.
	Sequence() SequenceComparer
}

Comparer is the main interface that aggregates all engine-specific comparers.

func Get

func Get(engine Engine) (Comparer, error)

Get retrieves a comparer from the global registry.

type Engine

type Engine string

Engine represents a database engine type.

const (
	// MySQL represents MySQL database engine.
	MySQL Engine = "MYSQL"
	// PostgreSQL represents PostgreSQL database engine.
	PostgreSQL Engine = "POSTGRES"
	// MariaDB represents MariaDB database engine.
	MariaDB Engine = "MARIADB"
	// TiDB represents TiDB database engine.
	TiDB Engine = "TIDB"
	// OceanBase represents OceanBase database engine.
	OceanBase Engine = "OCEANBASE"
	// Unknown represents an unknown database engine.
	Unknown Engine = "UNKNOWN"
)

func ListEngines

func ListEngines() []Engine

ListEngines returns all registered engines from the global registry.

func ParseEngine

func ParseEngine(s string) Engine

ParseEngine parses a string into an Engine type.

func (Engine) IsCaseSensitive

func (e Engine) IsCaseSensitive() bool

IsCaseSensitive returns true if the engine is case-sensitive for object names. MySQL, MariaDB, TiDB, and OceanBase are case-insensitive for column names and other details.

func (Engine) IsMySQL

func (e Engine) IsMySQL() bool

IsMySQL returns true if the engine is MySQL-like (MySQL, MariaDB, TiDB, OceanBase).

func (Engine) IsPostgreSQL

func (e Engine) IsPostgreSQL() bool

IsPostgreSQL returns true if the engine is PostgreSQL.

func (Engine) String

func (e Engine) String() string

String returns the string representation of the engine.

type ExpressionComparer

type ExpressionComparer interface {
	// CompareExpressionsSemantically compares two expressions semantically.
	// Returns true if they are semantically equivalent, false otherwise.
	CompareExpressionsSemantically(expr1, expr2 string) (bool, error)

	// NormalizeExpression normalizes an expression to a standard form.
	// This removes engine-specific quirks like type casts, extra parentheses, etc.
	NormalizeExpression(expr string) string
}

ExpressionComparer provides semantic comparison of database expressions. Different engines may have different expression syntaxes that are semantically equivalent.

type FunctionComparer

type FunctionComparer interface {
	// Equal checks if two functions are identical.
	Equal(oldFunc, newFunc *schemaextract.Function) bool

	// CompareDetailed provides detailed comparison result with specific change types.
	CompareDetailed(oldFunc, newFunc *schemaextract.Function) (*diff.FunctionComparisonResult, error)

	// GetSignature extracts the function signature (name + parameters).
	GetSignature(function *schemaextract.Function) string

	// CanUseAlterFunction determines if ALTER FUNCTION can be used instead of DROP/CREATE.
	CanUseAlterFunction(oldFunc, newFunc *schemaextract.Function) bool
}

FunctionComparer provides engine-specific function comparison logic.

type IndexComparer

type IndexComparer interface {
	// CompareIndexes compares two indexes and returns detailed differences.
	CompareIndexes(oldIndex, newIndex *schemaextract.Index) (*diff.IndexDiff, error)

	// CompareIndexWhereConditions compares WHERE clauses of partial indexes.
	CompareIndexWhereConditions(def1, def2 string) bool

	// ExtractWhereClauseFromIndexDef extracts the WHERE clause from an index definition.
	ExtractWhereClauseFromIndexDef(definition string) string
}

IndexComparer provides engine-specific index comparison logic.

type ProcedureComparer

type ProcedureComparer interface {
	// CompareProcedures compares two procedures and returns detailed differences.
	CompareProcedures(oldProc, newProc *schemaextract.Procedure) (*diff.ProcedureDiff, error)

	// CanUseAlterProcedure determines if ALTER PROCEDURE can be used.
	CanUseAlterProcedure(oldProc, newProc *schemaextract.Procedure) bool
}

ProcedureComparer provides engine-specific stored procedure comparison logic.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds the registered comparers for each engine.

func GetRegistry

func GetRegistry() *Registry

GetRegistry returns the global registry instance. This is useful for testing or custom registry scenarios.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new comparer registry.

func (*Registry) Get

func (r *Registry) Get(engine Engine) (Comparer, error)

Get retrieves the comparer for a specific engine. Returns an error if no comparer is registered for the engine.

func (*Registry) Has

func (r *Registry) Has(engine Engine) bool

Has checks if a comparer is registered for the given engine.

func (*Registry) ListEngines

func (r *Registry) ListEngines() []Engine

ListEngines returns all registered engines.

func (*Registry) Register

func (r *Registry) Register(engine Engine, comparer Comparer)

Register registers a comparer for a specific engine. If a comparer is already registered for the engine, it will be replaced.

func (*Registry) Unregister

func (r *Registry) Unregister(engine Engine)

Unregister removes the comparer for a specific engine.

type SequenceComparer

type SequenceComparer interface {
	// CompareSequences compares two sequences and returns detailed differences.
	CompareSequences(oldSeq, newSeq *schemaextract.Sequence) (*diff.SequenceDiff, error)
}

SequenceComparer provides engine-specific sequence comparison logic.

type ViewComparer

type ViewComparer interface {
	// CompareView compares two views and returns detailed differences.
	CompareView(oldView, newView *schemaextract.View) (*diff.ViewComparisonResult, error)

	// CompareMaterializedView compares two materialized views and returns detailed differences.
	CompareMaterializedView(oldMV, newMV *schemaextract.MaterializedView) (*diff.MaterializedViewComparisonResult, error)

	// RequiresRecreation determines if a view change requires DROP/CREATE instead of ALTER.
	RequiresRecreation(oldView, newView *schemaextract.View) bool
}

ViewComparer provides engine-specific view comparison logic.

Directories

Path Synopsis
Package defaultcomparer provides shared comparison utilities and base implementations for database schema comparison engines.
Package defaultcomparer provides shared comparison utilities and base implementations for database schema comparison engines.
Package mysql provides MySQL-specific schema comparison engine.
Package mysql provides MySQL-specific schema comparison engine.
Package postgres provides PostgreSQL-specific schema comparison engine.
Package postgres provides PostgreSQL-specific schema comparison engine.

Jump to

Keyboard shortcuts

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