Skip to content

Commit 06c84c0

Browse files
authored
fix(package): Normalize tables when writing tables.json (#1227)
We might want to refactor this to no (ab)use the MemDB plugin but I think this is ok for now. Also we should call `FlattenTables` otherwise we only output the top level ones ---
1 parent 966a1ff commit 06c84c0

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

internal/memdb/memdb.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ func GetNewClient(options ...Option) plugin.NewClientFunc {
4343
c := &client{
4444
memoryDB: make(map[string][]arrow.Record),
4545
memoryDBLock: sync.RWMutex{},
46-
tables: make(map[string]*schema.Table),
46+
tables: map[string]*schema.Table{
47+
"table1": {Name: "table1", Relations: schema.Tables{{Name: "table2"}}},
48+
},
4749
}
4850
for _, opt := range options {
4951
opt(c)

serve/package.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,50 @@ type TargetBuild struct {
4646
Checksum string `json:"checksum"`
4747
}
4848

49+
// This is the structure the CLI publish command expects
50+
type pluginTable struct {
51+
Description string `json:"description,omitempty"`
52+
IsIncremental bool `json:"is_incremental,omitempty"`
53+
Name string `json:"name,omitempty"`
54+
Parent *string `json:"parent,omitempty"`
55+
Relations *[]string `json:"relations,omitempty"`
56+
Title string `json:"title,omitempty"`
57+
}
58+
4959
func (s *PluginServe) writeTablesJSON(ctx context.Context, dir string) error {
5060
tables, err := s.plugin.Tables(ctx, plugin.TableOptions{
5161
Tables: []string{"*"},
5262
})
5363
if err != nil {
5464
return err
5565
}
66+
flattenedTables := tables.FlattenTables()
67+
tablesToEncode := make([]pluginTable, 0, len(flattenedTables))
68+
for _, t := range flattenedTables {
69+
table := tables.Get(t.Name)
70+
var parent *string
71+
if table.Parent != nil {
72+
parent = &table.Parent.Name
73+
}
74+
var relations *[]string
75+
if table.Relations != nil {
76+
names := table.Relations.TableNames()
77+
relations = &names
78+
}
79+
tablesToEncode = append(tablesToEncode, pluginTable{
80+
Description: table.Description,
81+
IsIncremental: table.IsIncremental,
82+
Name: table.Name,
83+
Parent: parent,
84+
Relations: relations,
85+
Title: table.Title,
86+
})
87+
}
5688
buffer := &bytes.Buffer{}
5789
m := json.NewEncoder(buffer)
58-
m.SetIndent("", " ")
90+
m.SetIndent("", "")
5991
m.SetEscapeHTML(false)
60-
err = m.Encode(tables)
92+
err = m.Encode(tablesToEncode)
6193
if err != nil {
6294
return err
6395
}

serve/package_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ with multiple lines and **markdown**`
9797
"overview.md",
9898
}
9999
checkDocs(t, filepath.Join(distDir, "docs"), expectDocs)
100+
checkTables(t, distDir)
100101
})
101102
}
102103
}
@@ -125,6 +126,18 @@ func checkDocs(t *testing.T, dir string, expect []string) {
125126
}
126127
}
127128

129+
func checkTables(t *testing.T, distDir string) {
130+
content, err := os.ReadFile(filepath.Join(distDir, "tables.json"))
131+
if err != nil {
132+
t.Fatal(err)
133+
}
134+
tablesString := string(content)
135+
136+
if diff := cmp.Diff(tablesString, "[{\"name\":\"table1\",\"relations\":[\"table2\"]},{\"name\":\"table2\"}]\n"); diff != "" {
137+
t.Fatalf("unexpected files in docs directory (-want +got):\n%s", diff)
138+
}
139+
}
140+
128141
func checkPackageJSONContents(t *testing.T, filename string, expect PackageJSON) {
129142
f, err := os.Open(filename)
130143
if err != nil {

serve/plugin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestPluginServe(t *testing.T) {
8080
t.Fatal(err)
8181
}
8282

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

0 commit comments

Comments
 (0)