Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions generator/postgres/postgres_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,54 @@ func GenerateDB(db *sql.DB, schema, destDir string, templates ...template.Templa
return nil
}

type GeneratorOption func(opts *generatorOptions)

type generatorOptions struct {
DestDir string
SkipClean bool
}

func WithSkipClean() GeneratorOption {
return func(opts *generatorOptions) {
opts.SkipClean = true
}
}

func WithDestDir(destDir string) GeneratorOption {
return func(opts *generatorOptions) {
opts.DestDir = destDir
}
}

func GenerateWithOptions(db *sql.DB, schema string, templates []template.Template, options ...GeneratorOption) error {
generatorTemplate := template.Default(postgres.Dialect)
if len(templates) > 0 {
generatorTemplate = templates[0]
}

schemaMetadata, err := metadata.GetSchema(db, &postgresQuerySet{}, schema)
if err != nil {
return fmt.Errorf("failed to get '%s' schema metadata: %w", schema, err)
}

genOptions := &generatorOptions{}
for _, option := range options {
option(genOptions)
}

processOptions := make([]template.ProcessSchemaOption, 0)
if genOptions.SkipClean {
processOptions = append(processOptions, template.WithSkipClean())
}

err = template.ProcessSchema(genOptions.DestDir, schemaMetadata, generatorTemplate, processOptions...)
if err != nil {
return fmt.Errorf("failed to generate schema %s: %d", schemaMetadata.Name, err)
}

return nil
}

func openConnection(dsn string) (*sql.DB, error) {
fmt.Println("Connecting to postgres database...")

Expand Down
28 changes: 24 additions & 4 deletions generator/template/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,39 @@ import (
"github.com/go-jet/jet/v2/internal/jet"
)

type processSchemaOptions struct {
SkipClean bool
}

type ProcessSchemaOption func(*processSchemaOptions)

func WithSkipClean() func(*processSchemaOptions) {
return func(o *processSchemaOptions) {
o.SkipClean = true
}
}

// ProcessSchema will process schema metadata and constructs go files using generator Template
func ProcessSchema(dirPath string, schemaMetaData metadata.Schema, generatorTemplate Template) error {
func ProcessSchema(dirPath string, schemaMetaData metadata.Schema, generatorTemplate Template, opts ...ProcessSchemaOption) error {
if schemaMetaData.IsEmpty() {
return nil
}

options := &processSchemaOptions{}
for _, optFn := range opts {
optFn(options)
}

schemaTemplate := generatorTemplate.Schema(schemaMetaData)
schemaPath := filepath.Join(dirPath, schemaTemplate.Path)

fmt.Println("Destination directory:", schemaPath)
fmt.Println("Cleaning up destination directory...")
if err := os.RemoveAll(schemaPath); err != nil {
return errors.New("failed to cleanup generated files")

if !options.SkipClean {
fmt.Println("Cleaning up destination directory...")
if err := os.RemoveAll(schemaPath); err != nil {
return errors.New("failed to cleanup generated files")
}
}

err := processModel(schemaPath, schemaMetaData, schemaTemplate)
Expand Down