Skip to content
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

Allow adding autoincrement column via "ALTER VSCHEMA..." statement #5094

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,9 @@ type DDL struct {

// VindexCols is set for AddColVindexStr.
VindexCols []ColIdent

// AutoIncSpec is set for AddAutoIncStr.
AutoIncSpec *AutoIncSpec
}

// DDL strings.
Expand All @@ -756,6 +759,7 @@ const (
AddColVindexStr = "on table add vindex"
DropColVindexStr = "on table drop vindex"
AddSequenceStr = "add sequence"
AddAutoIncStr = "add auto_increment"

// Vindex DDL param to specify the owner of a vindex
VindexOwnerStr = "owner"
Expand Down Expand Up @@ -816,6 +820,8 @@ func (node *DDL) Format(buf *TrackedBuffer) {
buf.Myprintf("alter vschema on %v drop vindex %v", node.Table, node.VindexSpec.Name)
case AddSequenceStr:
buf.Myprintf("alter vschema add sequence %v", node.Table)
case AddAutoIncStr:
buf.Myprintf("alter vschema on %v add auto_increment %v", node.Table, node.AutoIncSpec)
default:
buf.Myprintf("%s table %v", node.Action, node.Table)
}
Expand Down Expand Up @@ -1355,6 +1361,23 @@ type VindexSpec struct {
Params []VindexParam
}

// AutoIncSpec defines and autoincrement value for a ADD AUTO_INCREMENT statement
type AutoIncSpec struct {
Column ColIdent
Sequence TableName
}

// Format formats the node.
func (node *AutoIncSpec) Format(buf *TrackedBuffer) {
buf.Myprintf("%v ", node.Column)
buf.Myprintf("using %v", node.Sequence)
}

func (node *AutoIncSpec) walkSubtree(visit Visit) error {
err := Walk(visit, node.Sequence, node.Column)
return err
}

// ParseParams parses the vindex parameter list, pulling out the special-case
// "owner" parameter
func (node *VindexSpec) ParseParams() (string, map[string]string) {
Expand Down
2 changes: 2 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,8 @@ var (
input: "alter vschema add table a",
}, {
input: "alter vschema add sequence a_seq",
}, {
input: "alter vschema on a add auto_increment id using a_seq",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this syntax!

}, {
input: "alter vschema drop table a",
}, {
Expand Down
Loading