diff --git a/plugins/.snapshots/TestGenerateSourcePluginDocs-JSON-__tables.json b/plugins/.snapshots/TestGenerateSourcePluginDocs-JSON-__tables.json index 67f5d496b2..4ed34ef55c 100644 --- a/plugins/.snapshots/TestGenerateSourcePluginDocs-JSON-__tables.json +++ b/plugins/.snapshots/TestGenerateSourcePluginDocs-JSON-__tables.json @@ -61,7 +61,36 @@ "type": "TypeString" } ], - "relations": [] + "relations": [ + { + "name": "relation_relation_table", + "description": "Description for relational table's relation", + "columns": [ + { + "name": "_cq_source_name", + "type": "TypeString" + }, + { + "name": "_cq_sync_time", + "type": "TypeTimestamp" + }, + { + "name": "_cq_id", + "type": "TypeUUID", + "is_primary_key": true + }, + { + "name": "_cq_parent_id", + "type": "TypeUUID" + }, + { + "name": "string_col", + "type": "TypeString" + } + ], + "relations": [] + } + ] }, { "name": "relation_table2", diff --git a/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-README.md b/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-README.md index 8d727ef655..5ef4c70d71 100644 --- a/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-README.md +++ b/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-README.md @@ -1,7 +1,6 @@ # Source Plugin: test ## Tables -| Name | -| ------------- | -| [test_table](test_table.md) | -| ↳ [relation_table](relation_table.md) | -| ↳ [relation_table2](relation_table2.md) | +- [test_table](test_table.md) + - [relation_table](relation_table.md) + - [relation_relation_table](relation_relation_table.md) + - [relation_table2](relation_table2.md) diff --git a/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-relation_relation_table.md b/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-relation_relation_table.md new file mode 100644 index 0000000000..a4cce162a8 --- /dev/null +++ b/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-relation_relation_table.md @@ -0,0 +1,18 @@ +# Table: relation_relation_table + +Description for relational table's relation + +The primary key for this table is **_cq_id**. + +## Relations +This table depends on [relation_table](relation_table.md). + + +## Columns +| Name | Type | +| ------------- | ------------- | +|_cq_source_name|String| +|_cq_sync_time|Timestamp| +|_cq_id (PK)|UUID| +|_cq_parent_id|UUID| +|string_col|String| diff --git a/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-relation_table.md b/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-relation_table.md index 893eb833e7..71c4b3fae8 100644 --- a/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-relation_table.md +++ b/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-relation_table.md @@ -7,6 +7,9 @@ The primary key for this table is **_cq_id**. ## Relations This table depends on [test_table](test_table.md). +The following tables depend on relation_table: + - [relation_relation_table](relation_relation_table.md) + ## Columns | Name | Type | | ------------- | ------------- | diff --git a/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-test_table.md b/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-test_table.md index 38ccb4b679..9c1fd5e91a 100644 --- a/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-test_table.md +++ b/plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-test_table.md @@ -5,6 +5,7 @@ Description for test table The composite primary key for this table is (**id_col**, **id_col2**). ## Relations + The following tables depend on test_table: - [relation_table](relation_table.md) - [relation_table2](relation_table2.md) diff --git a/plugins/source_docs.go b/plugins/source_docs.go index d77c87885b..ddc0c4d10f 100644 --- a/plugins/source_docs.go +++ b/plugins/source_docs.go @@ -83,7 +83,9 @@ func (p *SourcePlugin) renderTablesAsMarkdown(dir string) error { return err } } - t, err := template.New("all_tables.md.go.tpl").ParseFS(templatesFS, "templates/all_tables.md.go.tpl") + t, err := template.New("all_tables.md.go.tpl").Funcs(template.FuncMap{ + "indentToDepth": indentToDepth, + }).ParseFS(templatesFS, "templates/all_tables*.md.go.tpl") if err != nil { return fmt.Errorf("failed to parse template for README.md: %v", err) } @@ -136,3 +138,13 @@ func renderTable(table *schema.Table, dir string) error { func formatType(v schema.ValueType) string { return strings.TrimPrefix(v.String(), "Type") } + +func indentToDepth(table *schema.Table) string { + s := "" + t := table + for t.Parent != nil { + s += " " + t = t.Parent + } + return s +} diff --git a/plugins/source_docs_test.go b/plugins/source_docs_test.go index 520640c814..10d1d00555 100644 --- a/plugins/source_docs_test.go +++ b/plugins/source_docs_test.go @@ -42,6 +42,18 @@ var testTables = []*schema.Table{ Type: schema.TypeString, }, }, + Relations: []*schema.Table{ + { + Name: "relation_relation_table", + Description: "Description for relational table's relation", + Columns: []schema.Column{ + { + Name: "string_col", + Type: schema.TypeString, + }, + }, + }, + }, }, { Name: "relation_table2", @@ -68,7 +80,7 @@ func TestGenerateSourcePluginDocs(t *testing.T) { t.Fatalf("unexpected error calling GenerateSourcePluginDocs: %v", err) } - expectFiles := []string{"test_table.md", "relation_table.md", "README.md"} + expectFiles := []string{"test_table.md", "relation_table.md", "relation_relation_table.md", "README.md"} for _, exp := range expectFiles { t.Run(exp, func(t *testing.T) { output := path.Join(tmpdir, exp) diff --git a/plugins/templates/all_tables.md.go.tpl b/plugins/templates/all_tables.md.go.tpl index 741df1ff23..618559dcb4 100644 --- a/plugins/templates/all_tables.md.go.tpl +++ b/plugins/templates/all_tables.md.go.tpl @@ -1,10 +1,5 @@ # Source Plugin: {{.Name}} ## Tables -| Name | -| ------------- | {{- range $table := $.Tables }} -| [{{$table.Name}}]({{$table.Name}}.md) | -{{- range $index, $rel := $table.Relations}} -| ↳ [{{$rel.Name}}]({{$rel.Name}}.md) | -{{- end}} +{{- template "all_tables_entry.md.go.tpl" $table}} {{- end }} \ No newline at end of file diff --git a/plugins/templates/all_tables_entry.md.go.tpl b/plugins/templates/all_tables_entry.md.go.tpl new file mode 100644 index 0000000000..166ef83be6 --- /dev/null +++ b/plugins/templates/all_tables_entry.md.go.tpl @@ -0,0 +1,5 @@ + +{{. | indentToDepth}}- [{{.Name}}]({{.Name}}.md) +{{- range $index, $rel := .Relations}} +{{- template "all_tables_entry.md.go.tpl" $rel}} +{{- end}} \ No newline at end of file diff --git a/plugins/templates/table.md.go.tpl b/plugins/templates/table.md.go.tpl index 46a416f185..de60c9a580 100644 --- a/plugins/templates/table.md.go.tpl +++ b/plugins/templates/table.md.go.tpl @@ -17,8 +17,7 @@ The composite primary key for this table is ({{ range $index, $pk := $.PrimaryKe {{- if $.Parent }} This table depends on [{{ $.Parent.Name }}]({{ $.Parent.Name }}.md). {{- end}} - -{{- if $.Relations }} +{{ if $.Relations }} The following tables depend on {{.Name}}: {{- range $rel := $.Relations }} - [{{ $rel.Name }}]({{ $rel.Name }}.md)