-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.go
More file actions
236 lines (217 loc) · 6.75 KB
/
pipeline.go
File metadata and controls
236 lines (217 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package sqlproc
import (
"context"
"database/sql"
"errors"
"fmt"
"log"
"os"
"path/filepath"
)
// Logger is a minimal logging interface used by the orchestration pipeline.
type Logger interface {
Printf(format string, v ...any)
}
// PipelineOptions configure orchestration of migrations and code generation.
type PipelineOptions struct {
// SQLInputs are directories and/or files containing stored procedure SQL.
SQLInputs []string
// MigrationInputs are directories/files with schema migrations. Optional.
MigrationInputs []string
// OutputDir is where generated Go files are written. Defaults to ./generated.
OutputDir string
// PackageName overrides the generated Go package name. Defaults to "generated".
PackageName string
// SkipMigrate toggles executing migrations against a database.
SkipMigrate bool
// SkipGenerate toggles emitting Go code.
SkipGenerate bool
// DB allows callers to supply an existing *sql.DB handle.
DB *sql.DB
// DBURL is used to lazily open a connection when DB is nil.
DBURL string
// DBDriver is the sql driver name used with DBURL (default: "postgres").
DBDriver string
// Parser allows providing a custom parser. Defaults to NewParser().
Parser *Parser
// GeneratorOptions are passed to the Go code generator.
GeneratorOptions GeneratorOptions
// Logger emits progress logs. Defaults to a standard logger writing to stdout.
Logger Logger
// SchemaModels controls schema-introspection-based model generation.
SchemaModels *SchemaModelOptions
}
// PipelineResult captures the work performed by Run.
type PipelineResult struct {
Procedures []*Procedure
SchemaMigrations []*SchemaMigration
OutputDir string
GeneratedFiles []string
SchemaTables []*Table
SchemaFiles []string
}
// Run executes the configured pipeline: resolve -> parse -> migrate -> generate.
func Run(ctx context.Context, opts PipelineOptions) (*PipelineResult, error) {
if ctx == nil {
ctx = context.Background()
}
if len(opts.SQLInputs) == 0 && len(opts.MigrationInputs) == 0 && opts.SchemaModels == nil {
return nil, errors.New("sqlproc: provide SQL inputs, migrations, or schema model options")
}
logWriter := opts.Logger
if logWriter == nil {
logWriter = log.New(os.Stdout, "[sqlproc] ", log.LstdFlags)
}
parser := opts.Parser
if parser == nil {
parser = NewParser()
}
var procs []*Procedure
if len(opts.SQLInputs) > 0 {
sqlFiles, err := ResolveFiles(opts.SQLInputs)
if err != nil {
return nil, fmt.Errorf("resolve SQL inputs: %w", err)
}
logWriter.Printf("resolved %d SQL file(s)", len(sqlFiles))
procs, err = parser.ParseFiles(sqlFiles)
if err != nil {
return nil, fmt.Errorf("parse SQL files: %w", err)
}
} else {
logWriter.Printf("no stored procedure inputs provided; skipping procedure parsing")
}
var schemaMigrations []*SchemaMigration
if len(opts.MigrationInputs) > 0 {
migFiles, err := ResolveFiles(opts.MigrationInputs)
if err != nil {
return nil, fmt.Errorf("resolve migration inputs: %w", err)
}
schemaMigrations, err = LoadSchemaMigrations(migFiles)
if err != nil {
return nil, fmt.Errorf("load migrations: %w", err)
}
logWriter.Printf("resolved %d schema migration(s)", len(schemaMigrations))
}
db, cleanup, err := prepareDB(ctx, opts)
if err != nil {
return nil, err
}
if cleanup != nil {
defer cleanup()
}
if !opts.SkipMigrate {
if db == nil {
return nil, errors.New("sqlproc: DB or DBURL must be provided when migrations are enabled")
}
if len(schemaMigrations) > 0 {
logWriter.Printf("applying %d schema migration(s)", len(schemaMigrations))
}
if len(procs) > 0 {
logWriter.Printf("applying %d stored procedure(s)", len(procs))
}
if err := runMigrations(ctx, db, schemaMigrations, procs); err != nil {
return nil, err
}
}
outputDir := opts.OutputDir
if outputDir == "" {
outputDir = "./generated"
}
defaultPackage := opts.PackageName
if defaultPackage == "" {
defaultPackage = opts.GeneratorOptions.PackageName
}
if defaultPackage == "" {
defaultPackage = "generated"
}
var generatedFiles []string
if !opts.SkipGenerate {
pkgName := opts.PackageName
if pkgName == "" {
pkgName = opts.GeneratorOptions.PackageName
}
if pkgName == "" {
pkgName = "generated"
}
if len(procs) == 0 {
logWriter.Printf("no procedures to generate; skipping code emission")
} else {
genOpts := opts.GeneratorOptions
genOpts.PackageName = pkgName
gen := NewGenerator(genOpts)
if err := gen.Generate(procs, outputDir); err != nil {
return nil, fmt.Errorf("generate Go code: %w", err)
}
generatedFiles = []string{
filepath.Join(outputDir, "db.go"),
filepath.Join(outputDir, "models.go"),
filepath.Join(outputDir, "queries.go"),
}
logWriter.Printf("generated Go package %q in %s", pkgName, outputDir)
}
}
var schemaTables []*Table
var schemaFiles []string
if opts.SchemaModels != nil {
if db == nil {
return nil, errors.New("sqlproc: schema model generation requires a database connection or DBURL")
}
schemaOpts := opts.SchemaModels.withDefaults(outputDir, defaultPackage)
var err error
schemaTables, err = loadSchemaTables(ctx, db, schemaOpts)
if err != nil {
return nil, fmt.Errorf("introspect schema: %w", err)
}
generator := &SchemaModelGenerator{Options: schemaOpts}
schemaFiles, err = generator.Generate(schemaTables)
if err != nil {
return nil, fmt.Errorf("generate schema models: %w", err)
}
if len(schemaTables) > 0 {
logWriter.Printf("generated %d schema model(s) in %s", len(schemaTables), schemaOpts.OutputDir)
} else {
logWriter.Printf("no tables discovered for schema model generation")
}
}
return &PipelineResult{
Procedures: procs,
SchemaMigrations: schemaMigrations,
OutputDir: outputDir,
GeneratedFiles: generatedFiles,
SchemaTables: schemaTables,
SchemaFiles: schemaFiles,
}, nil
}
func prepareDB(ctx context.Context, opts PipelineOptions) (*sql.DB, func(), error) {
db := opts.DB
if db != nil {
return db, nil, nil
}
if opts.DBURL == "" {
return nil, nil, nil
}
driver := opts.DBDriver
if driver == "" {
driver = "postgres"
}
newDB, err := sql.Open(driver, opts.DBURL)
if err != nil {
return nil, nil, fmt.Errorf("open db: %w", err)
}
if err := newDB.PingContext(ctx); err != nil {
_ = newDB.Close()
return nil, nil, fmt.Errorf("ping db: %w", err)
}
return newDB, func() { _ = newDB.Close() }, nil
}
func runMigrations(ctx context.Context, db *sql.DB, schemaMigrations []*SchemaMigration, procs []*Procedure) error {
if len(schemaMigrations) > 0 {
if err := NewSchemaMigrator(db).Migrate(ctx, schemaMigrations); err != nil {
return fmt.Errorf("schema migrations: %w", err)
}
}
if err := NewMigrator(db).Migrate(ctx, procs); err != nil {
return fmt.Errorf("procedure migrations: %w", err)
}
return nil
}