-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathschema.go
196 lines (185 loc) · 3.72 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
package testutil
import (
"testing"
"github.com/k1LoW/tbls/schema"
)
func NewSchema(t *testing.T) *schema.Schema {
const (
tableAName = "a"
tableBName = "b"
tableViewName = "view"
labelBlueName = "blue"
labelRedName = "red"
labelGreenName = "green"
enumName = "enum"
)
labelBlue := &schema.Label{
Name: labelBlueName,
Virtual: false,
}
labelRed := &schema.Label{
Name: labelRedName,
Virtual: false,
}
labelGreen := &schema.Label{
Name: labelGreenName,
Virtual: true,
}
ca := &schema.Column{
Name: "a",
Type: "INTEGER",
Comment: "column a",
}
cb := &schema.Column{
Name: "b",
Type: "INTEGER",
Comment: "column b",
}
cView := &schema.Column{
Name: "view_column",
Type: "INTEGER",
Comment: "column of view",
}
ta := &schema.Table{
Name: tableAName,
Comment: "table a",
Columns: []*schema.Column{
ca,
&schema.Column{
Name: "a2",
Type: "TEXT",
Comment: "column a2",
},
},
Labels: []*schema.Label{labelBlue, labelGreen},
}
ta.Indexes = []*schema.Index{
&schema.Index{
Name: "PRIMARY KEY",
Def: "PRIMARY KEY(a)",
Table: &ta.Name,
Columns: []string{"a"},
},
}
ta.Constraints = []*schema.Constraint{
&schema.Constraint{
Name: "PRIMARY",
Table: &ta.Name,
Def: "PRIMARY KEY (a)",
},
}
ta.Triggers = []*schema.Trigger{
&schema.Trigger{
Name: "update_a_a2",
Def: "CREATE CONSTRAINT TRIGGER update_a_a2 AFTER INSERT OR UPDATE ON a",
},
}
tb := &schema.Table{
Name: tableBName,
Comment: "table b",
Columns: []*schema.Column{
cb,
&schema.Column{
Name: "b2",
Type: "TEXT",
Comment: "column b2",
},
},
Labels: []*schema.Label{labelRed, labelGreen},
}
tView := &schema.Table{
Name: tableViewName,
Comment: "view",
Columns: []*schema.Column{
cView,
},
Type: "VIEW",
Def: "CREATE VIEW view AS SELECT a, b FROM a JOIN b ON a.a = b.b",
ReferencedTables: []*schema.Table{
ta,
tb,
},
}
enum := &schema.Enum{
Name: enumName,
Values: []string{"one", "two", "three"},
}
r := &schema.Relation{
Table: tb,
Columns: []*schema.Column{cb},
Cardinality: schema.OneOrMore,
ParentTable: ta,
ParentColumns: []*schema.Column{ca},
ParentCardinality: schema.ExactlyOne,
Def: "FOREIGN KEY (b) REFERENCES a(a)",
Virtual: false,
}
ca.ChildRelations = []*schema.Relation{r}
cb.ParentRelations = []*schema.Relation{r}
s := &schema.Schema{
Name: "testschema",
Tables: []*schema.Table{
ta,
tb,
tView,
},
Enums: []*schema.Enum{
enum,
},
Relations: []*schema.Relation{
r,
},
Viewpoints: schema.Viewpoints{
&schema.Viewpoint{
Name: "table a b",
Desc: "select table a and b",
Tables: []string{
tableAName,
tableBName,
},
},
&schema.Viewpoint{
Name: "label blue",
Desc: "select label blue",
Labels: []string{
labelBlueName,
},
},
&schema.Viewpoint{
Name: "label green",
Desc: "select label green",
Labels: []string{
labelGreenName,
},
Groups: []*schema.ViewpointGroup{
&schema.ViewpointGroup{
Name: "label red",
Desc: "select label red",
Labels: []string{
labelRedName,
},
},
},
},
&schema.Viewpoint{
Name: "table a label red",
Desc: "select table a and label red\n\n- table a\n- label red",
Tables: []string{
tableAName,
},
Labels: []string{
labelRedName,
},
},
},
Driver: &schema.Driver{
Name: "testdriver",
DatabaseVersion: "1.0.0",
Meta: &schema.DriverMeta{},
},
}
if err := s.Repair(); err != nil {
t.Fatal(err)
}
return s
}