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

Max/drop pk #83

Merged
merged 5 commits into from
Aug 2, 2021
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
24 changes: 21 additions & 3 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,7 @@ const (
FulltextStr = "fulltext"
SetStr = "set"
TemporaryStr = "temporary"
PrimaryStr = "primary"
)

// Format formats the node.
Expand Down Expand Up @@ -2295,12 +2296,25 @@ func (idx *IndexSpec) Format(buf *TrackedBuffer) {
case "create", "CREATE":
buf.Myprintf("add ")
if idx.Type != "" {
buf.Myprintf("%s ", idx.Type)
if idx.Type == PrimaryStr {
if idx.ToName.val == "" {
buf.Myprintf("primary key ")
} else {
buf.Myprintf("constraint %s primary key ", idx.ToName.val)
}
} else {
buf.Myprintf("%s ", idx.Type)
}
}

if idx.Type != PrimaryStr {
buf.Myprintf("index %s ", idx.ToName.val)
}
buf.Myprintf("index %s ", idx.ToName.val)

if idx.Using.val != "" {
buf.Myprintf("using %s ", idx.Using.val)
}

buf.Myprintf("(")
for i, col := range idx.Columns {
if i != 0 {
Expand All @@ -2325,7 +2339,11 @@ func (idx *IndexSpec) Format(buf *TrackedBuffer) {
}
}
case "drop", "DROP":
buf.Myprintf("drop index %s", idx.ToName.val)
if idx.Type == PrimaryStr {
buf.Myprintf("drop primary key")
} else {
buf.Myprintf("drop index %s", idx.ToName.val)
}
case "rename", "RENAME":
buf.Myprintf("rename index %s to %s", idx.FromName.val, idx.ToName.val)
}
Expand Down
10 changes: 5 additions & 5 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,8 +1188,11 @@ var (
input: "alter table a add foreign key (x) references y(z)",
output: "alter table a add foreign key (x) references y (z)",
}, {
input: "alter table a add primary key",
output: "alter table a",
input: "alter table a add primary key (a, b)",
}, {
input: "alter table a add constraint a_pk primary key (a, b)",
}, {
input: "alter table a drop primary key",
}, {
input: "alter table a drop column id",
}, {
Expand Down Expand Up @@ -1219,9 +1222,6 @@ var (
input: "alter table a drop check ch_1",
}, {
input: "alter table a drop foreign key fk_something",
}, {
input: "alter table a drop primary key",
output: "alter table a",
}, {
input: "alter table a drop constraint b",
}, {
Expand Down
Loading