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

support rename constraint syntax #330

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,15 @@ func (node *DDL) alterFormat(buf *TrackedBuffer) {
default:
buf.Myprintf(" drop constraint %s", node.TableSpec.Constraints[0].Name)
}
} else if node.ConstraintAction == RenameStr && node.TableSpec != nil && len(node.TableSpec.Constraints) == 2 {
switch node.TableSpec.Constraints[0].Details.(type) {
case *ForeignKeyDefinition:
buf.Myprintf(" rename constraint %s to", node.TableSpec.Constraints[0].Name)
}
switch node.TableSpec.Constraints[1].Details.(type) {
case *ForeignKeyDefinition:
buf.Myprintf(" %s", node.TableSpec.Constraints[1].Name)
}
Copy link

Choose a reason for hiding this comment

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

Do we need to switch on the type of Details? I see that this field will currently always be a ForeignKeyDefinition, but it seems like that would be likely to change in the future if we expanded this to support renaming other constraints, and then this code wouldn't print out the statement correctly anymore. Not a huge deal, since we should catch that in development, but seems like it could be simpler if we just printed out the constraint name without asserting the type.

} else if node.DefaultSpec != nil {
buf.Myprintf(" %v", node.DefaultSpec)
} else if node.AlterCollationSpec != nil {
Expand Down
12 changes: 9 additions & 3 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1627,11 +1627,17 @@ var (
input: "alter table a drop check status",
}, {
input: "alter table a drop constraint status",
}, {
},
{
input: "alter table a drop foreign key fk_something",
}, {
},
{
input: "alter table a drop constraint b",
}, {
},
{
input: "alter table a rename constraint oldfk to newfk",
},
{
input: "alter table a drop id",
output: "alter table a drop column id",
}, {
Expand Down
Loading
Loading