diff --git a/generator/postgres/postgres_generator.go b/generator/postgres/postgres_generator.go index 196840ef..57e35404 100644 --- a/generator/postgres/postgres_generator.go +++ b/generator/postgres/postgres_generator.go @@ -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...") diff --git a/generator/template/process.go b/generator/template/process.go index 493713ff..2cfa9d76 100644 --- a/generator/template/process.go +++ b/generator/template/process.go @@ -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)