Skip to content

Commit

Permalink
Implement CLI command, refactor legacy command to use new RPC
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Mason <[email protected]>
  • Loading branch information
ajm188 committed Jan 22, 2021
1 parent 6e261be commit da1437c
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 18 deletions.
99 changes: 99 additions & 0 deletions go/cmd/vtctldclient/internal/command/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Copyright 2021 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 command

import (
"errors"
"fmt"
"strings"

"github.com/spf13/cobra"

"vitess.io/vitess/go/cmd/vtctldclient/cli"
"vitess.io/vitess/go/vt/topo/topoproto"

vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
)

// GetSchema makes a GetSchema gRPC call to a vtctld.
var GetSchema = &cobra.Command{
Use: "GetSchema [--tables TABLES ...] [--exclude-tables EXCLUDE_TABLES ...] [{--table-names-only | --table-sizes-only}] [--include-views] alias",
Args: cobra.ExactArgs(1),
RunE: commandGetSchema,
}

var getSchemaOptions = struct {
Tables []string
ExcludeTables []string
IncludeViews bool
TableNamesOnly bool
TableSizesOnly bool
}{}

func commandGetSchema(cmd *cobra.Command, args []string) error {
if getSchemaOptions.TableNamesOnly && getSchemaOptions.TableSizesOnly {
return errors.New("can only pass one of --table-names-only and --table-sizes-only")
}

alias, err := topoproto.ParseTabletAlias(cmd.Flags().Arg(0))
if err != nil {
return err
}

resp, err := client.GetSchema(commandCtx, &vtctldatapb.GetSchemaRequest{
TabletAlias: alias,
Tables: getSchemaOptions.Tables,
ExcludeTables: getSchemaOptions.ExcludeTables,
IncludeViews: getSchemaOptions.IncludeViews,
TableNamesOnly: getSchemaOptions.TableNamesOnly,
TableSizesOnly: getSchemaOptions.TableSizesOnly,
})
if err != nil {
return err
}

if getSchemaOptions.TableNamesOnly {
names := make([]string, len(resp.Schema.TableDefinitions))

for i, td := range resp.Schema.TableDefinitions {
names[i] = td.Name
}

fmt.Printf("%s\n", strings.Join(names, "\n"))

return nil
}

data, err := cli.MarshalJSON(resp.Schema)
if err != nil {
return err
}

fmt.Printf("%s\n", data)

return nil
}

func init() {
GetSchema.Flags().StringSliceVar(&getSchemaOptions.Tables, "tables", nil, "TODO")
GetSchema.Flags().StringSliceVar(&getSchemaOptions.ExcludeTables, "exclude-tables", nil, "TODO")
GetSchema.Flags().BoolVar(&getSchemaOptions.IncludeViews, "include-views", false, "TODO")
GetSchema.Flags().BoolVarP(&getSchemaOptions.TableNamesOnly, "table-names-only", "n", false, "TODO")
GetSchema.Flags().BoolVarP(&getSchemaOptions.TableSizesOnly, "table-sizes-only", "s", false, "TODO")

Root.AddCommand(GetSchema)
}
29 changes: 11 additions & 18 deletions go/vt/vtctl/vtctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ import (
"vitess.io/vitess/go/vt/wrangler"

replicationdatapb "vitess.io/vitess/go/vt/proto/replicationdata"
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
Expand Down Expand Up @@ -2673,32 +2672,26 @@ func commandGetSchema(ctx context.Context, wr *wrangler.Wrangler, subFlags *flag
excludeTableArray = strings.Split(*excludeTables, ",")
}

sd, err := wr.GetSchema(ctx, tabletAlias, tableArray, excludeTableArray, *includeViews)
resp, err := wr.VtctldServer().GetSchema(ctx, &vtctldatapb.GetSchemaRequest{
TabletAlias: tabletAlias,
Tables: tableArray,
ExcludeTables: excludeTableArray,
IncludeViews: *includeViews,
TableNamesOnly: *tableNamesOnly,
TableSizesOnly: *tableSizesOnly,
})
if err != nil {
return err
}

if *tableNamesOnly {
for _, td := range sd.TableDefinitions {
for _, td := range resp.Schema.TableDefinitions {
wr.Logger().Printf("%v\n", td.Name)
}
return nil
}

if *tableSizesOnly {
sizeTds := make([]*tabletmanagerdatapb.TableDefinition, len(sd.TableDefinitions))
for i, td := range sd.TableDefinitions {
sizeTds[i] = &tabletmanagerdatapb.TableDefinition{
Name: td.Name,
Type: td.Type,
RowCount: td.RowCount,
DataLength: td.DataLength,
}
}

sd.TableDefinitions = sizeTds
}

return printJSON(wr.Logger(), sd)
return printJSON(wr.Logger(), resp.Schema)
}

func commandReloadSchema(ctx context.Context, wr *wrangler.Wrangler, subFlags *flag.FlagSet, args []string) error {
Expand Down

0 comments on commit da1437c

Please sign in to comment.