Skip to content

Commit 3d4ebe0

Browse files
authored
fix(package): Only return one level down of relations when writing tables.json (#1321)
Fixes cloudquery/cloudquery-issues#690 (internal issue). `Tables.TableNames()` is recursive and gets all nested names so we can't use it. Without the fix each table will have all of descendants under `relations` instead of only the children (1 level descendants) ---
1 parent 9072098 commit 3d4ebe0

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

internal/memdb/memdb.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ func GetNewClient(options ...Option) plugin.NewClientFunc {
7272
Unique: false,
7373
},
7474
},
75+
Relations: schema.Tables{
76+
{
77+
Name: "table3",
78+
Columns: []schema.Column{
79+
{
80+
Name: "col1",
81+
Type: types.UUID,
82+
Description: "col1 description",
83+
PrimaryKey: false,
84+
NotNull: false,
85+
IncrementalKey: true,
86+
Unique: false,
87+
},
88+
},
89+
},
90+
},
7591
},
7692
},
7793
},
@@ -165,12 +181,12 @@ func (c *client) Sync(_ context.Context, options plugin.SyncOptions, res chan<-
165181
return nil
166182
}
167183

168-
func (c *client) Tables(context.Context, plugin.TableOptions) (schema.Tables, error) {
184+
func (c *client) Tables(_ context.Context, opts plugin.TableOptions) (schema.Tables, error) {
169185
tables := make(schema.Tables, 0, len(c.tables))
170186
for _, table := range c.tables {
171187
tables = append(tables, table)
172188
}
173-
return tables, nil
189+
return tables.FilterDfs(opts.Tables, opts.SkipTables, opts.SkipDependentTables)
174190
}
175191

176192
func (c *client) migrate(_ context.Context, table *schema.Table) {

serve/package.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ func (s *PluginServe) writeTablesJSON(ctx context.Context, dir string) error {
6464
if table.Parent != nil {
6565
parent = &table.Parent.Name
6666
}
67-
var relations *[]string
67+
relations := make([]string, 0, len(table.Relations))
6868
if table.Relations != nil {
69-
names := table.Relations.TableNames()
70-
relations = &names
69+
for _, relation := range table.Relations {
70+
relations = append(relations, relation.Name)
71+
}
7172
}
7273
columns := make([]cloudquery_api.PluginTableColumn, 0, len(table.Columns))
7374
for _, column := range table.Columns {
@@ -86,7 +87,7 @@ func (s *PluginServe) writeTablesJSON(ctx context.Context, dir string) error {
8687
IsIncremental: &table.IsIncremental,
8788
Name: table.Name,
8889
Parent: parent,
89-
Relations: relations,
90+
Relations: &relations,
9091
Title: &table.Title,
9192
Columns: &columns,
9293
})

serve/plugin_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestPluginServe(t *testing.T) {
6767
t.Fatal(err)
6868
}
6969

70-
getTablesRes, err := c.GetTables(ctx, &pb.GetTables_Request{})
70+
getTablesRes, err := c.GetTables(ctx, &pb.GetTables_Request{Tables: []string{"*"}})
7171
if err != nil {
7272
t.Fatal(err)
7373
}
@@ -80,7 +80,7 @@ func TestPluginServe(t *testing.T) {
8080
t.Fatal(err)
8181
}
8282

83-
if len(tables) != 2 {
83+
if len(tables) != 3 {
8484
t.Fatalf("Expected 2 tables but got %d", len(tables))
8585
}
8686
testTable := schema.Table{

serve/testdata/memdbtables.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,29 @@
3030
}
3131
],
3232
"description": "",
33+
"parent": "table1",
34+
"relations": ["table3"],
3335
"is_incremental": false,
3436
"name": "table2",
3537
"title": ""
38+
},
39+
{
40+
"columns": [
41+
{
42+
"description": "col1 description",
43+
"incremental_key": true,
44+
"name": "col1",
45+
"not_null": false,
46+
"primary_key": false,
47+
"type": "uuid",
48+
"unique": false
49+
}
50+
],
51+
"description": "",
52+
"parent": "table2",
53+
"relations": [],
54+
"is_incremental": false,
55+
"name": "table3",
56+
"title": ""
3657
}
3758
]

0 commit comments

Comments
 (0)