-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add OnlineDDL show support #13738
Add OnlineDDL show support #13738
Changes from 3 commits
4da799d
b2bf58f
2a11c41
d67a012
3bdfb6f
3a68dbc
98f6fcf
d11adfd
c13e65f
bcc4dac
3f3ede7
1c8df07
eec7f87
73c485a
8ab6696
3addca0
d37da34
495ee03
56271d2
ef39e58
9472588
0677d9c
22d6c13
370aa09
8e58aec
0785e2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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", | ||
ajm188 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to at least best-effort match the format that a mysql query provides, which we have currently lost in the new client |
||
} | ||
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") | ||
ajm188 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
OnlineDDL.AddCommand(OnlineDDLShow) | ||
Root.AddCommand(OnlineDDL) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be a bit clunkier for me to do this inline in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No strong opinions. If we keep this here, though, people may start using the function without realizing it loses precision. So perhaps better off moved to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, using |
||
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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately cobra won't support a positional argument before the subcommand name (or at least i couldn't see an easy/obvious way)