-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from vitessio/rohit/reference2
Add table row count to summary output
- Loading branch information
Showing
19 changed files
with
1,611 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"vitess.io/vitess/go/mysql" | ||
|
||
"github.com/vitessio/vt/go/schema" | ||
) | ||
|
||
func schemaCmd() *cobra.Command { | ||
var vtParams mysql.ConnParams | ||
|
||
cmd := &cobra.Command{ | ||
Use: "schema ", | ||
Short: "Loads info from the database including row counts", | ||
Example: "vt schema", | ||
Args: cobra.ExactArgs(0), | ||
RunE: func(_ *cobra.Command, _ []string) error { | ||
cfg := schema.Config{ | ||
VTParams: vtParams, | ||
} | ||
|
||
return schema.Run(cfg) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&vtParams.Host, "host", "", "127.0.0.1", "Database host") | ||
cmd.Flags().IntVarP(&vtParams.Port, "port", "", 3306, "Database port") | ||
cmd.Flags().StringVarP(&vtParams.Uname, "user", "", "root", "Database user") | ||
cmd.Flags().StringVarP(&vtParams.Pass, "password", "", "", "Database password") | ||
cmd.Flags().StringVarP(&vtParams.DbName, "database", "", "", "Database name") | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"vitess.io/vitess/go/mysql" | ||
) | ||
|
||
type Config struct { | ||
VTParams mysql.ConnParams | ||
} | ||
|
||
func Run(cfg Config) error { | ||
return run(os.Stdout, cfg) | ||
} | ||
|
||
func run(out io.Writer, cfg Config) error { | ||
si, err := Get(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
b, err := json.MarshalIndent(si, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
_, err = out.Write(b) | ||
return err | ||
} | ||
|
||
type TableInfo struct { | ||
Name string `json:"name"` | ||
Rows int `json:"rows"` | ||
} | ||
|
||
type Info struct { | ||
FileType string `json:"fileType"` | ||
Tables []TableInfo `json:"tables"` | ||
} | ||
|
||
func Get(cfg Config) (*Info, error) { | ||
vtParams := &mysql.ConnParams{ | ||
Host: cfg.VTParams.Host, | ||
Port: cfg.VTParams.Port, | ||
Uname: cfg.VTParams.Uname, | ||
Pass: cfg.VTParams.Pass, | ||
DbName: cfg.VTParams.DbName, | ||
} | ||
|
||
vtConn, err := mysql.Connect(context.Background(), vtParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer vtConn.Close() | ||
queryTableSizes := "SELECT table_name, table_rows FROM information_schema.tables WHERE table_schema = '%s' and table_type = 'BASE TABLE'" | ||
qr, err := vtConn.ExecuteFetch(fmt.Sprintf(queryTableSizes, cfg.VTParams.DbName), -1, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var tables []TableInfo | ||
for _, row := range qr.Rows { | ||
tableName := row[0].ToString() | ||
tableRows, _ := row[1].ToInt64() | ||
tables = append(tables, TableInfo{ | ||
Name: tableName, | ||
Rows: int(tableRows), | ||
}) | ||
} | ||
schemaInfo := &Info{ | ||
Tables: tables, | ||
} | ||
return schemaInfo, nil | ||
} | ||
|
||
func Load(fileName string) (*Info, error) { | ||
b, err := os.ReadFile(fileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var si Info | ||
err = json.Unmarshal(b, &si) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &si, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSchema(t *testing.T) { | ||
si, err := Load("../testdata/sakila-schema-info.json") | ||
require.NoError(t, err) | ||
require.NotNil(t, si) | ||
require.NotEmpty(t, si.Tables) | ||
require.Len(t, si.Tables, 16) | ||
var tables []string | ||
for _, table := range si.Tables { | ||
tables = append(tables, table.Name) | ||
} | ||
require.Contains(t, tables, "actor") | ||
require.NotContains(t, tables, "foo") | ||
for _, table := range si.Tables { | ||
require.NotEmpty(t, table.Name) | ||
switch table.Name { | ||
case "language": | ||
require.Equal(t, 6, table.Rows) | ||
case "film": | ||
require.Equal(t, 1000, table.Rows) | ||
default: | ||
require.Positive(t, table.Rows, "table %s has no rows", table.Name) | ||
} | ||
} | ||
} |
Oops, something went wrong.