-
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathpostgres_generator.go
More file actions
144 lines (118 loc) · 3.69 KB
/
postgres_generator.go
File metadata and controls
144 lines (118 loc) · 3.69 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
package postgres
import (
"database/sql"
"fmt"
"net/url"
"path/filepath"
"strconv"
"github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/generator/template"
"github.com/go-jet/jet/v2/postgres"
"github.com/jackc/pgconn"
)
// DBConnection contains postgres connection details
type DBConnection struct {
Host string
Port int
User string
Password string
SslMode string
Params string
DBName string
SchemaName string
}
// Generate generates jet files at destination dir from database connection details
func Generate(destDir string, dbConn DBConnection, genTemplate ...template.Template) (err error) {
dsn := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=%s",
url.PathEscape(dbConn.User),
url.PathEscape(dbConn.Password),
dbConn.Host,
strconv.Itoa(dbConn.Port),
url.PathEscape(dbConn.DBName),
dbConn.SslMode,
)
return GenerateDSN(dsn, dbConn.SchemaName, destDir, genTemplate...)
}
// GenerateDSN generates jet files using dsn connection string
func GenerateDSN(dsn, schema, destDir string, templates ...template.Template) error {
cfg, err := pgconn.ParseConfig(dsn)
if err != nil {
return fmt.Errorf("failed to parse config: %w", err)
}
if cfg.Database == "" {
return fmt.Errorf("database name is required")
}
db, err := openConnection(dsn)
if err != nil {
return fmt.Errorf("failed to open db connection: %w", err)
}
defer db.Close()
fmt.Println("Retrieving schema information...")
return GenerateDB(db, schema, filepath.Join(destDir, cfg.Database), templates...)
}
// GenerateDB generates jet files using the provided *sql.DB
func GenerateDB(db *sql.DB, schema, destDir string, templates ...template.Template) 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)
}
err = template.ProcessSchema(destDir, schemaMetadata, generatorTemplate)
if err != nil {
return fmt.Errorf("failed to generate schema %s: %d", schemaMetadata.Name, err)
}
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...")
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, fmt.Errorf("failed to open db connection: %w", err)
}
err = db.Ping()
if err != nil {
return nil, fmt.Errorf("failed to ping database: %w", err)
}
return db, nil
}