Skip to content

Commit

Permalink
Add OnlineDDL show support
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Mason <andrew@planetscale.com>
  • Loading branch information
Andrew Mason committed Aug 7, 2023
1 parent 54f0b33 commit 4da799d
Show file tree
Hide file tree
Showing 15 changed files with 24,572 additions and 17,054 deletions.
119 changes: 119 additions & 0 deletions go/cmd/vtctldclient/command/onlineddl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Copyright 2023 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 (
"fmt"
"strings"
"time"

"github.com/spf13/cobra"

"vitess.io/vitess/go/cmd/vtctldclient/cli"
"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/vt/schema"
"vitess.io/vitess/go/vt/vtctl/schematools"

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

var (
OnlineDDL = &cobra.Command{
Use: "OnlineDDL <cmd> <keyspace> [args]",
DisableFlagsInUseLine: true,
Args: cobra.MinimumNArgs(2),
}
OnlineDDLShow = &cobra.Command{
Use: "show",
DisableFlagsInUseLine: true,
Args: cobra.RangeArgs(0, 1),
RunE: commandOnlineDDLShow,
}
)

var onlineDDLShowArgs = struct {
JSON bool
OrderStr string
Limit uint64
Skip uint64
}{
OrderStr: "ascending",
}

func commandOnlineDDLShow(cmd *cobra.Command, args []string) error {
var order vtctldatapb.QueryOrdering
switch strings.ToLower(onlineDDLShowArgs.OrderStr) {
case "":
order = vtctldatapb.QueryOrdering_NONE
case "asc", "ascending":
order = vtctldatapb.QueryOrdering_ASCENDING
case "desc", "descending":
order = vtctldatapb.QueryOrdering_DESCENDING
default:
return fmt.Errorf("invalid ordering %s (choices are 'asc', 'ascending', 'desc', 'descending')", onlineDDLShowArgs.OrderStr)
}

cli.FinishedParsing(cmd)

req := &vtctldatapb.GetSchemaMigrationsRequest{
Keyspace: cmd.Flags().Arg(0),
Order: order,
Limit: onlineDDLShowArgs.Limit,
Skip: onlineDDLShowArgs.Skip,
}

switch arg := cmd.Flags().Arg(1); arg {
case "", "all":
case "recent":
req.Recent = protoutil.DurationToProto(7 * 24 * time.Hour)
default:
if status, err := schematools.ParseSchemaMigrationStatus(arg); err == nil {
// Argument is a status name.
req.Status = status
} else if schema.IsOnlineDDLUUID(arg) {
req.Uuid = arg
} else {
req.MigrationContext = arg
}
}

resp, err := client.GetSchemaMigrations(commandCtx, req)
if err != nil {
return err
}

switch {
case onlineDDLShowArgs.JSON:
data, err := cli.MarshalJSON(resp)
if err != nil {
return err
}
fmt.Printf("%s\n", data)
default:
// TODO: support tabular/textual format
}
return nil
}

func init() {
OnlineDDLShow.Flags().BoolVar(&onlineDDLShowArgs.JSON, "json", false, "TODO")
OnlineDDLShow.Flags().StringVar(&onlineDDLShowArgs.OrderStr, "order", "asc", "TODO")
OnlineDDLShow.Flags().Uint64Var(&onlineDDLShowArgs.Limit, "limit", 0, "TODO")
OnlineDDLShow.Flags().Uint64Var(&onlineDDLShowArgs.Skip, "skip", 0, "TODO")

OnlineDDL.AddCommand(OnlineDDLShow)
Root.AddCommand(OnlineDDL)
}
11 changes: 11 additions & 0 deletions go/sqltypes/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ func (v Value) ToInt() (int, error) {
return strconv.Atoi(v.RawStr())
}

// ToFloat32 first parses the value as MySQL would return it as a float64, then
// downcasts it to a float32. It may overflow if the value exceeds 32 bits.
func (v Value) ToFloat32() (float32, error) {
f64, err := v.ToFloat64()
if err != nil {
return 0, err
}

return float32(f64), nil
}

// ToFloat64 returns the value as MySQL would return it as a float64.
func (v Value) ToFloat64() (float64, error) {
if !IsNumber(v.typ) {
Expand Down
Loading

0 comments on commit 4da799d

Please sign in to comment.