transformation

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: 17 Imported by: 0

Documentation

Overview

Package transformation provides SQL transformation and rollback management. It handles generation of rollback scripts, backup creation, and safe transformation operations for PostgreSQL migration squashing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackupConfig

type BackupConfig struct {
	Type           BackupType   `json:"type"`
	Format         BackupFormat `json:"format"`
	Compression    bool         `json:"compression"`
	VerboseOutput  bool         `json:"verbose_output"`
	IncludeDrops   bool         `json:"include_drops"`
	SchemaOnly     bool         `json:"schema_only"`
	DataOnly       bool         `json:"data_only"`
	InsertFormat   bool         `json:"insert_format"`
	ColumnInserts  bool         `json:"column_inserts"`
	IfExists       bool         `json:"if_exists"`
	CreateDatabase bool         `json:"create_database"`
	CleanFirst     bool         `json:"clean_first"`
	Encoding       string       `json:"encoding"`
	ExcludeTables  []string     `json:"exclude_tables"`
	IncludeTables  []string     `json:"include_tables"`
	ExcludeSchemas []string     `json:"exclude_schemas"`
	IncludeSchemas []string     `json:"include_schemas"`
}

BackupConfig controls backup generation behavior

func DefaultBackupConfig

func DefaultBackupConfig() *BackupConfig

DefaultBackupConfig returns sensible backup defaults

type BackupFormat

type BackupFormat int

BackupFormat defines the output format for backups

const (
	SQLFormat BackupFormat = iota
	CustomFormat
	TarFormat
	DirectoryFormat
)

type BackupGenerator

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

BackupGenerator handles database backup and rollback generation

func NewBackupGenerator

func NewBackupGenerator(config *BackupConfig, db *sql.DB) *BackupGenerator

NewBackupGenerator creates a new backup generator.

The default working directory is a dedicated pgsquash-backups directory under the system temp dir - never the shared temp dir itself, because CleanupOldBackups glob-deletes "*backup*.sql" inside the working directory and must only ever touch files pgsquash created. Callers that want backups somewhere durable (e.g. <output>/.backups, as the squasher engine enforces) use SetWorkingDirectory.

func (*BackupGenerator) CleanupOldBackups

func (bg *BackupGenerator) CleanupOldBackups(maxAge time.Duration, maxCount int) error

CleanupOldBackups removes old backup files based on retention policy

func (*BackupGenerator) GeneratePostMigrationBackup

func (bg *BackupGenerator) GeneratePostMigrationBackup(ctx context.Context, dbURL string) (*BackupResult, error)

GeneratePostMigrationBackup creates a backup after applying migrations

func (*BackupGenerator) GeneratePreMigrationBackup

func (bg *BackupGenerator) GeneratePreMigrationBackup(ctx context.Context, dbURL string) (*BackupResult, error)

GeneratePreMigrationBackup creates a backup before applying migrations

func (*BackupGenerator) GenerateRollbackScript

func (bg *BackupGenerator) GenerateRollbackScript(ctx context.Context, statements []types.Statement) ([]*RollbackScript, error)

GenerateRollbackScript creates rollback scripts for migrations

func (*BackupGenerator) SetWorkingDirectory

func (bg *BackupGenerator) SetWorkingDirectory(dir string) error

SetWorkingDirectory sets the working directory for backup operations

func (*BackupGenerator) ValidateBackup

func (bg *BackupGenerator) ValidateBackup(ctx context.Context, backupPath string) error

ValidateBackup verifies that a backup can be restored

type BackupResult

type BackupResult struct {
	BackupPath      string        `json:"backup_path"`
	Size            int64         `json:"size"`
	Duration        time.Duration `json:"duration"`
	TablesBackedUp  int           `json:"tables_backed_up"`
	SchemasBackedUp int           `json:"schemas_backed_up"`
	Success         bool          `json:"success"`
	Error           string        `json:"error,omitempty"`
	Warnings        []string      `json:"warnings"`
}

BackupResult represents the result of a backup operation

type BackupType

type BackupType int

BackupType defines the type of backup to generate

const (
	SchemaOnly BackupType = iota
	DataOnly
	SchemaAndData
	Structure
	DDLOnly
)

type RollbackExecution

type RollbackExecution struct {
	PlanID        string          `json:"plan_id"`
	StartedAt     time.Time       `json:"started_at"`
	CompletedAt   *time.Time      `json:"completed_at,omitempty"`
	Status        RollbackStatus  `json:"status"`
	Progress      int             `json:"progress"` // Percentage
	CurrentScript int             `json:"current_script"`
	Results       []*ScriptResult `json:"results"`
	Error         string          `json:"error,omitempty"`
	Context       map[string]any  `json:"context"`
}

RollbackExecution tracks the execution of a rollback plan

type RollbackManager

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

RollbackManager handles rollback plan creation and execution

func NewRollbackManager

func NewRollbackManager(db *sql.DB, workDir string) *RollbackManager

NewRollbackManager creates a new rollback manager

func (*RollbackManager) CreateRollbackPlan

func (rm *RollbackManager) CreateRollbackPlan(ctx context.Context, name string, statements []types.Statement) (*RollbackPlan, error)

CreateRollbackPlan generates a comprehensive rollback plan for migrations

func (*RollbackManager) DeleteRollbackPlan

func (rm *RollbackManager) DeleteRollbackPlan(planID string) error

DeleteRollbackPlan removes a rollback plan

func (*RollbackManager) ExecuteRollbackPlan

func (rm *RollbackManager) ExecuteRollbackPlan(ctx context.Context, planID string, dryRun bool) (*RollbackExecution, error)

ExecuteRollbackPlan executes a rollback plan

func (*RollbackManager) GetRollbackExecution

func (rm *RollbackManager) GetRollbackExecution(executionID string) (*RollbackExecution, error)

GetRollbackExecution retrieves a specific rollback execution

func (*RollbackManager) GetRollbackPlan

func (rm *RollbackManager) GetRollbackPlan(planID string) (*RollbackPlan, error)

GetRollbackPlan retrieves a specific rollback plan

func (*RollbackManager) ListRollbackPlans

func (rm *RollbackManager) ListRollbackPlans() []*RollbackPlan

ListRollbackPlans returns all available rollback plans

func (*RollbackManager) LoadAllPlans

func (rm *RollbackManager) LoadAllPlans() error

LoadAllPlans loads all rollback plans from disk

func (*RollbackManager) ValidateRollbackPlan

func (rm *RollbackManager) ValidateRollbackPlan(ctx context.Context, planID string) error

ValidateRollbackPlan validates that a rollback plan is executable

type RollbackMetadata

type RollbackMetadata struct {
	SourceMigration   string            `json:"source_migration"`
	DatabaseVersion   string            `json:"database_version"`
	SchemaChecksum    string            `json:"schema_checksum"`
	EstimatedDuration time.Duration     `json:"estimated_duration"`
	Dependencies      []string          `json:"dependencies"`
	Warnings          []string          `json:"warnings"`
	Tags              map[string]string `json:"tags"`
}

RollbackMetadata contains additional information about the rollback

type RollbackPlan

type RollbackPlan struct {
	ID          string            `json:"id"`
	Name        string            `json:"name"`
	Description string            `json:"description"`
	CreatedAt   time.Time         `json:"created_at"`
	Scripts     []*RollbackScript `json:"scripts"`
	Metadata    *RollbackMetadata `json:"metadata"`
	Status      RollbackStatus    `json:"status"`
}

RollbackPlan represents a complete rollback strategy

type RollbackScript

type RollbackScript struct {
	ID           string    `json:"id"`
	Description  string    `json:"description"`
	SQL          string    `json:"sql"`
	CreatedAt    time.Time `json:"created_at"`
	Order        int       `json:"order"`
	Dependencies []string  `json:"dependencies"`
}

RollbackScript represents a rollback operation

type RollbackStatus

type RollbackStatus int

RollbackStatus tracks the execution status of rollback plans

const (
	RollbackPending RollbackStatus = iota
	RollbackInProgress
	RollbackCompleted
	RollbackFailed
	RollbackCancelled
)

type SQLTransformer

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

SQLTransformer handles SQL transformations for safety and compatibility

func NewSQLTransformer

func NewSQLTransformer(config *TransformationConfig) *SQLTransformer

NewSQLTransformer creates a new SQL transformer

func (*SQLTransformer) BatchTransform

func (st *SQLTransformer) BatchTransform(ctx context.Context, statements []string) ([]*TransformationResult, error)

BatchTransform processes multiple SQL statements

func (*SQLTransformer) Transform

func (st *SQLTransformer) Transform(ctx context.Context, sql string) (*TransformationResult, error)

Transform applies configured transformations to SQL

Transformation Pipeline Order: 1. Function volatility markers (pre-parse, regex-based) 2. SQL parsing with pg_query (AST generation) 3. AST-dependent transformations (DML to SELECT, etc.)

Note: Volatility fix runs BEFORE parsing because: - Doesn't require AST (uses regex pattern matching) - pg_query parser can fail on complex/large migration files (9000+ lines) - Critical for index predicate compatibility

func (*SQLTransformer) ValidateTransformation

func (st *SQLTransformer) ValidateTransformation(original, transformed string) error

ValidateTransformation checks if transformation preserves semantic meaning

type ScriptResult

type ScriptResult struct {
	ScriptID     string        `json:"script_id"`
	StartedAt    time.Time     `json:"started_at"`
	CompletedAt  *time.Time    `json:"completed_at,omitempty"`
	Success      bool          `json:"success"`
	Error        string        `json:"error,omitempty"`
	Duration     time.Duration `json:"duration"`
	RowsAffected int64         `json:"rows_affected"`
	Output       string        `json:"output,omitempty"`
}

ScriptResult tracks the result of executing a single rollback script

type TransformationApplied

type TransformationApplied struct {
	Type        TransformationType `json:"type"`
	Description string             `json:"description"`
	LineStart   int                `json:"line_start"`
	LineEnd     int                `json:"line_end"`
	Before      string             `json:"before"`
	After       string             `json:"after"`
}

TransformationApplied tracks what transformations were applied

type TransformationConfig

type TransformationConfig struct {
	EnableDMLToSelect    bool   `json:"enable_dml_to_select"`
	EnableDropToComment  bool   `json:"enable_drop_to_comment"`
	EnableUnsafeToSafe   bool   `json:"enable_unsafe_to_safe"`
	EnableModernSyntax   bool   `json:"enable_modern_syntax"`
	EnablePerformance    bool   `json:"enable_performance"`
	EnableSyntaxFixes    bool   `json:"enable_syntax_fixes"` // Fix common SQL syntax errors
	PreserveSrcPositions bool   `json:"preserve_src_positions"`
	TargetVersion        string `json:"target_version"` // PostgreSQL version target
}

TransformationConfig controls SQL transformation behavior

func DefaultTransformationConfig

func DefaultTransformationConfig() *TransformationConfig

DefaultTransformationConfig returns sensible defaults

type TransformationResult

type TransformationResult struct {
	OriginalSQL     string                  `json:"original_sql"`
	TransformedSQL  string                  `json:"transformed_sql"`
	Transformations []TransformationApplied `json:"transformations"`
	Warnings        []string                `json:"warnings"`
	Success         bool                    `json:"success"`
	Error           string                  `json:"error,omitempty"`
}

TransformationResult represents the result of a transformation

type TransformationType

type TransformationType int

TransformationType defines the type of SQL transformation

const (
	DMLToSelect TransformationType = iota
	DropToComment
	UnsafeToSafe
	ModernSyntax
	Performance
)

Jump to

Keyboard shortcuts

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