-
Notifications
You must be signed in to change notification settings - Fork 157
/
schema.go
234 lines (218 loc) · 6.63 KB
/
schema.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
package schema
import (
"database/sql"
"encoding/json"
"fmt"
"sort"
"github.com/pkg/errors"
)
// Index is the struct for database index
type Index struct {
Name string `json:"name"`
Def string `json:"def"`
Table *string `json:"table"`
Columns []string `json:"columns"`
}
// Constraint is the struct for database constraint
type Constraint struct {
Name string `json:"name"`
Type string `json:"type"`
Def string `json:"def"`
Table *string `json:"table"`
ReferenceTable *string `json:"reference_table"`
Columns []string `json:"columns"`
ReferenceColumns []string `json:"reference_columns"`
}
// Trigger is the struct for database trigger
type Trigger struct {
Name string `json:"name"`
Def string `json:"def"`
}
// Column is the struct for table column
type Column struct {
Name string `json:"name"`
Type string `json:"type"`
Nullable bool `json:"nullable"`
Default sql.NullString `json:"default"`
Comment string `json:"comment"`
ParentRelations []*Relation `json:"-"`
ChildRelations []*Relation `json:"-"`
}
// Table is the struct for database table
type Table struct {
Name string `json:"name"`
Type string `json:"type"`
Comment string `json:"comment"`
Columns []*Column `json:"columns"`
Indexes []*Index `json:"indexes"`
Constraints []*Constraint `json:"constraints"`
Triggers []*Trigger `json:"triggers"`
Def string `json:"def"`
}
// Relation is the struct for table relation
type Relation struct {
Table *Table `json:"table"`
Columns []*Column `json:"columns"`
ParentTable *Table `json:"parent_table"`
ParentColumns []*Column `json:"parent_columns"`
Def string `json:"def"`
IsAdditional bool `json:"is_additional"`
}
// Driver is the struct for tbls driver information
type Driver struct {
Name string `json:"name"`
DatabaseVersion string `json:"database_version"`
}
// Schema is the struct for database schema
type Schema struct {
Name string `json:"name"`
Tables []*Table `json:"tables"`
Relations []*Relation `json:"relations"`
Driver *Driver `json:"driver"`
}
// MarshalJSON return custom JSON byte
func (c Column) MarshalJSON() ([]byte, error) {
if c.Default.Valid {
return json.Marshal(&struct {
Name string `json:"name"`
Type string `json:"type"`
Nullable bool `json:"nullable"`
Default string `json:"default"`
Comment string `json:"comment"`
ParentRelations []*Relation `json:"-"`
ChildRelations []*Relation `json:"-"`
}{
Name: c.Name,
Type: c.Type,
Nullable: c.Nullable,
Default: c.Default.String,
Comment: c.Comment,
ParentRelations: c.ParentRelations,
ChildRelations: c.ChildRelations,
})
}
return json.Marshal(&struct {
Name string `json:"name"`
Type string `json:"type"`
Nullable bool `json:"nullable"`
Default *string `json:"default"`
Comment string `json:"comment"`
ParentRelations []*Relation `json:"-"`
ChildRelations []*Relation `json:"-"`
}{
Name: c.Name,
Type: c.Type,
Nullable: c.Nullable,
Default: nil,
Comment: c.Comment,
ParentRelations: c.ParentRelations,
ChildRelations: c.ChildRelations,
})
}
// UnmarshalJSON ...
func (c *Column) UnmarshalJSON(data []byte) error {
s := struct {
Name string `json:"name"`
Type string `json:"type"`
Nullable bool `json:"nullable"`
Default *string `json:"default"`
Comment string `json:"comment"`
ParentRelations []*Relation `json:"-"`
ChildRelations []*Relation `json:"-"`
}{}
json.Unmarshal(data, &s)
c.Name = s.Name
c.Type = s.Type
c.Nullable = s.Nullable
if s.Default != nil {
c.Default.Valid = true
c.Default.String = *s.Default
} else {
c.Default.Valid = false
c.Default.String = ""
}
c.Comment = s.Comment
return nil
}
// FindTableByName find table by table name
func (s *Schema) FindTableByName(name string) (*Table, error) {
for _, t := range s.Tables {
if t.Name == name {
return t, nil
}
}
return nil, errors.WithStack(fmt.Errorf("not found table '%s'", name))
}
// FindColumnByName find column by column name
func (t *Table) FindColumnByName(name string) (*Column, error) {
for _, c := range t.Columns {
if c.Name == name {
return c, nil
}
}
return nil, errors.WithStack(fmt.Errorf("not found column '%s.%s'", t.Name, name))
}
// Sort schema tables, columns, relations, and constrains
func (s *Schema) Sort() error {
for _, t := range s.Tables {
for _, c := range t.Columns {
sort.SliceStable(c.ParentRelations, func(i, j int) bool {
return c.ParentRelations[i].Table.Name < c.ParentRelations[j].Table.Name
})
sort.SliceStable(c.ChildRelations, func(i, j int) bool {
return c.ChildRelations[i].Table.Name < c.ChildRelations[j].Table.Name
})
}
sort.SliceStable(t.Columns, func(i, j int) bool {
return t.Columns[i].Name < t.Columns[j].Name
})
sort.SliceStable(t.Indexes, func(i, j int) bool {
return t.Indexes[i].Name < t.Indexes[j].Name
})
sort.SliceStable(t.Constraints, func(i, j int) bool {
return t.Constraints[i].Name < t.Constraints[j].Name
})
sort.SliceStable(t.Triggers, func(i, j int) bool {
return t.Triggers[i].Name < t.Triggers[j].Name
})
}
sort.SliceStable(s.Tables, func(i, j int) bool {
return s.Tables[i].Name < s.Tables[j].Name
})
sort.SliceStable(s.Relations, func(i, j int) bool {
return s.Relations[i].Table.Name < s.Relations[j].Table.Name
})
return nil
}
// Repair column relations
func (s *Schema) Repair() error {
for _, r := range s.Relations {
t, err := s.FindTableByName(r.Table.Name)
if err != nil {
return errors.Wrap(err, "failed to repair relation")
}
for _, rc := range r.Columns {
c, err := t.FindColumnByName(rc.Name)
if err != nil {
return errors.Wrap(err, "failed to repair relation")
}
c.ParentRelations = append(c.ParentRelations, r)
rc = c
}
r.Table = t
pt, err := s.FindTableByName(r.ParentTable.Name)
if err != nil {
return errors.Wrap(err, "failed to repair relation")
}
for _, rc := range r.ParentColumns {
pc, err := pt.FindColumnByName(rc.Name)
if err != nil {
return errors.Wrap(err, "failed to repair relation")
}
pc.ChildRelations = append(pc.ChildRelations, r)
rc = pc
}
r.ParentTable = pt
}
return nil
}