Documentation
ΒΆ
Overview ΒΆ
Package plugins provides a unified third-party integration system. It allows third-party services (auth providers, ORMs, managed Platforms) to hook into the migration analysis, transformation, and validation pipeline.
Index ΒΆ
- func Register(plugin Plugin) error
- type BaseConsolidator
- type BasePlugin
- func (bp *BasePlugin) DetectAuthPattern(stmt *types.Statement) string
- func (bp *BasePlugin) DetectPatterns(sql string) []Pattern
- func (bp *BasePlugin) EnrichStatement(ctx context.Context, stmt *types.Statement) error
- func (bp *BasePlugin) FixFunctionVolatility(ctx context.Context, functionSQL string) (string, error)
- func (bp *BasePlugin) GetConflictingPlugins() []string
- func (bp *BasePlugin) GetConsolidationRules() []ConsolidationRule
- func (bp *BasePlugin) GetRequiredExtensions() []string
- func (bp *BasePlugin) InjectCompatibilityLayer(ctx context.Context) string
- func (bp *BasePlugin) Name() string
- func (bp *BasePlugin) Priority() int
- func (bp *BasePlugin) ShouldPreserve(stmt *types.Statement) bool
- func (bp *BasePlugin) TransformSQL(ctx context.Context, sql string) (string, error)
- func (bp *BasePlugin) ValidateSchema(ctx context.Context, db *sql.DB) error
- type ConsolidationRule
- type FunctionConsolidator
- type Location
- type Pattern
- type PatternSeverity
- type PatternType
- type Plugin
- type PolicyConsolidator
- func (pc *PolicyConsolidator) AllClausesIdentical(clauses []string) bool
- func (pc *PolicyConsolidator) AllSameObjectName(statements []*types.Statement) bool
- func (pc *PolicyConsolidator) AllSameObjectType(statements []*types.Statement, objectType types.ObjectType) bool
- func (pc *PolicyConsolidator) AllSameTargetTable(statements []*types.Statement) bool
- func (pc *PolicyConsolidator) ExtractPolicyClauses(statements []*types.Statement) ([]string, []string)
- func (pc *PolicyConsolidator) HaveSamePolicyLogic(statements []*types.Statement) bool
- type Registry
- func (r *Registry) ActivePlugins() []Plugin
- func (r *Registry) DiscoverAndInitialize(ctx context.Context, migrations []*types.Migration, config map[string]any) error
- func (r *Registry) EnrichStatement(ctx context.Context, stmt *types.Statement) error
- func (r *Registry) GetConsolidationRules() []ConsolidationRule
- func (r *Registry) GetPlugin(name string) (Plugin, bool)
- func (r *Registry) GetRequiredExtensions() []string
- func (r *Registry) InjectCompatibilityLayer(ctx context.Context) string
- func (r *Registry) IsActive(name string) bool
- func (r *Registry) Register(plugin Plugin) error
- func (r *Registry) Reset()
- func (r *Registry) ShouldPreserve(stmt *types.Statement) bool
- func (r *Registry) TransformSQL(ctx context.Context, sql string) (string, error)
- func (r *Registry) ValidateSchema(ctx context.Context, db *sql.DB) error
- type TableConsolidator
- func (tc *TableConsolidator) AllSameTable(statements []*types.Statement) bool
- func (tc *TableConsolidator) ConservativeMerge(statements []*types.Statement) *types.Statement
- func (tc *TableConsolidator) ContainsKeyword(statements []*types.Statement, keyword string) bool
- func (tc *TableConsolidator) FilterByOperation(statements []*types.Statement, op types.Operation) []*types.Statement
- func (tc *TableConsolidator) HasCreateOperation(statements []*types.Statement) bool
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
Types ΒΆ
type BaseConsolidator ΒΆ
type BaseConsolidator struct {
// contains filtered or unexported fields
}
BaseConsolidator provides common consolidation logic for plugins
func NewBaseConsolidator ΒΆ
func NewBaseConsolidator(name string) *BaseConsolidator
NewBaseConsolidator creates a new base consolidator
type BasePlugin ΒΆ
type BasePlugin struct {
// contains filtered or unexported fields
}
BasePlugin provides default implementations for optional Plugin methods
func NewBasePlugin ΒΆ
func NewBasePlugin(name string, priority int) *BasePlugin
NewBasePlugin creates a base plugin with common defaults
func (*BasePlugin) DetectAuthPattern ΒΆ
func (bp *BasePlugin) DetectAuthPattern(stmt *types.Statement) string
func (*BasePlugin) DetectPatterns ΒΆ
func (bp *BasePlugin) DetectPatterns(sql string) []Pattern
func (*BasePlugin) EnrichStatement ΒΆ
Default implementations (no-ops)
func (*BasePlugin) FixFunctionVolatility ΒΆ
func (*BasePlugin) GetConflictingPlugins ΒΆ
func (bp *BasePlugin) GetConflictingPlugins() []string
func (*BasePlugin) GetConsolidationRules ΒΆ
func (bp *BasePlugin) GetConsolidationRules() []ConsolidationRule
func (*BasePlugin) GetRequiredExtensions ΒΆ
func (bp *BasePlugin) GetRequiredExtensions() []string
func (*BasePlugin) InjectCompatibilityLayer ΒΆ
func (bp *BasePlugin) InjectCompatibilityLayer(ctx context.Context) string
func (*BasePlugin) Name ΒΆ
func (bp *BasePlugin) Name() string
func (*BasePlugin) Priority ΒΆ
func (bp *BasePlugin) Priority() int
func (*BasePlugin) ShouldPreserve ΒΆ
func (bp *BasePlugin) ShouldPreserve(stmt *types.Statement) bool
func (*BasePlugin) TransformSQL ΒΆ
func (*BasePlugin) ValidateSchema ΒΆ
type ConsolidationRule ΒΆ
type ConsolidationRule struct {
Name string // Rule identifier
Priority int // Higher priority overrides conflicts
ObjectType types.ObjectType // Target object type
AuthPattern types.AuthPatternType // Target auth pattern (if applicable)
Conflicts []string // Conflicting rule names
CanMerge func([]*types.Statement) bool // Check if statements can merge
Merge func([]*types.Statement) *types.Statement // Perform merge
}
ConsolidationRule defines how to consolidate plugin-specific statements
type FunctionConsolidator ΒΆ
type FunctionConsolidator struct {
*BaseConsolidator
}
FunctionConsolidator provides common function consolidation helpers
func NewFunctionConsolidator ΒΆ
func NewFunctionConsolidator(name string) *FunctionConsolidator
NewFunctionConsolidator creates a new function consolidator
func (*FunctionConsolidator) AllSameFunctionSignature ΒΆ
func (fc *FunctionConsolidator) AllSameFunctionSignature(statements []*types.Statement) bool
AllSameFunctionSignature checks if all functions have the same signature
type Pattern ΒΆ
type Pattern struct {
Type PatternType // Pattern category
Name string // Pattern identifier (e.g., "clerk_org_function", "prisma_directive")
Location Location // Source location in SQL
Metadata map[string]string // Pattern-specific metadata
Severity PatternSeverity // How important is this pattern
}
Pattern represents a detected plugin-specific pattern in SQL
type PatternSeverity ΒΆ
type PatternSeverity string
PatternSeverity indicates pattern importance
const ( SeverityCritical PatternSeverity = "CRITICAL" // Must preserve (e.g., auth functions) SeverityHigh PatternSeverity = "HIGH" // Should preserve (e.g., RLS policies) SeverityMedium PatternSeverity = "MEDIUM" // Can consolidate carefully SeverityLow PatternSeverity = "LOW" // Safe to consolidate )
type PatternType ΒΆ
type PatternType string
PatternType categorizes detected patterns
const ( PatternTypeAuth PatternType = "AUTH" // Authentication/authorization patterns PatternTypeORM PatternType = "ORM" // ORM-specific syntax PatternTypeSchema PatternType = "SCHEMA" // Schema metadata PatternTypeFunction PatternType = "FUNCTION" // Function definitions PatternTypePolicy PatternType = "POLICY" // RLS/security policies PatternTypeStorage PatternType = "STORAGE" // File storage patterns PatternTypeTrigger PatternType = "TRIGGER" // Trigger definitions PatternTypeExtension PatternType = "EXTENSION" // Extension usage )
type Plugin ΒΆ
type Plugin interface {
// Name returns the unique plugin identifier (e.g., "clerk", "supabase", "prisma")
Name() string
// Priority returns plugin priority for conflict resolution (higher = higher priority)
// Standard priorities:
// - Auth services: 90-100
// - ORMs: 70-89
// - Platforms: 50-69
// - Utilities: 0-49
Priority() int
// Detect analyzes migrations to determine if this plugin is applicable.
// Returns true if plugin should be activated based on migration patterns.
Detect(migrations []*types.Migration) bool
// Initialize configures the plugin with service-specific settings.
// Config is the plugin-specific configuration section from pgsquash.config.json.
Initialize(ctx context.Context, config any) error
// EnrichStatement adds plugin-specific metadata to parsed statements.
// Called after pg_query parsing, before tracking.
// Examples:
// - Mark auth functions with AuthPattern metadata
// - Tag ORM-generated tables with schema preservation flags
// - Add cross-reference information for validation
EnrichStatement(ctx context.Context, stmt *types.Statement) error
// DetectPatterns analyzes SQL text for plugin-specific patterns.
// Returns patterns found (e.g., JWT claim access, Prisma directives).
// Called before pg_query parsing for pre-parse transformations.
DetectPatterns(sql string) []Pattern
// DetectAuthPattern analyzes a statement for authentication patterns.
// Returns the auth pattern identifier (e.g., "clerk_jwt_v2", "supabase_rls")
// or empty string if no auth pattern is detected.
// This allows plugins to claim ownership of auth-related statements.
DetectAuthPattern(stmt *types.Statement) string
// TransformSQL modifies SQL statements for compatibility or optimization.
// Called before pg_query parsing to fix syntax issues.
// Examples:
// - Add STABLE markers to auth functions
// - Convert Prisma @@ directives to PostgreSQL equivalents
// - Fix ORM-specific syntax
TransformSQL(ctx context.Context, sql string) (string, error)
// InjectCompatibilityLayer returns SQL to execute before migrations.
// Used to create mock functions, roles, or schemas for validation.
// Examples:
// - Clerk: CREATE SCHEMA auth; CREATE FUNCTION auth.jwt() ...
// - Supabase: CREATE ROLE authenticated; CREATE FUNCTION auth.uid() ...
// - NextAuth: CREATE TABLE accounts, sessions, users, verification_tokens ...
InjectCompatibilityLayer(ctx context.Context) string
// FixFunctionVolatility adds IMMUTABLE/STABLE/VOLATILE markers to functions.
// Service-specific implementation based on function patterns.
// Returns modified SQL with volatility markers added.
FixFunctionVolatility(ctx context.Context, functionSQL string) (string, error)
// ValidateSchema performs plugin-specific schema validation.
// Called after standard validation with access to database connection.
// Examples:
// - Verify RLS policies reference correct auth functions
// - Check Prisma shadow database compatibility
// - Validate ORM adapter table schemas
ValidateSchema(ctx context.Context, db *sql.DB) error
// GetRequiredExtensions returns PostgreSQL extensions required by this plugin.
// Examples:
// - Supabase: []string{"uuid-ossp"}
// - Vector DBs: []string{"vector"}
GetRequiredExtensions() []string
// GetConsolidationRules returns plugin-specific consolidation rules.
// These rules are merged with standard consolidation rules.
// Higher priority plugins override conflicting rules.
GetConsolidationRules() []ConsolidationRule
// ShouldPreserve determines if a statement should never be consolidated.
// Returns true to mark statement as critical (e.g., auth functions, ORM metadata).
// Examples:
// - Preserve Clerk JWT v2 organization functions
// - Preserve Prisma migration metadata tables
// - Preserve NextAuth session management triggers
ShouldPreserve(stmt *types.Statement) bool
// GetConflictingPlugins returns names of plugins that conflict with this one.
// Used for conflict resolution when multiple plugins want to handle same pattern.
GetConflictingPlugins() []string
}
Plugin represents a third-party service integration that can hook into the migration processing pipeline at multiple points.
Lifecycle:
- Registry.Detect() checks all migrations to find applicable plugins
- Plugin.Initialize() is called with service-specific configuration
- Parser calls EnrichStatement() to add metadata
- Transformation calls TransformSQL() to modify SQL
- Validation calls InjectCompatibilityLayer() and ValidateSchema()
- Squasher uses GetConsolidationRules() and ShouldPreserve()
func ResolveConflicts ΒΆ
ResolveConflicts applies priority-based conflict resolution to an arbitrary set of plugins and returns the plugins that remain active, ordered by priority (highest first).
This is the exact resolution logic the registry applies during DiscoverAndInitialize: plugins are visited in descending priority order and each surviving plugin excludes every plugin named by its GetConflictingPlugins(). It is exported so public API surfaces (pkg/plugins) can report compatibility identically to the squashing pipeline.
type PolicyConsolidator ΒΆ
type PolicyConsolidator struct {
*BaseConsolidator
}
PolicyConsolidation provides common policy consolidation helpers
func NewPolicyConsolidator ΒΆ
func NewPolicyConsolidator(name string) *PolicyConsolidator
NewPolicyConsolidator creates a new policy consolidator
func (*PolicyConsolidator) AllClausesIdentical ΒΆ
func (pc *PolicyConsolidator) AllClausesIdentical(clauses []string) bool
AllClausesIdentical checks if all clauses in a list are identical
func (*PolicyConsolidator) AllSameObjectName ΒΆ
func (pc *PolicyConsolidator) AllSameObjectName(statements []*types.Statement) bool
AllSameObjectName checks if all statements have the same object name
func (*PolicyConsolidator) AllSameObjectType ΒΆ
func (pc *PolicyConsolidator) AllSameObjectType(statements []*types.Statement, objectType types.ObjectType) bool
AllSameObjectType checks if all statements are of the same type
func (*PolicyConsolidator) AllSameTargetTable ΒΆ
func (pc *PolicyConsolidator) AllSameTargetTable(statements []*types.Statement) bool
AllSameTargetTable checks if all policy statements target the same table
func (*PolicyConsolidator) ExtractPolicyClauses ΒΆ
func (pc *PolicyConsolidator) ExtractPolicyClauses(statements []*types.Statement) ([]string, []string)
ExtractPolicyClauses extracts USING and WITH CHECK clauses from policies
func (*PolicyConsolidator) HaveSamePolicyLogic ΒΆ
func (pc *PolicyConsolidator) HaveSamePolicyLogic(statements []*types.Statement) bool
HaveSamePolicyLogic checks if policies have the same security logic
type Registry ΒΆ
type Registry struct {
// contains filtered or unexported fields
}
Registry manages the lifecycle of all plugins
func GlobalRegistry ΒΆ
func GlobalRegistry() *Registry
GlobalRegistry returns the global plugin registry
func (*Registry) ActivePlugins ΒΆ
ActivePlugins returns the list of currently active plugins (sorted by priority)
func (*Registry) DiscoverAndInitialize ΒΆ
func (r *Registry) DiscoverAndInitialize(ctx context.Context, migrations []*types.Migration, config map[string]any) error
DiscoverAndInitialize detects applicable plugins from migrations and initializes them This is the main entry point called by the squashing engine
func (*Registry) EnrichStatement ΒΆ
EnrichStatement calls EnrichStatement on all active plugins Plugins are called in priority order (highest first)
func (*Registry) GetConsolidationRules ΒΆ
func (r *Registry) GetConsolidationRules() []ConsolidationRule
GetConsolidationRules aggregates consolidation rules from all active plugins
func (*Registry) GetRequiredExtensions ΒΆ
GetRequiredExtensions aggregates required extensions from all active plugins
func (*Registry) InjectCompatibilityLayer ΒΆ
InjectCompatibilityLayer aggregates compatibility SQL from all active plugins
func (*Registry) Register ΒΆ
Register adds a plugin to the registry.
Registration is idempotent by plugin name: registering a plugin whose name is already present is a no-op. This allows every entry point (CLI main, pkg/engine construction, library callers) to ensure the default plugin set is registered without coordinating a single init site.
func (*Registry) Reset ΒΆ
func (r *Registry) Reset()
Reset clears all active plugins (useful for testing)
func (*Registry) ShouldPreserve ΒΆ
ShouldPreserve checks if any active plugin wants to preserve this statement
func (*Registry) TransformSQL ΒΆ
TransformSQL calls TransformSQL on all active plugins Plugins are called in priority order, each transforming the output of the previous
type TableConsolidator ΒΆ
type TableConsolidator struct {
*BaseConsolidator
}
TableConsolidator provides common table consolidation helpers
func NewTableConsolidator ΒΆ
func NewTableConsolidator(name string) *TableConsolidator
NewTableConsolidator creates a new table consolidator
func (*TableConsolidator) AllSameTable ΒΆ
func (tc *TableConsolidator) AllSameTable(statements []*types.Statement) bool
AllSameTable checks if all statements target the same table
func (*TableConsolidator) ConservativeMerge ΒΆ
func (tc *TableConsolidator) ConservativeMerge(statements []*types.Statement) *types.Statement
ConservativeMerge returns the first statement (conservative approach)
func (*TableConsolidator) ContainsKeyword ΒΆ
func (tc *TableConsolidator) ContainsKeyword(statements []*types.Statement, keyword string) bool
ContainsKeyword checks if any statement contains a specific keyword
func (*TableConsolidator) FilterByOperation ΒΆ
func (tc *TableConsolidator) FilterByOperation(statements []*types.Statement, op types.Operation) []*types.Statement
FilterByOperation filters statements by operation type
func (*TableConsolidator) HasCreateOperation ΒΆ
func (tc *TableConsolidator) HasCreateOperation(statements []*types.Statement) bool
HasCreateOperation checks if any statement is a CREATE operation
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
Package auth provides shared authentication compatibility layer generation for validation and testing.
|
Package auth provides shared authentication compatibility layer generation for validation and testing. |
|
Package clerk provides Clerk authentication integration for pgsquash.
|
Package clerk provides Clerk authentication integration for pgsquash. |
|
Package drizzle provides Drizzle ORM integration for pgsquash.
|
Package drizzle provides Drizzle ORM integration for pgsquash. |
|
Package prisma provides Prisma ORM integration for pgsquash.
|
Package prisma provides Prisma ORM integration for pgsquash. |
|
Package supabase provides Supabase Platform integration for pgsquash.
|
Package supabase provides Supabase Platform integration for pgsquash. |
|
Package volatility provides shared function volatility marker detection and fixing.
|
Package volatility provides shared function volatility marker detection and fixing. |