consolidation

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterCoreRules

func RegisterCoreRules(registry *RuleRegistry) error

RegisterCoreRules registers all core consolidation rules with metadata

Types

type AdvancedColumnLifecycleRule

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

AdvancedColumnLifecycleRule handles complex column evolution patterns with edge cases

func NewAdvancedColumnLifecycleRule

func NewAdvancedColumnLifecycleRule() *AdvancedColumnLifecycleRule

NewAdvancedColumnLifecycleRule creates a new advanced column lifecycle rule

func (*AdvancedColumnLifecycleRule) Apply

Apply applies the advanced column lifecycle rule

func (*AdvancedColumnLifecycleRule) CanApply

func (r *AdvancedColumnLifecycleRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply determines if this rule can be applied to the lifecycle

func (*AdvancedColumnLifecycleRule) Risk

Risk returns the risk level for advanced column lifecycle operations

type ColumnChange

type ColumnChange struct {
	Name      string
	Operation string // ADD, DROP, ALTER, RENAME
	DataType  string
	Default   string
	NotNull   bool
	Order     int
}

ColumnChange tracks a column modification operation

type ColumnConstraint

type ColumnConstraint struct {
	Type       ConstraintType `json:"type"`
	Name       string         `json:"name"`
	Definition string         `json:"definition"`
	TableLevel bool           `json:"table_level"`
}

ColumnConstraint represents a constraint on a column

type ColumnConstraintInfo

type ColumnConstraintInfo struct {
	Name            string
	Type            ConstraintType
	Definition      string
	AffectedColumns []string
	TableLevel      bool
}

ColumnConstraintInfo represents parsed constraint information for column lifecycle tracking

type ColumnEvolutionRule

type ColumnEvolutionRule struct{}

ColumnEvolutionRule handles complex column lifecycle patterns

func (*ColumnEvolutionRule) Apply

Apply applies the consolidation rule to the given lifecycle

func (*ColumnEvolutionRule) CanApply

func (r *ColumnEvolutionRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply checks if the rule can be applied to the given lifecycle

func (*ColumnEvolutionRule) Risk

Risk returns the risk level for this rule

type ColumnLifecycleState

type ColumnLifecycleState struct {
	Name            string                 `json:"name"`
	OriginalName    string                 `json:"original_name"`
	DataType        string                 `json:"data_type"`
	IsNullable      bool                   `json:"is_nullable"`
	DefaultValue    string                 `json:"default_value"`
	Constraints     []ColumnConstraint     `json:"constraints"`
	Position        int                    `json:"position"`
	Transformations []ColumnTransformation `json:"transformations"`
	Status          ColumnStatus           `json:"status"`
	Dependencies    []string               `json:"dependencies"`
}

ColumnLifecycleState represents the complete state of a column through its lifecycle

type ColumnOperation

type ColumnOperation string

ColumnOperation represents operations that can be performed on columns

const (
	ColumnOpAdd            ColumnOperation = "ADD"
	ColumnOpDrop           ColumnOperation = "DROP"
	ColumnOpRename         ColumnOperation = "RENAME"
	ColumnOpChangeType     ColumnOperation = "CHANGE_TYPE"
	ColumnOpSetDefault     ColumnOperation = "SET_DEFAULT"
	ColumnOpDropDefault    ColumnOperation = "DROP_DEFAULT"
	ColumnOpSetNotNull     ColumnOperation = "SET_NOT_NULL"
	ColumnOpDropNotNull    ColumnOperation = "DROP_NOT_NULL"
	ColumnOpAddConstraint  ColumnOperation = "ADD_CONSTRAINT"
	ColumnOpDropConstraint ColumnOperation = "DROP_CONSTRAINT"
)

type ColumnStatus

type ColumnStatus string

ColumnStatus represents the current status of a column

const (
	ColumnStatusActive    ColumnStatus = "ACTIVE"
	ColumnStatusDropped   ColumnStatus = "DROPPED"
	ColumnStatusRenamed   ColumnStatus = "RENAMED"
	ColumnStatusTransient ColumnStatus = "TRANSIENT" // Added and dropped in same lifecycle
)

type ColumnTransformation

type ColumnTransformation struct {
	Operation  ColumnOperation `json:"operation"`
	OldValue   string          `json:"old_value"`
	NewValue   string          `json:"new_value"`
	AtSequence int             `json:"at_sequence"`
	SQL        string          `json:"sql"`
	HasDataOps bool            `json:"has_data_ops"`
}

ColumnTransformation tracks changes to a column

type ConditionalSchemaRule

type ConditionalSchemaRule struct{}

ConditionalSchemaRule consolidates conditional schema operations (IF NOT EXISTS, CREATE OR REPLACE)

func (*ConditionalSchemaRule) Apply

Apply applies the consolidation rule to the given lifecycle

func (*ConditionalSchemaRule) CanApply

func (r *ConditionalSchemaRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply checks if the rule can be applied to the given lifecycle

func (*ConditionalSchemaRule) Risk

Risk returns the risk level for this rule

type ConditionalState

type ConditionalState struct {
	ShouldExist  bool
	UseReplace   bool
	FinalSQL     string
	Dependencies []string
	WasCreated   bool
}

ConditionalState represents the final desired state after analyzing conditional operations

type ConflictPolicy

type ConflictPolicy string

ConflictPolicy defines how to handle rule conflicts

const (
	ConflictPolicyHighestPriority ConflictPolicy = "highest_priority" // Use rule with highest priority
	ConflictPolicyFirstRegistered ConflictPolicy = "first_registered" // Use first registered rule
	ConflictPolicyError           ConflictPolicy = "error"            // Return error on conflict
)

type ConsolidationEngine

type ConsolidationEngine interface {
	GetTracker() *tracking.Tracker
	GetConfig() *config.Config
	GetSafetyLevel() string // Returns the current safety level (paranoid, conservative, standard, aggressive)
}

ConsolidationEngine interface for the engine that applies consolidation rules

type ConsolidationRule

type ConsolidationRule interface {
	CanApply(lifecycle *tracking.ObjectLifecycle) bool
	Apply(lifecycle *tracking.ObjectLifecycle, engine ConsolidationEngine) (*tracking.ConsolidationResult, error)
	Risk() tracking.RiskLevel
}

ConsolidationRule interface for consolidation rules used by the squasher

type ConsolidationRuleEngine

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

ConsolidationRuleEngine manages and applies consolidation rules

func NewConsolidationRuleEngine

func NewConsolidationRuleEngine() *ConsolidationRuleEngine

NewConsolidationRuleEngine creates a new rule engine with all extracted rules

func (*ConsolidationRuleEngine) AddRule

func (cre *ConsolidationRuleEngine) AddRule(rule ConsolidationRule)

AddRule adds an explicitly ordered rule to the engine.

func (*ConsolidationRuleEngine) ApplyRules

ApplyRules applies all applicable rules to a lifecycle If no rules apply, returns a default consolidation that preserves the original SQL

func (*ConsolidationRuleEngine) GetApplicableRules

func (cre *ConsolidationRuleEngine) GetApplicableRules(lifecycle *tracking.ObjectLifecycle) []ConsolidationRule

GetApplicableRules returns all rules that can be applied to a lifecycle

func (*ConsolidationRuleEngine) GetRegistry

func (cre *ConsolidationRuleEngine) GetRegistry() *RuleRegistry

GetRegistry returns the rule registry.

func (*ConsolidationRuleEngine) Rules

Rules returns the explicitly ordered rules registered on this engine. The returned slice is a copy; mutating it does not affect the engine.

type ConstraintType

type ConstraintType string

ConstraintType represents different types of column constraints

const (
	ConstraintPrimaryKey ConstraintType = "PRIMARY_KEY"
	ConstraintForeignKey ConstraintType = "FOREIGN_KEY"
	ConstraintUnique     ConstraintType = "UNIQUE"
	ConstraintCheck      ConstraintType = "CHECK"
	ConstraintDefault    ConstraintType = "DEFAULT"
	ConstraintNotNull    ConstraintType = "NOT_NULL"
)

type CreateAlterConsolidationRule

type CreateAlterConsolidationRule struct{}

CreateAlterConsolidationRule consolidates CREATE statements followed by ALTER statements

func (*CreateAlterConsolidationRule) Apply

Apply applies the consolidation rule to the given lifecycle

func (*CreateAlterConsolidationRule) CanApply

CanApply checks if the rule can be applied to the given lifecycle

func (*CreateAlterConsolidationRule) Risk

Risk returns the risk level for this rule

type DOBlockAlterTableRule

type DOBlockAlterTableRule struct{}

DOBlockAlterTableRule extracts ALTER TABLE statements from DO blocks This allows ALTER statements wrapped in IF NOT EXISTS checks to be consolidated with their corresponding CREATE TABLE statements

func (*DOBlockAlterTableRule) Apply

Apply applies the consolidation rule to the given lifecycle

func (*DOBlockAlterTableRule) CanApply

func (r *DOBlockAlterTableRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply checks if the rule can be applied to the given lifecycle

func (*DOBlockAlterTableRule) Risk

Risk returns the risk level for this rule

type DOBlockEnumTypeRule

type DOBlockEnumTypeRule struct{}

DOBlockEnumTypeRule consolidates DO blocks that create ENUM types

func (*DOBlockEnumTypeRule) Apply

Apply applies the consolidation rule to the given lifecycle

func (*DOBlockEnumTypeRule) CanApply

func (r *DOBlockEnumTypeRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply checks if the rule can be applied to the given lifecycle

func (*DOBlockEnumTypeRule) Risk

Risk returns the risk level for this rule

type DropCreateCycleRule

type DropCreateCycleRule struct{}

DropCreateCycleRule handles DROP followed by CREATE consolidation This includes tables and views (DROP VIEW + CREATE VIEW → CREATE OR REPLACE VIEW)

func (*DropCreateCycleRule) Apply

Apply applies the consolidation rule to the given lifecycle

func (*DropCreateCycleRule) CanApply

func (r *DropCreateCycleRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply checks if the rule can be applied to the given lifecycle

func (*DropCreateCycleRule) Risk

Risk returns the risk level for this rule

type EnumDeduplicationRule

type EnumDeduplicationRule struct{}

EnumDeduplicationRule detects and resolves duplicate ENUM type definitions This handles cases where multiple ENUMs with similar names or conflicting values exist

func (*EnumDeduplicationRule) Apply

Apply applies the consolidation rule to the given lifecycle

func (*EnumDeduplicationRule) CanApply

func (r *EnumDeduplicationRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply checks if the rule can be applied to the given lifecycle

func (*EnumDeduplicationRule) Risk

Risk returns the risk level for this rule

type ErrorRecoveryRule

type ErrorRecoveryRule struct {
	MaxRetries     int
	RecoveryMode   string // "conservative", "aggressive", "fallback"
	ValidateSQL    bool
	LogFailures    bool
	FailureMetrics map[string]int
	// contains filtered or unexported fields
}

ErrorRecoveryRule provides enhanced error recovery and validation for consolidation failures

func NewErrorRecoveryRule

func NewErrorRecoveryRule(maxRetries int, recoveryMode string, validateSQL bool) *ErrorRecoveryRule

NewErrorRecoveryRule creates a new error recovery rule with specified configuration

func (*ErrorRecoveryRule) Apply

Apply implements the ConsolidationRule interface with error recovery

func (*ErrorRecoveryRule) CanApply

func (rule *ErrorRecoveryRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply returns true for all objects to provide universal error recovery

func (*ErrorRecoveryRule) Risk

func (rule *ErrorRecoveryRule) Risk() tracking.RiskLevel

Risk returns the risk level for error recovery rule

type ExternalDependencyFilterRule

type ExternalDependencyFilterRule struct {
	ExternalSchemas map[string]bool
	ExternalTables  map[string]bool
}

ExternalDependencyFilterRule filters out dependencies on external schemas

func NewExternalDependencyFilterRule

func NewExternalDependencyFilterRule() *ExternalDependencyFilterRule

NewExternalDependencyFilterRule creates a new rule with default external dependencies

func (*ExternalDependencyFilterRule) Apply

Apply applies the consolidation rule to the given lifecycle

func (*ExternalDependencyFilterRule) CanApply

CanApply checks if the rule can be applied to the given lifecycle

func (*ExternalDependencyFilterRule) Risk

Risk returns the risk level for this rule

type FunctionDeduplicationRule

type FunctionDeduplicationRule struct{}

FunctionDeduplicationRule consolidates duplicate function definitions

func (*FunctionDeduplicationRule) Apply

Apply applies the consolidation rule to the given lifecycle

func (*FunctionDeduplicationRule) CanApply

func (r *FunctionDeduplicationRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply checks if the rule can be applied to the given lifecycle

func (*FunctionDeduplicationRule) Risk

Risk returns the risk level for this rule

type MultipleCreateConsolidationRule

type MultipleCreateConsolidationRule struct{}

MultipleCreateConsolidationRule handles multiple CREATE statements for the same object

func (*MultipleCreateConsolidationRule) Apply

Apply applies the consolidation rule to the given lifecycle

func (*MultipleCreateConsolidationRule) CanApply

CanApply checks if the rule can be applied to the given lifecycle

func (*MultipleCreateConsolidationRule) Risk

Risk returns the risk level for this rule

type PublicationDeduplicationRule

type PublicationDeduplicationRule struct{}

PublicationDeduplicationRule removes duplicate ALTER PUBLICATION ADD TABLE statements This prevents "relation X is already member of publication Y" errors during deployment

func (*PublicationDeduplicationRule) Apply

Apply applies the rule to deduplicate publication member additions

func (*PublicationDeduplicationRule) CanApply

CanApply checks if this rule can be applied to the given lifecycle

func (*PublicationDeduplicationRule) Risk

Risk returns the risk level of this consolidation rule

type RLSConsolidationRule

type RLSConsolidationRule struct{}

RLSConsolidationRule consolidates Row Level Security operations

func (*RLSConsolidationRule) Apply

Apply applies the consolidation rule to the given lifecycle

func (*RLSConsolidationRule) CanApply

func (r *RLSConsolidationRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply checks if the rule can be applied to the given lifecycle

func (*RLSConsolidationRule) Risk

Risk returns the risk level for this rule

type RegisteredRule

type RegisteredRule struct {
	Rule     ConsolidationRule
	Metadata RuleMetadata
}

RegisteredRule wraps a ConsolidationRule with metadata

type RegistryStats

type RegistryStats struct {
	TotalRules      int
	EnabledRules    int
	DisabledRules   int
	RulesByCategory map[RuleCategory]int
	RulesByProvider map[string]int
}

RegistryStats provides statistics about the registry

type RuleCategory

type RuleCategory string

RuleCategory represents the category of a consolidation rule

const (
	CategoryTableOps     RuleCategory = "table_operations"    // Table CREATE/ALTER consolidation
	CategoryFunctionOps  RuleCategory = "function_operations" // Function deduplication
	CategoryDeadCode     RuleCategory = "dead_code"           // Dead code removal
	CategorySecurity     RuleCategory = "security"            // RLS, policies, auth
	CategoryOptimization RuleCategory = "optimization"        // Performance optimizations
	CategoryExtension    RuleCategory = "extension"           // Extension-specific rules
	CategoryPluginAuth   RuleCategory = "plugin_auth"         // Plugin authentication patterns
	CategoryPluginORM    RuleCategory = "plugin_orm"          // ORM-specific patterns
)

type RuleMetadata

type RuleMetadata struct {
	Name        string       // Unique rule name (e.g., "create_alter_consolidation")
	Description string       // Human-readable description
	Category    RuleCategory // Rule category for organization
	Priority    int          // Execution priority (higher = executed first)
	Provider    string       // Provider name (e.g., "core", "supabase", "clerk")
	Tags        []string     // Tags for filtering (e.g., "aggressive", "safe", "auth")
	Enabled     bool         // Whether rule is enabled
	Version     string       // Rule version for compatibility
}

RuleMetadata provides descriptive information about a consolidation rule

type RuleRegistry

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

RuleRegistry manages dynamic rule registration with priorities and filtering

func GetRegistry

func GetRegistry() *RuleRegistry

GetRegistry returns the global rule registry (singleton)

func NewRuleRegistry

func NewRuleRegistry(conflictPolicy ConflictPolicy) *RuleRegistry

NewRuleRegistry creates a new rule registry

func (*RuleRegistry) Clear

func (r *RuleRegistry) Clear()

Clear removes all rules from the registry

func (*RuleRegistry) DisableRule

func (r *RuleRegistry) DisableRule(name string) error

DisableRule disables a rule

func (*RuleRegistry) EnableRule

func (r *RuleRegistry) EnableRule(name string) error

EnableRule enables a rule

func (*RuleRegistry) GetAllRules

func (r *RuleRegistry) GetAllRules() []*RegisteredRule

GetAllRules returns all registered rules sorted by priority (descending)

func (*RuleRegistry) GetApplicableRules

func (r *RuleRegistry) GetApplicableRules(lifecycle *tracking.ObjectLifecycle) []*RegisteredRule

GetApplicableRules returns all enabled rules that can apply to a lifecycle

func (*RuleRegistry) GetEnabledRules

func (r *RuleRegistry) GetEnabledRules() []*RegisteredRule

GetEnabledRules returns all enabled rules sorted by priority

func (*RuleRegistry) GetRule

func (r *RuleRegistry) GetRule(name string) (*RegisteredRule, error)

GetRule retrieves a registered rule by name

func (*RuleRegistry) GetRulesByCategory

func (r *RuleRegistry) GetRulesByCategory(category RuleCategory) []*RegisteredRule

GetRulesByCategory returns all rules in a category sorted by priority

func (*RuleRegistry) GetRulesByProvider

func (r *RuleRegistry) GetRulesByProvider(provider string) []*RegisteredRule

GetRulesByProvider returns all rules from a provider sorted by priority

func (*RuleRegistry) GetRulesByTag

func (r *RuleRegistry) GetRulesByTag(tag string) []*RegisteredRule

GetRulesByTag returns all rules with a specific tag

func (*RuleRegistry) GetStats

func (r *RuleRegistry) GetStats() RegistryStats

GetStats returns registry statistics

func (*RuleRegistry) Register

func (r *RuleRegistry) Register(rule ConsolidationRule, metadata RuleMetadata) error

Register registers a rule with metadata

func (*RuleRegistry) Unregister

func (r *RuleRegistry) Unregister(name string) error

Unregister removes a rule from the registry

type SeparateAlterRule

type SeparateAlterRule struct{}

SeparateAlterRule identifies ALTER statements that cannot be integrated into CREATE TABLE and must remain as separate statements. This includes RLS operations, column modifications, renames, owner changes, and schema changes.

This rule is critical for maintaining PostgreSQL execution order requirements: - RLS can only be enabled AFTER table exists - Column modifications require existing columns - Renames/ownership changes are administrative operations

func (*SeparateAlterRule) Apply

Apply separates ALTER statements that cannot be integrated into CREATE TABLE

func (*SeparateAlterRule) CanApply

func (r *SeparateAlterRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply checks if the lifecycle has ALTER statements that must remain separate

func (*SeparateAlterRule) Risk

Risk returns the risk level for this rule

type TransactionBoundaryRule

type TransactionBoundaryRule struct{}

TransactionBoundaryRule optimizes transaction boundaries for better performance

func (*TransactionBoundaryRule) Apply

Apply applies the consolidation rule to the given lifecycle Delegates to squasher.TransactionPlanner for transaction grouping (single source of truth)

func (*TransactionBoundaryRule) CanApply

func (r *TransactionBoundaryRule) CanApply(lifecycle *tracking.ObjectLifecycle) bool

CanApply checks if the rule can be applied to the given lifecycle

func (*TransactionBoundaryRule) Risk

Risk returns the risk level for this rule

Jump to

Keyboard shortcuts

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