From 36ed72710871ed88d0771be9e7f6ab6b4e3f32c4 Mon Sep 17 00:00:00 2001 From: harakeishi Date: Wed, 8 Feb 2023 23:24:47 +0900 Subject: [PATCH 1/4] =?UTF-8?q?schema.json=E3=81=AB=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trv/db.go | 54 +++++++++++++++++++++++++++++++++++---------------- trv/source.go | 4 ---- trv/table.go | 19 +++++++++--------- trv/trv.go | 4 ++-- 4 files changed, 49 insertions(+), 32 deletions(-) diff --git a/trv/db.go b/trv/db.go index 8e805e4..5c95c76 100644 --- a/trv/db.go +++ b/trv/db.go @@ -13,7 +13,9 @@ import ( ) type DB struct { - tables []Table + Tables []Table `json:"tables"` + Name string `json:"name"` + Desc string `json:"desc"` } // If there is DB data locally, load it and return it. @@ -23,7 +25,7 @@ func (d *DB) loadData(repo, path string) { log.Printf("loadData fail:%s", err) } raw, _ := ioutil.ReadFile(fmt.Sprintf("%s/.trv/%s-%s.json", home, repo, strings.Replace(path, "/", "-", -1))) - json.Unmarshal(raw, &d.tables) + json.Unmarshal(raw, &d.Tables) } // Store DB data locally. @@ -37,7 +39,7 @@ func (d *DB) saveData(repo, path string) { log.Printf("saveData fail:%s", err) } } - file, err := json.MarshalIndent(d.tables, "", " ") + file, err := json.MarshalIndent(d.Tables, "", " ") if err != nil { log.Printf("saveData fail:%s", err) } @@ -47,21 +49,39 @@ func (d *DB) saveData(repo, path string) { } func (d *DB) fetchDBInfo(client *github.Client, ctx context.Context, source Source) error { - _, contents, _, err := client.Repositories.GetContents(ctx, source.Owner, source.Repo, source.Path, nil) - if err != nil { - return fmt.Errorf("fech DB info fail:%w", err) - } - for _, v := range contents { - path := v.GetPath() - if strings.Contains(path, ".md") { - if strings.Contains(path, "README.md") { - continue - } - var table Table - if err := table.fetchTableInfo(client, ctx, source.Owner, source.Repo, path); err != nil { - return fmt.Errorf("fech DB info fail:%w", err) + content, _, _, _ := client.Repositories.GetContents(ctx, source.Owner, source.Repo, fmt.Sprintf("%s/schema.json", source.Path), nil) + if content != nil { + text, err := content.GetContent() + if err != nil { + return fmt.Errorf("fetch table info fail:%w", err) + } + var table DB + err = json.Unmarshal([]byte(text), &table) + if err != nil { + fmt.Println(err) + } + d.Tables = table.Tables + } else { + if len(d.Tables) != 0 { + return nil + } + // Processing in the absence of schema.json + _, contents, _, err := client.Repositories.GetContents(ctx, source.Owner, source.Repo, source.Path, nil) + if err != nil { + return fmt.Errorf("fech DB info fail:%w", err) + } + for _, v := range contents { + path := v.GetPath() + if strings.Contains(path, ".md") { + if strings.Contains(path, "README.md") { + continue + } + var table Table + if err := table.fetchTableInfoFromMarkdown(client, ctx, source.Owner, source.Repo, path); err != nil { + return fmt.Errorf("fech DB info fail:%w", err) + } + d.Tables = append(d.Tables, table) } - d.tables = append(d.tables, table) } } return nil diff --git a/trv/source.go b/trv/source.go index 9043408..42cbc6c 100644 --- a/trv/source.go +++ b/trv/source.go @@ -26,10 +26,6 @@ func (s Source) setDbData() (DB, error) { return DB{}, err } - if len(db.tables) != 0 { - return db, nil - } - if err := db.fetchDBInfo(client, ctx, s); err != nil { return DB{}, fmt.Errorf("set DB data fail:%w", err) } diff --git a/trv/table.go b/trv/table.go index c99111e..f26cfd0 100644 --- a/trv/table.go +++ b/trv/table.go @@ -10,16 +10,17 @@ import ( ) type Column struct { - Name string - Type string - Defaul bool - Comment string + Name string `json:"name"` + Type string `json:"type"` + Nullable bool `json:"nullable"` + Defaul bool `json:"default"` + Comment string `json:"comment"` } type Table struct { - Name string - Description string - Columns []Column - UpdateDate time.Time + Name string `json:"name"` + Description string `json:"comment"` + Columns []Column `json:"columns"` + UpdateDate time.Time `json:"tables"` } // return table_name.column_name @@ -27,7 +28,7 @@ func (t Table) getFullName(i int) string { return t.Name + "." + t.Columns[i].Name } -func (t *Table) fetchTableInfo(client *github.Client, ctx context.Context, owner, repo, path string) error { +func (t *Table) fetchTableInfoFromMarkdown(client *github.Client, ctx context.Context, owner, repo, path string) error { content, _, _, err := client.Repositories.GetContents(ctx, owner, repo, path, nil) if err != nil { return fmt.Errorf("fetch table info fail:%w", err) diff --git a/trv/trv.go b/trv/trv.go index 7061eef..01344da 100644 --- a/trv/trv.go +++ b/trv/trv.go @@ -103,7 +103,7 @@ func (t *Trv) setSourceSelecter() error { t.Pages.ShowPage("error") return } - t.Tables = t.DB.tables + t.Tables = t.DB.Tables t.filterList() t.App.SetFocus(t.Searcher) }) @@ -125,7 +125,7 @@ func (t *Trv) addDropdownOption() error { t.SourceSelecter.RemoveOption(currentOptionCount - 1) t.SourceSelecter.AddOption(t.Source[lastOptionIndex], func() { t.DB, err = t.Config.Source[lastOptionIndex].setDbData() - t.Tables = t.DB.tables + t.Tables = t.DB.Tables t.filterList() t.App.SetFocus(t.Searcher) }) From 966ab04847efe26fa1f210b8da57bd0fe0f16a39 Mon Sep 17 00:00:00 2001 From: harakeishi Date: Wed, 8 Feb 2023 23:26:21 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BD=99=E8=A8=88=E3=81=AA=E5=B7=AE?= =?UTF-8?q?=E5=88=86=E3=82=92=E9=99=A4=E5=8E=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trv/table.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/trv/table.go b/trv/table.go index f26cfd0..ca15266 100644 --- a/trv/table.go +++ b/trv/table.go @@ -17,10 +17,10 @@ type Column struct { Comment string `json:"comment"` } type Table struct { - Name string `json:"name"` - Description string `json:"comment"` - Columns []Column `json:"columns"` - UpdateDate time.Time `json:"tables"` + Name string `json:"name"` + Description string `json:"comment"` + Columns []Column `json:"columns"` + UpdateDate time.Time } // return table_name.column_name From c7bd3a637a62983603604fb1db25ae05c4ca2206 Mon Sep 17 00:00:00 2001 From: harakeishi Date: Wed, 8 Feb 2023 23:27:28 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=81=84=E3=81=AA=E3=81=84=E3=83=95=E3=82=A3=E3=83=BC=E3=83=AB?= =?UTF-8?q?=E3=83=89=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trv/table.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/trv/table.go b/trv/table.go index ca15266..55b25f6 100644 --- a/trv/table.go +++ b/trv/table.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "strings" - "time" "github.com/google/go-github/github" ) @@ -20,7 +19,6 @@ type Table struct { Name string `json:"name"` Description string `json:"comment"` Columns []Column `json:"columns"` - UpdateDate time.Time } // return table_name.column_name From f5d230bec6564d722683c0f1dd8a7386fc7477ea Mon Sep 17 00:00:00 2001 From: harakeishi Date: Fri, 10 Feb 2023 23:39:06 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=8F?= =?UTF-8?q?=E3=83=B3=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trv/db.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trv/db.go b/trv/db.go index 5c95c76..c4e8f47 100644 --- a/trv/db.go +++ b/trv/db.go @@ -58,7 +58,7 @@ func (d *DB) fetchDBInfo(client *github.Client, ctx context.Context, source Sour var table DB err = json.Unmarshal([]byte(text), &table) if err != nil { - fmt.Println(err) + return fmt.Errorf("fetch table info fail:%w", err) } d.Tables = table.Tables } else {