Skip to content

Commit

Permalink
fix: sync changes of generator.go with latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
thelissimus-work committed Feb 25, 2025
1 parent 86d78be commit 6bc614c
Showing 1 changed file with 13 additions and 33 deletions.
46 changes: 13 additions & 33 deletions pkg/schema/diff/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ import (

// Generator handles creation of migration files from detected changes
type Generator struct {
dialect dialect.Dialect
outputDir string
options GeneratorOptions
logger *logrus.Logger
tableDependencies map[string][]string // tracks table -> dependencies
processedTables map[string]bool // tracks which tables have been processed
processedChanges map[string]struct{} // tracks all processed objects by name
dialect dialect.Dialect
outputDir string
templates map[ChangeType]string
options GeneratorOptions
logger *logrus.Logger
}

type GeneratorOptions struct {
Expand Down Expand Up @@ -170,10 +168,6 @@ func (g *Generator) Generate(changes *ChangeSet) error {
return fmt.Errorf("failed to create output directory: %w", err)
}

// Sort changes based on dependencies
sortedChanges := g.sortChangesByDependencies(deduplicatedChanges)
changes.Changes = sortedChanges

// Create timestamp-based filename
timestamp := time.Now().Unix()
fileName := fmt.Sprintf("changes-%d.sql", timestamp)
Expand Down Expand Up @@ -215,7 +209,7 @@ func (g *Generator) Generate(changes *ChangeSet) error {

// Write the migration file
if err := os.WriteFile(filePath, []byte(content.String()), 0644); err != nil {
return fmt.Errorf("failed to write migration file %q: %w", filePath, err)
return fmt.Errorf("failed to write migration file: %w", err)
}

// Generate down migration if enabled
Expand Down Expand Up @@ -465,10 +459,6 @@ func (g *Generator) generateChangeStatement(change *Change) (string, error) {
return fmt.Sprintf("ALTER TABLE %s ADD CONSTRAINT %s %s;",
change.ObjectName, change.Object.Name, def), nil
}

case DropTable:
g.logger.Debugf("Generating DROP TABLE statement for %s", change.ObjectName)
return fmt.Sprintf("DROP TABLE IF EXISTS %s CASCADE;", change.ObjectName), nil
}

return "", fmt.Errorf("unsupported change type or missing data: %v", change.Type)
Expand Down Expand Up @@ -598,21 +588,9 @@ func (g *Generator) generateDownStatement(change *Change) string {

// NewGenerator creates a new migration generator
func NewGenerator(opts GeneratorOptions) (*Generator, error) {
if opts.Logger == nil {
opts.Logger = logrus.New()
}

// Validate output directory
if opts.OutputDir == "" {
return nil, fmt.Errorf("output directory path cannot be empty")
}

// Clean and validate the path
outputDir := filepath.Clean(opts.OutputDir)

// Create output directory if it doesn't exist
if err := os.MkdirAll(outputDir, 0755); err != nil {
return nil, fmt.Errorf("failed to create output directory %q: %w", outputDir, err)
d, ok := dialect.Get(opts.Dialect)
if !ok {
return nil, fmt.Errorf("unsupported dialect: %s", opts.Dialect)
}

// Initialize logger if not provided
Expand All @@ -623,9 +601,11 @@ func NewGenerator(opts GeneratorOptions) (*Generator, error) {
}

return &Generator{
dialect: d,
outputDir: opts.OutputDir,
options: opts,
outputDir: outputDir,
logger: opts.Logger,
templates: loadDefaultTemplates(),
logger: logger,
}, nil
}

Expand Down

0 comments on commit 6bc614c

Please sign in to comment.