-
Notifications
You must be signed in to change notification settings - Fork 22
/
table.go
308 lines (276 loc) · 8.14 KB
/
table.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
package schema
import (
"context"
"fmt"
"regexp"
"github.com/cloudquery/plugin-sdk/internal/glob"
)
// TableResolver is the main entry point when a table is sync is called.
//
// Table resolver has 3 main arguments:
// - meta(ClientMeta): is the client returned by the plugin.Provider Configure call
// - parent(Resource): resource is the parent resource in case this table is called via parent table (i.e. relation)
// - res(chan any): is a channel to pass results fetched by the TableResolver
type TableResolver func(ctx context.Context, meta ClientMeta, parent *Resource, res chan<- any) error
type RowResolver func(ctx context.Context, meta ClientMeta, resource *Resource) error
type Multiplexer func(meta ClientMeta) []ClientMeta
type Transform func(table *Table) error
type Tables []*Table
// This is deprecated
type SyncSummary struct {
Resources uint64
Errors uint64
Panics uint64
}
type Table struct {
// Name of table
Name string `json:"name"`
// table description
Description string `json:"description"`
// Columns are the set of fields that are part of this table
Columns ColumnList `json:"columns"`
// Relations are a set of related tables defines
Relations Tables `json:"relations"`
// Transform
Transform Transform `json:"-"`
// Resolver is the main entry point to fetching table data and
Resolver TableResolver `json:"-"`
// Multiplex returns re-purposed meta clients. The sdk will execute the table with each of them
Multiplex Multiplexer `json:"-"`
// PostResourceResolver is called after all columns have been resolved, but before the Resource is sent to be inserted. The ordering of resolvers is:
// (Table) Resolver → PreResourceResolver → ColumnResolvers → PostResourceResolver
PostResourceResolver RowResolver `json:"-"`
// PreResourceResolver is called before all columns are resolved but after Resource is created. The ordering of resolvers is:
// (Table) Resolver → PreResourceResolver → ColumnResolvers → PostResourceResolver
PreResourceResolver RowResolver `json:"-"`
// IgnoreInTests is used to exclude a table from integration tests.
// By default, integration tests fetch all resources from cloudquery's test account, and verify all tables
// have at least one row.
// When IgnoreInTests is true, integration tests won't fetch from this table.
// Used when it is hard to create a reproducible environment with a row in this table.
IgnoreInTests bool `json:"ignore_in_tests"`
// Parent is the parent table in case this table is called via parent table (i.e. relation)
Parent *Table `json:"-"`
}
var (
reValidTableName = regexp.MustCompile(`^[a-z_][a-z\d_]*$`)
reValidColumnName = regexp.MustCompile(`^[a-z_][a-z\d_]*$`)
)
func (tt Tables) FilterDfs(tables, skipTables []string) (Tables, error) {
flattenedTables := tt.FlattenTables()
for _, includePattern := range tables {
matched := false
for _, table := range flattenedTables {
if glob.Glob(includePattern, table.Name) {
matched = true
break
}
}
if !matched {
return nil, fmt.Errorf("tables include a pattern %s with no matches", includePattern)
}
}
for _, excludePattern := range skipTables {
matched := false
for _, table := range flattenedTables {
if glob.Glob(excludePattern, table.Name) {
matched = true
break
}
}
if !matched {
return nil, fmt.Errorf("skip_tables include a pattern %s with no matches", excludePattern)
}
}
filteredTables := make(Tables, 0, len(tt))
for _, t := range tt {
filteredTable := t.Copy(nil)
filteredTable = filteredTable.filterDfs(false, tables, skipTables)
if filteredTable != nil {
filteredTables = append(filteredTables, filteredTable)
}
}
return filteredTables, nil
}
func (tt Tables) FlattenTables() Tables {
tables := make(Tables, 0, len(tt))
for _, t := range tt {
tables = append(tables, t)
tables = append(tables, t.Relations.FlattenTables()...)
}
return tables
}
func (tt Tables) TableNames() []string {
ret := []string{}
for _, t := range tt {
ret = append(ret, t.TableNames()...)
}
return ret
}
// GetTopLevel returns a table by name. Only returns the table if it is in top-level list.
func (tt Tables) GetTopLevel(name string) *Table {
for _, t := range tt {
if t.Name == name {
return t
}
}
return nil
}
// Get returns a table by name. Returns top-level tables and relations.
func (tt Tables) Get(name string) *Table {
for _, t := range tt {
if t.Name == name {
return t
}
table := t.Relations.Get(name)
if table != nil {
return table
}
}
return nil
}
func (tt Tables) ValidateDuplicateColumns() error {
for _, t := range tt {
if err := t.ValidateDuplicateColumns(); err != nil {
return err
}
}
return nil
}
func (tt Tables) ValidateDuplicateTables() error {
tables := make(map[string]bool, len(tt))
for _, t := range tt {
if _, ok := tables[t.Name]; ok {
return fmt.Errorf("duplicate table %s", t.Name)
}
tables[t.Name] = true
}
return nil
}
func (tt Tables) ValidateTableNames() error {
for _, t := range tt {
if err := t.ValidateName(); err != nil {
return err
}
if err := t.Relations.ValidateTableNames(); err != nil {
return err
}
}
return nil
}
func (tt Tables) ValidateColumnNames() error {
for _, t := range tt {
if err := t.ValidateColumnNames(); err != nil {
return err
}
if err := t.Relations.ValidateColumnNames(); err != nil {
return err
}
}
return nil
}
// this will filter the tree in-place
func (t *Table) filterDfs(parentMatched bool, tables []string, skipTables []string) *Table {
matched := parentMatched
for _, includeTable := range tables {
if glob.Glob(includeTable, t.Name) {
matched = true
break
}
}
for _, skipTable := range skipTables {
if glob.Glob(skipTable, t.Name) {
return nil
}
}
filteredRelations := make([]*Table, 0, len(t.Relations))
for _, r := range t.Relations {
filteredChild := r.filterDfs(matched, tables, skipTables)
if filteredChild != nil {
matched = true
filteredRelations = append(filteredRelations, r)
}
}
t.Relations = filteredRelations
if matched {
return t
}
return nil
}
func (t *Table) ValidateName() error {
ok := reValidTableName.MatchString(t.Name)
if !ok {
return fmt.Errorf("table name %q is not valid: table names must contain only lower-case letters, numbers and underscores, and must start with a lower-case letter or underscore", t.Name)
}
return nil
}
func (t *Table) ValidateDuplicateColumns() error {
columns := make(map[string]bool, len(t.Columns))
for _, c := range t.Columns {
if _, ok := columns[c.Name]; ok {
return fmt.Errorf("duplicate column %s in table %s", c.Name, t.Name)
}
columns[c.Name] = true
}
for _, rel := range t.Relations {
if err := rel.ValidateDuplicateColumns(); err != nil {
return err
}
}
return nil
}
func (t *Table) ValidateColumnNames() error {
for _, c := range t.Columns {
ok := reValidColumnName.MatchString(c.Name)
if !ok {
return fmt.Errorf("column name %q on table %q is not valid: column names must contain only lower-case letters, numbers and underscores, and must start with a lower-case letter or underscore", c.Name, t.Name)
}
}
return nil
}
func (t *Table) Column(name string) *Column {
for _, c := range t.Columns {
if c.Name == name {
return &c
}
}
return nil
}
// If the column with the same name exists, overwrites it.
// Otherwise, adds the column to the beginning of the table.
func (t *Table) OverwriteOrAddColumn(column *Column) {
for i, c := range t.Columns {
if c.Name == column.Name {
t.Columns[i] = *column
return
}
}
t.Columns = append([]Column{*column}, t.Columns...)
}
func (t *Table) PrimaryKeys() []string {
var primaryKeys []string
for _, c := range t.Columns {
if c.CreationOptions.PrimaryKey {
primaryKeys = append(primaryKeys, c.Name)
}
}
return primaryKeys
}
func (t *Table) TableNames() []string {
ret := []string{t.Name}
for _, rel := range t.Relations {
ret = append(ret, rel.TableNames()...)
}
return ret
}
func (t *Table) Copy(parent *Table) *Table {
c := *t
c.Parent = parent
c.Columns = make([]Column, len(t.Columns))
copy(c.Columns, t.Columns)
c.Relations = make([]*Table, len(t.Relations))
for i := range t.Relations {
c.Relations[i] = t.Relations[i].Copy(&c)
}
return &c
}