Skip to content

Commit

Permalink
Merge pull request #312 from k1LoW/number
Browse files Browse the repository at this point in the history
Add config `format.number:` for display sequential numbers in table rows
  • Loading branch information
k1LoW authored Jul 20, 2021
2 parents 84b63c7 + 73cb686 commit a7dd441
Show file tree
Hide file tree
Showing 28 changed files with 1,712 additions and 27 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ doc: build doc_sqlite
./tbls doc my://root:mypass@localhost:33308/testdb -c testdata/exclude_test_tbls.yml -f sample/exclude
./tbls doc my://root:mypass@localhost:33308/testdb -c testdata/dict_test_tbls.yml -f sample/dict
./tbls doc my://root:mypass@localhost:33308/testdb -c testdata/font_test_tbls.yml -f sample/font
./tbls doc my://root:mypass@localhost:33308/testdb -c testdata/number_test_tbls.yml -f sample/number

doc_sqlite: build
./tbls doc sq://$(PWD)/testdata/testdb.sqlite3 -c testdata/test_tbls.yml -f sample/sqlite
Expand All @@ -75,6 +76,7 @@ testdoc: build testdoc_sqlite
./tbls diff my://root:mypass@localhost:33308/testdb -c testdata/exclude_test_tbls.yml sample/exclude
./tbls diff my://root:mypass@localhost:33308/testdb -c testdata/dict_test_tbls.yml sample/dict
./tbls diff my://root:mypass@localhost:33308/testdb -c testdata/font_test_tbls.yml sample/font
./tbls diff my://root:mypass@localhost:33308/testdb -c testdata/number_test_tbls.yml sample/number

testdoc_sqlite: build
./tbls diff sq://$(PWD)/testdata/testdb.sqlite3 -c testdata/test_tbls.yml sample/sqlite
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,12 @@ format:
# Adjust the column width of Markdown format table
# Default is false
adjust: true
# Sort the order of table list and columns
# Sort the order of table list and columns
# Default is false
sort: false
# Display sequential numbers in table rows
# Default is false
number: false
```

### ER diagram
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type DSN struct {
type Format struct {
Adjust bool `yaml:"adjust,omitempty"`
Sort bool `yaml:"sort,omitempty"`
Number bool `yaml:"number,omitempty"`
}

// ER is er setting
Expand Down
66 changes: 53 additions & 13 deletions output/md/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"

Expand Down Expand Up @@ -76,7 +77,7 @@ func (m *Md) OutputSchema(wr io.Writer, s *schema.Schema) error {
return errors.WithStack(err)
}
tmpl := template.Must(template.New("index").Funcs(output.Funcs(&m.config.MergedDict)).Parse(ts))
templateData := m.makeSchemaTemplateData(s, m.config.Format.Adjust)
templateData := m.makeSchemaTemplateData(s)
templateData["er"] = m.er
templateData["erFormat"] = m.config.ER.Format
templateData["baseUrl"] = m.config.BaseUrl
Expand All @@ -94,7 +95,7 @@ func (m *Md) OutputTable(wr io.Writer, t *schema.Table) error {
return errors.WithStack(err)
}
tmpl := template.Must(template.New(t.Name).Funcs(output.Funcs(&m.config.MergedDict)).Parse(ts))
templateData := m.makeTableTemplateData(t, m.config.Format.Adjust)
templateData := m.makeTableTemplateData(t)
templateData["er"] = m.er
templateData["erFormat"] = m.config.ER.Format
templateData["baseUrl"] = m.config.BaseUrl
Expand Down Expand Up @@ -430,24 +431,35 @@ func outputExists(s *schema.Schema, path string) bool {
return false
}

func (m *Md) makeSchemaTemplateData(s *schema.Schema, adjust bool) map[string]interface{} {
tablesData := [][]string{
func (m *Md) makeSchemaTemplateData(s *schema.Schema) map[string]interface{} {
number := m.config.Format.Number
adjust := m.config.Format.Adjust

tablesData := [][]string{}

tablesData = append(tablesData,
[]string{
m.config.MergedDict.Lookup("Name"),
m.config.MergedDict.Lookup("Columns"),
m.config.MergedDict.Lookup("Comment"),
m.config.MergedDict.Lookup("Type"),
},
[]string{"----", "-------", "-------", "----"},
}
)

for _, t := range s.Tables {
data := []string{
fmt.Sprintf("[%s](%s%s.md)", t.Name, m.config.BaseUrl, t.Name),
fmt.Sprintf("%d", len(t.Columns)),
t.Comment,
t.Type,
}
tablesData = append(tablesData, data)
tablesData = append(tablesData,
[]string{
fmt.Sprintf("[%s](%s%s.md)", t.Name, m.config.BaseUrl, t.Name),
fmt.Sprintf("%d", len(t.Columns)),
t.Comment,
t.Type,
},
)
}

if number {
tablesData = m.addNumberToTable(tablesData)
}

if adjust {
Expand All @@ -463,7 +475,10 @@ func (m *Md) makeSchemaTemplateData(s *schema.Schema, adjust bool) map[string]in
}
}

func (m *Md) makeTableTemplateData(t *schema.Table, adjust bool) map[string]interface{} {
func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} {
number := m.config.Format.Number
adjust := m.config.Format.Adjust

// Columns
columnsData := [][]string{}
if t.HasColumnWithExtraDef() {
Expand Down Expand Up @@ -638,6 +653,13 @@ func (m *Md) makeTableTemplateData(t *schema.Table, adjust bool) map[string]inte
referencedTables = append(referencedTables, fmt.Sprintf("[%s](%s%s.md)", rt.Name, m.config.BaseUrl, rt.Name))
}

if number {
columnsData = m.addNumberToTable(columnsData)
constraintsData = m.addNumberToTable(constraintsData)
indexesData = m.addNumberToTable(indexesData)
triggersData = m.addNumberToTable(triggersData)
}

if adjust {
return map[string]interface{}{
"Table": t,
Expand Down Expand Up @@ -682,3 +704,21 @@ func adjustTable(data [][]string) [][]string {

return data
}

func (m *Md) addNumberToTable(data [][]string) [][]string {
w := len(data[0])/10 + 1

for i, r := range data {
switch {
case i == 0:
r = append([]string{m.config.MergedDict.Lookup("#")}, r...)
case i == 1:
r = append([]string{strings.Repeat("-", w)}, r...)
default:
r = append([]string{strconv.Itoa(i - 1)}, r...)
}
data[i] = r
}

return data
}
29 changes: 16 additions & 13 deletions output/md/md_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ var tests = []struct {
gotFile string
wantFile string
adjust bool
number bool
}{
{"README.md", "README.md", "md_test_README.md.golden", false},
{"a.md", "a.md", "md_test_a.md.golden", false},
{"--adjust option", "README.md", "md_test_README.md.adjust.golden", true},
{"README.md", "README.md", "md_test_README.md.golden", false, false},
{"a.md", "a.md", "md_test_a.md.golden", false, false},
{"--adjust option", "README.md", "md_test_README.md.adjust.golden", true, false},
{"number", "README.md", "md_test_README.md.number.golden", false, true},
}

var testsTemplate = []struct {
name string
gotFile string
wantFile string
adjust bool
number bool
}{
{"README.md", "README.md", "md_template_test_README.md.golden", false},
{"a.md", "a.md", "md_template_test_a.md.golden", false},
{"--adjust option", "README.md", "md_template_test_README.md.adjust.golden", true},
{"README.md", "README.md", "md_template_test_README.md.golden", false, false},
{"a.md", "a.md", "md_template_test_a.md.golden", false, false},
{"--adjust option", "README.md", "md_template_test_README.md.adjust.golden", true, false},
{"number", "README.md", "md_template_test_README.md.number.golden", false, true},
}

func TestOutput(t *testing.T) {
Expand All @@ -40,15 +44,15 @@ func TestOutput(t *testing.T) {
if err != nil {
t.Error(err)
}
tempDir, _ := ioutil.TempDir("", "tbls")
tempDir := t.TempDir()
force := true
adjust := tt.adjust
erFormat := "png"
defer os.RemoveAll(tempDir)
err = c.Load(filepath.Join(testdataDir(), "out_test_tbls.yml"), config.DocPath(tempDir), config.Adjust(adjust), config.ERFormat(erFormat))
if err != nil {
t.Error(err)
}
c.Format.Number = tt.number
err = c.MergeAdditionalData(s)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -78,19 +82,18 @@ func TestOutputTemplate(t *testing.T) {
if err != nil {
t.Error(err)
}
tempDir, _ := ioutil.TempDir("", "tbls")
tempDir := t.TempDir()
force := true
adjust := tt.adjust
erFormat := "png"
defer os.RemoveAll(tempDir)
err = c.Load(filepath.Join(testdataDir(), "out_templates_test_tbls.yml"), config.DocPath(tempDir), config.Adjust(adjust), config.ERFormat(erFormat))
if err != nil {
t.Error(err)
}
c.Format.Number = tt.number
// use the templates in the testdata directory
c.Templates.MD.Table = filepath.Join(testdataDir(), c.Templates.MD.Table)
c.Templates.MD.Index = filepath.Join(testdataDir(), c.Templates.MD.Index)

err = c.MergeAdditionalData(s)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -121,15 +124,15 @@ func TestDiffSchemaAndDocs(t *testing.T) {
if err != nil {
t.Error(err)
}
docPath, _ := ioutil.TempDir("", "tbls")
docPath := t.TempDir()
force := true
adjust := tt.adjust
erFormat := "png"
defer os.RemoveAll(docPath)
err = c.Load(filepath.Join(testdataDir(), "out_test_tbls.yml"), config.DocPath(docPath), config.Adjust(adjust), config.ERFormat(erFormat))
if err != nil {
t.Error(err)
}
c.Format.Number = tt.number
err = c.MergeAdditionalData(s)
if err != nil {
t.Error(err)
Expand Down
43 changes: 43 additions & 0 deletions sample/number/CamelizeTable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# CamelizeTable

## Description

<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `CamelizeTable` (
`id` bigint NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
```

</details>

## Columns

| # | Name | Type | Default | Nullable | Extra Definition | Children | Parents | Comment |
| - | ---- | ---- | ------- | -------- | --------------- | -------- | ------- | ------- |
| 1 | id | bigint | | false | auto_increment | | | |
| 2 | created | datetime | | false | | | | |

## Constraints

| # | Name | Type | Definition |
| - | ---- | ---- | ---------- |
| 1 | PRIMARY | PRIMARY KEY | PRIMARY KEY (id) |

## Indexes

| # | Name | Definition |
| - | ---- | ---------- |
| 1 | PRIMARY | PRIMARY KEY (id) USING BTREE |

## Relations

![er](CamelizeTable.svg)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
29 changes: 29 additions & 0 deletions sample/number/CamelizeTable.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions sample/number/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# testdb

## Description

Sample database document.

## Labels

`sample` `tbls`

## Tables

| # | Name | Columns | Comment | Type |
| - | ---- | ------- | ------- | ---- |
| 1 | [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE |
| 2 | [comment_stars](comment_stars.md) | 6 | | BASE TABLE |
| 3 | [comments](comments.md) | 7 | Comments<br>Multi-line<br>table<br>comment | BASE TABLE |
| 4 | [hyphen-table](hyphen-table.md) | 3 | | BASE TABLE |
| 5 | [logs](logs.md) | 7 | Auditログ | BASE TABLE |
| 6 | [post_comments](post_comments.md) | 7 | post and comments View table | VIEW |
| 7 | [posts](posts.md) | 7 | Posts table | BASE TABLE |
| 8 | [user_options](user_options.md) | 4 | User options table | BASE TABLE |
| 9 | [users](users.md) | 6 | Users table | BASE TABLE |

## Relations

![er](schema.svg)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Loading

0 comments on commit a7dd441

Please sign in to comment.