-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
390 lines (357 loc) · 9.42 KB
/
Copy pathmain.go
File metadata and controls
390 lines (357 loc) · 9.42 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// sqlite2d2 converts a SQLite database schema into a D2 diagram file.
//
// Usage:
//
// sqlite2d2 path/to/db.sqlite # writes D2 to stdout
// sqlite2d2 path/to/db.sqlite -o schema.d2 # writes to file
// sqlite2d2 -direction down db.sqlite # change layout direction
//
// Render with:
//
// d2 --layout=elk schema.d2 schema.svg
package main
import (
"database/sql"
"flag"
"fmt"
"os"
"sort"
"strings"
_ "modernc.org/sqlite"
)
type Column struct {
Name string
Type string
NotNull bool
Default sql.NullString
PK int // 0 if not PK, else 1-based position within the PK
}
type ForeignKey struct {
ID int
Seq int
Table string
From string
To string
OnUpdate string
OnDelete string
}
type Index struct {
Name string
Unique bool
Origin string // "c" = CREATE INDEX, "u" = UNIQUE, "pk" = PRIMARY KEY
Partial bool
Columns []string
}
type Table struct {
Name string
Columns []Column
ForeignKeys []ForeignKey
Indexes []Index
}
func main() {
output := flag.String("o", "", "Output file (default: stdout)")
direction := flag.String("direction", "right", "D2 layout direction: up|down|left|right")
notes := flag.Bool("notes", false,
"Emit per-table markdown index notes pinned with `near:`. "+
"Only the TALA layout engine supports `near: <object>`; ELK and "+
"dagre will reject it, so this is off by default.")
flag.Usage = func() {
fmt.Fprintln(os.Stderr,
"usage: sqlite2d2 [-o out.d2] [-direction right] [-notes] <database.sqlite>")
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
os.Exit(2)
}
dbPath := flag.Arg(0)
if _, err := os.Stat(dbPath); err != nil {
fmt.Fprintf(os.Stderr, "cannot open %s: %v\n", dbPath, err)
os.Exit(1)
}
db, err := sql.Open("sqlite", "file:"+dbPath+"?mode=ro")
if err != nil {
fmt.Fprintf(os.Stderr, "open db: %v\n", err)
os.Exit(1)
}
defer db.Close()
tables, err := loadSchema(db)
if err != nil {
fmt.Fprintf(os.Stderr, "load schema: %v\n", err)
os.Exit(1)
}
out := renderD2(tables, *direction, *notes)
if *output == "" {
fmt.Print(out)
} else if err := os.WriteFile(*output, []byte(out), 0644); err != nil {
fmt.Fprintf(os.Stderr, "write: %v\n", err)
os.Exit(1)
}
}
func loadSchema(db *sql.DB) ([]Table, error) {
rows, err := db.Query(`
SELECT name FROM sqlite_master
WHERE type = 'table' AND name NOT LIKE 'sqlite_%'
ORDER BY name`)
if err != nil {
return nil, err
}
var names []string
for rows.Next() {
var n string
if err := rows.Scan(&n); err != nil {
rows.Close()
return nil, err
}
names = append(names, n)
}
rows.Close()
tables := make([]Table, 0, len(names))
for _, name := range names {
t := Table{Name: name}
// Columns
cr, err := db.Query(fmt.Sprintf(`PRAGMA table_info(%s)`, quoteSQL(name)))
if err != nil {
return nil, fmt.Errorf("table_info(%s): %w", name, err)
}
for cr.Next() {
var cid int
var c Column
if err := cr.Scan(&cid, &c.Name, &c.Type, &c.NotNull, &c.Default, &c.PK); err != nil {
cr.Close()
return nil, err
}
t.Columns = append(t.Columns, c)
}
cr.Close()
// Foreign keys
fr, err := db.Query(fmt.Sprintf(`PRAGMA foreign_key_list(%s)`, quoteSQL(name)))
if err != nil {
return nil, fmt.Errorf("foreign_key_list(%s): %w", name, err)
}
for fr.Next() {
var fk ForeignKey
var match string
if err := fr.Scan(&fk.ID, &fk.Seq, &fk.Table, &fk.From, &fk.To,
&fk.OnUpdate, &fk.OnDelete, &match); err != nil {
fr.Close()
return nil, err
}
t.ForeignKeys = append(t.ForeignKeys, fk)
}
fr.Close()
// Indexes
ir, err := db.Query(fmt.Sprintf(`PRAGMA index_list(%s)`, quoteSQL(name)))
if err != nil {
return nil, fmt.Errorf("index_list(%s): %w", name, err)
}
var idxs []Index
for ir.Next() {
var seq, unique, partial int
var idx Index
if err := ir.Scan(&seq, &idx.Name, &unique, &idx.Origin, &partial); err != nil {
ir.Close()
return nil, err
}
idx.Unique = unique != 0
idx.Partial = partial != 0
idxs = append(idxs, idx)
}
ir.Close()
for i := range idxs {
iir, err := db.Query(fmt.Sprintf(`PRAGMA index_info(%s)`, quoteSQL(idxs[i].Name)))
if err != nil {
return nil, fmt.Errorf("index_info(%s): %w", idxs[i].Name, err)
}
for iir.Next() {
var seqno, cid int
var cname sql.NullString
if err := iir.Scan(&seqno, &cid, &cname); err != nil {
iir.Close()
return nil, err
}
if cname.Valid {
idxs[i].Columns = append(idxs[i].Columns, cname.String)
}
}
iir.Close()
}
// Stable order: PK index first, then unique, then explicit
sort.SliceStable(idxs, func(i, j int) bool {
rank := func(o string) int {
switch o {
case "pk":
return 0
case "u":
return 1
default:
return 2
}
}
return rank(idxs[i].Origin) < rank(idxs[j].Origin)
})
t.Indexes = idxs
tables = append(tables, t)
}
return tables, nil
}
func renderD2(tables []Table, direction string, notes bool) string {
var sb strings.Builder
sb.WriteString("# Auto-generated from SQLite schema by sqlite2d2\n")
sb.WriteString("# Render: d2 --layout=elk schema.d2 schema.svg\n\n")
fmt.Fprintf(&sb, "direction: %s\n\n", direction)
for _, t := range tables {
fkCols := map[string]bool{}
for _, fk := range t.ForeignKeys {
fkCols[fk.From] = true
}
uniqueCols := map[string]bool{}
for _, idx := range t.Indexes {
// Single-column UNIQUE constraint from table definition
if idx.Unique && idx.Origin == "u" && len(idx.Columns) == 1 {
uniqueCols[idx.Columns[0]] = true
}
}
fmt.Fprintf(&sb, "%s: {\n", d2Ident(t.Name))
sb.WriteString(" shape: sql_table\n")
for _, c := range t.Columns {
typeStr := strings.TrimSpace(c.Type)
if typeStr == "" {
typeStr = "ANY"
}
if c.NotNull {
typeStr += " NOT NULL"
}
if c.Default.Valid {
typeStr += " DEFAULT " + condense(c.Default.String)
}
var cons []string
if c.PK > 0 {
cons = append(cons, "primary_key")
}
if fkCols[c.Name] {
cons = append(cons, "foreign_key")
}
if uniqueCols[c.Name] && c.PK == 0 {
cons = append(cons, "unique")
}
line := fmt.Sprintf(" %s: %s", d2Ident(c.Name), typeStr)
switch len(cons) {
case 0:
// nothing
case 1:
line += fmt.Sprintf(" {constraint: %s}", cons[0])
default:
line += fmt.Sprintf(" {constraint: [%s]}", strings.Join(cons, "; "))
}
sb.WriteString(line + "\n")
}
sb.WriteString("}\n\n")
// Indexes as a markdown note pinned near the table.
// Skip the implicit PK index ("pk") since it duplicates the PK constraint.
// Also skip single-column unique-origin indexes already shown as {constraint: unique}.
if !notes {
continue
}
var notable []Index
for _, idx := range t.Indexes {
if idx.Origin == "pk" {
continue
}
if idx.Origin == "u" && len(idx.Columns) == 1 {
continue
}
notable = append(notable, idx)
}
if len(notable) > 0 {
noteName := d2Ident(t.Name + "_indexes")
fmt.Fprintf(&sb, "%s: |md\n", noteName)
fmt.Fprintf(&sb, " **Indexes on `%s`**\n\n", t.Name)
for _, idx := range notable {
tags := []string{}
if idx.Unique {
tags = append(tags, "unique")
}
if idx.Partial {
tags = append(tags, "partial")
}
suffix := ""
if len(tags) > 0 {
suffix = " *(" + strings.Join(tags, ", ") + ")*"
}
fmt.Fprintf(&sb, " - `%s`%s on (%s)\n",
idx.Name, suffix, strings.Join(idx.Columns, ", "))
}
fmt.Fprintf(&sb, "| {\n near: %s\n style.fill: \"#f7f7f5\"\n}\n\n", d2Ident(t.Name))
}
}
// Foreign-key arrows
hasFK := false
for _, t := range tables {
if len(t.ForeignKeys) > 0 {
hasFK = true
break
}
}
if hasFK {
sb.WriteString("# Foreign-key relationships\n")
for _, t := range tables {
for _, fk := range t.ForeignKeys {
label := ""
if fk.OnDelete != "" && fk.OnDelete != "NO ACTION" {
label = " : on delete " + strings.ToLower(fk.OnDelete)
}
fmt.Fprintf(&sb, "%s.%s -> %s.%s%s\n",
d2Ident(t.Name), d2Ident(fk.From),
d2Ident(fk.Table), d2Ident(fk.To), label)
}
}
}
return sb.String()
}
// d2ReservedKeys are D2 keywords that, when used as a bare identifier in
// a shape body, are interpreted as shape properties rather than as a
// column/child name. The one that bites in practice is `icon` (D2 reads
// the value as an image path and tries to bundle it). Quoting forces D2
// to treat the name as a child object.
var d2ReservedKeys = map[string]bool{
"icon": true,
"shape": true,
"style": true,
"near": true,
"label": true,
"tooltip": true,
"link": true,
"classes": true,
"direction": true,
"width": true,
"height": true,
"source": true,
"target": true,
"top": true,
"left": true,
"grid-rows": true,
"grid-cols": true,
}
// d2Ident quotes an identifier if it contains characters D2 treats
// specially or collides with a D2 reserved key.
func d2Ident(name string) string {
if name == "" {
return `""`
}
if d2ReservedKeys[name] ||
strings.ContainsAny(name, " .-/\\\t\n\"'(){}[]:;,&|<>") {
return `"` + strings.ReplaceAll(name, `"`, `\"`) + `"`
}
return name
}
// quoteSQL wraps an identifier in double quotes for use inside a PRAGMA call.
func quoteSQL(name string) string {
return `"` + strings.ReplaceAll(name, `"`, `""`) + `"`
}
// condense collapses internal whitespace so a multi-line default doesn't break
// the D2 column line.
func condense(s string) string {
return strings.Join(strings.Fields(s), " ")
}