-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb_schema_gen.go
358 lines (300 loc) · 9.15 KB
/
db_schema_gen.go
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package gen
import (
"bytes"
"context"
"fmt"
"go/format"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"text/template"
"github.com/kataras/pg"
"golang.org/x/mod/modfile"
)
// GoImportsTool is the name of the tool that will be used to format the generated code.
var GoImportsTool = "goimports"
// ExportOptions is the options for the schema export.
// Used in GenerateSchemaFromDatabase and GenerateColumnsFromSchema.
type ImportOptions struct {
ConnString string
ListTables pg.ListTablesOptions
}
// TODO: Add support for base-type entities.
// Make base classes, e.g.
// Accept:
// { "TargetDater": ["source_id", "target_date"],
// "Base": "id", "created_at", "updated_at"] }.
//
// Output:
// $ROOT_DIR/base/target_dater.go
// $ROOT_DIR/base/base.go
//
// And on each table which contains these columns,
// replace the column printing with the base.TargetDater and/or base.Base
// and import this 'base' package to each table's file.
func GenerateSchemaFromDatabase(ctx context.Context, i ImportOptions, e ExportOptions) error {
if err := e.apply(); err != nil {
return err
}
db, err := pg.Open(ctx, pg.NewSchema(), i.ConnString)
if err != nil {
return err
}
defer db.Close()
tables, err := db.ListTables(ctx, i.ListTables)
if err != nil {
return err
}
if len(tables) == 0 {
return nil
}
checkAndPrintTableColumnsMissingTypes(i, e, tables)
schemaFilename := e.GetFileName(e.RootDir, "schema.go")
err = mkdir(schemaFilename)
if err != nil {
return fmt.Errorf("mkdir: %s: %w", e.RootDir, err)
}
goModuleName, modFileAbsPath, err := findNearestGoModName(e.RootDir)
if err != nil {
return fmt.Errorf("find nearest go.mod: %w", err)
}
rootImportPath := goModuleName + strings.TrimPrefix(filepath.ToSlash(e.RootDir), filepath.ToSlash(modFileAbsPath))
// fmt.Printf("Root import path: %s\n", rootImportPath)
rootPackageName := e.GetPackageName("")
schemaData, err := generateSchemaFile(&e, rootPackageName, rootImportPath, goModuleName, db.ConnectionOptions.Database, tables)
if err != nil {
return fmt.Errorf("generate schema: %s: %w", schemaFilename, err)
}
err = os.WriteFile(schemaFilename, schemaData, e.FileMode)
if err != nil {
return fmt.Errorf("write schema: %s: %w", schemaFilename, err)
}
for _, td := range tables {
data, err := generateTable(e.GetPackageName(td.Name), td)
if err != nil {
return fmt.Errorf("generate table: %s: %w", td.Name, err)
}
filename := e.GetFileName(e.RootDir, td.Name)
if filename == "" {
continue
}
mkdir(filename)
err = os.WriteFile(filename, data, e.FileMode)
if err != nil {
return fmt.Errorf("write table: %s: defininion file: %s: %w", td.Name, filename, err)
}
}
// Even if the file contents is a result of format.Source (gofmt), some times the import paths
// are not formatted correctly, so we call the goimports if exists -w $ROOT_DIR directly.
if _, err = exec.LookPath(GoImportsTool); err == nil {
err = exec.Command(GoImportsTool, "-w", e.RootDir).Run()
if err != nil {
return fmt.Errorf("%s -w %s: %w", GoImportsTool, e.RootDir, err)
}
}
return nil
}
func checkAndPrintTableColumnsMissingTypes(i ImportOptions, e ExportOptions, tables []*pg.Table) {
if len(tables) == 0 {
return
}
var columnsTypeMissingLines []string
for _, td := range tables {
for _, col := range td.Columns {
if col.FieldType == nil || col.FieldType.Kind() == reflect.Invalid {
missingLine := fmt.Sprintf("%s.%s.%s", td.Name, col.Name, col.Type.String())
columnsTypeMissingLines = append(columnsTypeMissingLines, missingLine)
}
}
}
if len(columnsTypeMissingLines) == 0 {
return
}
fmt.Printf(`Field type is unknown for %d type(s): %s.
To fix that you have to apply some rules through the ImportOptions of the GenerateSchemaFromDatabase function,
example:
i := gen.ImportOptions{
ConnString: "%s",
ListTables: pg.ListTablesOptions{
Filter: pg.MapTypeFilter{
"%s": YourCustomType{},
// [...]
}
}
e := gen.ExportOptions {
RootDir: "%s",
// your other options...
}
err := gen.GenerateSchemaFromDatabase(context.Background(), i, e)
// [handle error...]
`, len(columnsTypeMissingLines), strings.Join(columnsTypeMissingLines, ", "), i.ConnString, columnsTypeMissingLines[0], e.RootDir)
}
var generateSchemaTmpl = template.Must(
template.New("").Funcs(template.FuncMap{
"add": func(a, b int) int {
return a + b
},
}).Parse(`
package {{.PackageName}}
import (
"github.com/kataras/pg"
{{range .Tables}}
{{- if .ImportPath -}}
"{{.ImportPath}}"
{{end -}}
{{end}}
)
// Schema describes the {{.DatabaseName}} database schema.
// Usage:
// db, err := pg.Open(context.Background(), Schema, "connection_string_secret_here")
var Schema = pg.NewSchema().
{{- $length := len .Tables }}
{{- range $i, $table := .Tables}}
MustRegister("{{$table.TableName}}", {{$table.StructInitText}} {{- if $table.ReadOnly}}, pg.View {{- end}}){{- if eq $length (add $i 1)}}{{- else}}.{{- end}}
{{- end}}
`))
func generateSchemaFile(e *ExportOptions, packageName, rootImportPath, goModuleName, databaseName string /* we need it to import the generated table entity files */, tables []*pg.Table) ([]byte, error) {
type TableSchemaData struct {
ImportPath string // e.g. github.com/kataras/pg/gen/_testdata/customer
TableName string // e.g. customers
StructInitText string // e.g customer.Customer{}
ReadOnly bool
}
tableSchemaTmplData := make([]TableSchemaData, 0, len(tables))
for _, table := range tables {
var (
tableFullPackageName string
structInitText = fmt.Sprintf("%s{}", table.StructName)
)
// testdata or customer
tablePackageName := e.GetPackageName(table.Name)
if tablePackageName != packageName {
tableFullPackageName = filepath.ToSlash(filepath.Join(rootImportPath, tablePackageName))
// customer.Customer{}
structInitText = fmt.Sprintf("%s.%s{}", tablePackageName, table.StructName)
}
tableSchemaTmplData = append(tableSchemaTmplData,
TableSchemaData{
ImportPath: tableFullPackageName,
TableName: table.Name,
StructInitText: structInitText,
ReadOnly: table.IsReadOnly(),
})
}
tmplData := map[string]interface{}{
"PackageName": packageName,
"DatabaseName": databaseName,
"Tables": tableSchemaTmplData,
}
var buf bytes.Buffer
if err := generateSchemaTmpl.Execute(&buf, tmplData); err != nil {
return nil, fmt.Errorf("execute: %w", err)
}
result, err := format.Source(buf.Bytes())
if err != nil {
return nil, fmt.Errorf("format source: %w\n%s", err, buf.String())
}
return result, nil
}
func findNearestGoModName(rootDir string) (string, string, error) {
if rootDir == "" {
// Get the current working directory
cwd, err := os.Getwd()
if err != nil {
return "", "", err
}
rootDir = cwd
}
// Find the nearest go.mod file and its module path
modName, modFileAbsPath, err := findGoModName(rootDir)
if err != nil {
return "", modFileAbsPath, err
}
return modName, modFileAbsPath, nil
}
func findGoModName(cwd string) (string, string, error) {
// Walk up the directory tree until finding go.mod or reaching root
for {
// fmt.Println("Searching in", cwd)
goModName, found, err := findGoMod(cwd)
if err != nil {
return "", "", err
}
if found {
return goModName, cwd, nil
}
parent := filepath.Dir(cwd)
if parent == cwd {
return "", "", fmt.Errorf("reached root without finding go.mod")
}
cwd = parent
}
}
const goModFileName = "go.mod"
// findGoMod returns true if there is a file named go.mod in the given directory
func findGoMod(dir string) (string, bool, error) {
files, err := os.ReadDir(dir)
if err != nil {
return "", false, err
}
for _, file := range files {
if file.IsDir() {
continue
}
filename := file.Name()
if filename == goModFileName {
fullpath := filepath.Join(dir, goModFileName)
data, err := os.ReadFile(fullpath)
if err != nil {
return "", false, err
}
return modfile.ModulePath(data), true, nil
}
}
return "", false, nil
}
var generateTableTmpl = template.Must(
template.New("").
Parse(`
package {{.PackageName}}
{{ $fileImports := .ListImportPaths }} {{ $length := len $fileImports }} {{if gt $length 0}}
import (
{{range $fileImports}} "{{.}}"
{{end}}
)
{{end}}
{{ if .Description }}
// {{.Description}}
{{- else }}
{{ if eq .Type 0}}
// {{.StructName}} is a struct value that represents a record in the {{.Name}} table.
{{- else }}
// {{.StructName}} is a struct value that represents a record in the {{.Name}} view.
{{- end }}
{{- end }}
type {{.StructName}} struct {
{{range .Columns}}
{{- if .Description}}
// {{.Description}}
{{- end }}
{{.FieldName}} {{if .FieldType}} {{.FieldType.String}} {{else }} UNKNOWN__USE_pg.MapTypeFilter {{end}} ` + "`" + `{{.FieldTagString true}}` + "`" + `
{{- end}}
}
`))
func generateTable(packageName string, td *pg.Table) ([]byte, error) {
tmplData := generateTemplateData{
Table: td,
PackageName: packageName,
}
var buf bytes.Buffer
if err := generateTableTmpl.Execute(&buf, tmplData); err != nil {
return nil, fmt.Errorf("execute: %w", err)
}
result, err := format.Source(buf.Bytes())
if err != nil {
return nil, fmt.Errorf("format source: %w\n%s", err, buf.String())
}
return result, nil
}