-
Notifications
You must be signed in to change notification settings - Fork 116
/
gen_ddl.go
197 lines (157 loc) · 4.03 KB
/
gen_ddl.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
package gen
import (
"fmt"
"os"
"strings"
"text/template"
"agola.io/agola/internal/sqlg"
"agola.io/agola/internal/sqlg/sql"
"github.com/Masterminds/sprig/v3"
"github.com/huandu/xstrings"
)
type DDLGenericData struct {
Version uint
Sequences []DDLDataTableSequence
Tables []DDLGenericDataTableInfo
Data map[sql.Type]DDLData
}
type DDLGenericDataTableInfo struct {
Name string
Columns []DDLGenericDataColInfo
}
type DDLGenericDataColInfo struct {
Name string
Type string
JSON bool
Nullable bool
}
type DDLData struct {
Version uint
DBType string
TableDefs []DDLDataTable
IndexDefs []string
}
type DDLDataTable struct {
Table string
ColumnDefs []string
ConstraintDefs []string
DDL string
}
type DDLDataTableSequence struct {
Name string
TableName string
ColName string
}
func genDDLGenericData(gd *genData) DDLGenericData {
objectsInfo := []sqlg.ObjectInfo{}
for _, oi := range gd.ObjectsInfo {
oi.Fields = append(objectMetaFields(), oi.Fields...)
objectsInfo = append(objectsInfo, oi)
}
objectsInfo = sqlg.PopulateObjectsInfo(objectsInfo, "")
data := DDLGenericData{
Version: gd.Version,
Data: make(map[sql.Type]DDLData),
}
for _, oi := range objectsInfo {
tableInfo := DDLGenericDataTableInfo{
Name: oi.Table,
Columns: []DDLGenericDataColInfo{},
}
for _, of := range oi.Fields {
colName := of.ColName
if of.Sequence {
sequenceName := fmt.Sprintf("%s_%s_seq", oi.Table, colName)
data.Sequences = append(data.Sequences, DDLDataTableSequence{
Name: sequenceName,
TableName: oi.Table,
ColName: colName,
})
}
tableInfo.Columns = append(tableInfo.Columns, DDLGenericDataColInfo{
Name: colName,
Type: of.BaseType,
JSON: of.JSON,
Nullable: of.Nullable,
})
}
data.Tables = append(data.Tables, tableInfo)
}
return data
}
func genDDLData(gd *genData, dbType sql.Type) DDLData {
objectsInfo := []sqlg.ObjectInfo{}
for _, oi := range gd.ObjectsInfo {
oi.Fields = append(objectMetaFields(), oi.Fields...)
objectsInfo = append(objectsInfo, oi)
}
objectsInfo = sqlg.PopulateObjectsInfo(objectsInfo, dbType)
data := DDLData{
Version: gd.Version,
DBType: xstrings.FirstRuneToUpper(string(dbType)),
}
for _, oi := range objectsInfo {
tableDef := DDLDataTable{Table: oi.Table}
for _, of := range oi.Fields {
colName := of.ColName
colDef := fmt.Sprintf("%s %s", colName, of.SQLType)
if !of.Nullable {
colDef += " NOT NULL"
}
if of.Unique {
colDef += " UNIQUE"
}
tableDef.ColumnDefs = append(tableDef.ColumnDefs, colDef)
}
tableDef.ConstraintDefs = oi.Constraints
tableDef.DDL = fmt.Sprintf(`create table if not exists %s (%s, PRIMARY KEY (id)`, tableDef.Table, strings.Join(tableDef.ColumnDefs, ", "))
if len(tableDef.ConstraintDefs) > 0 {
tableDef.DDL += ", "
tableDef.DDL += strings.Join(tableDef.ConstraintDefs, ", ")
}
tableDef.DDL += ")"
data.TableDefs = append(data.TableDefs, tableDef)
data.IndexDefs = append(data.IndexDefs, oi.Indexes...)
}
return data
}
func genDDL(gd *genData) {
data := genDDLGenericData(gd)
for _, dbType := range []sql.Type{sql.Postgres, sql.Sqlite3} {
data.Data[dbType] = genDDLData(gd, dbType)
}
f, err := os.Create("ddl.go")
if err != nil {
panic(err)
}
defer f.Close()
if err := ddlTemplate.Execute(f, data); err != nil {
panic(err)
}
}
var ddlTemplate = template.Must(template.New("").Funcs(sprig.TxtFuncMap()).Funcs(funcs).Parse(`// Code generated by go generate; DO NOT EDIT.
package db
import (
"agola.io/agola/internal/sqlg"
)
{{- range $dbType, $data := .Data }}
var DDL{{ $data.DBType }} = []string{
{{- range $tableDef := $data.TableDefs }}
"{{ $tableDef.DDL }}",
{{- end }}
// indexes
{{- range $index := .IndexDefs }}
"{{ $index }}",
{{- end }}
}
{{- end }}
var Sequences = []sqlg.Sequence {
{{- range $sequence := .Sequences }}
{
Name: "{{ $sequence.Name }}",
Table: "{{ $sequence.TableName }}",
Column: "{{ $sequence.ColName }}",
},
{{- end }}
}
`))