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

parser: respect TiDB comment when DROP INDEX IF EXISTS #30173

Merged
merged 4 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package ast

import (
"github.com/pingcap/errors"

"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/parser/format"
"github.com/pingcap/tidb/parser/model"
Expand Down Expand Up @@ -1689,7 +1690,9 @@ type DropIndexStmt struct {
func (n *DropIndexStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("DROP INDEX ")
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
ctx.WriteWithSpecialComments("", func() {
ctx.WriteKeyWord("IF EXISTS ")
})
}
ctx.WriteName(n.IndexName)
ctx.WriteKeyWord(" ON ")
Expand Down
26 changes: 25 additions & 1 deletion parser/ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package ast_test
import (
"testing"

"github.com/stretchr/testify/require"

. "github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/format"
"github.com/stretchr/testify/require"
)

func TestDDLVisitorCover(t *testing.T) {
Expand Down Expand Up @@ -623,3 +624,26 @@ func TestSequenceRestore(t *testing.T) {
}
runNodeRestoreTest(t, testCases, "%s", extractNodeFunc)
}

func TestDropIndexRestore(t *testing.T) {
t.Parallel()
sourceSQL := "drop index if exists idx on t"
cases := []struct {
flags format.RestoreFlags
expectSQL string
}{
{format.DefaultRestoreFlags, "DROP INDEX IF EXISTS `idx` ON `t`"},
{format.DefaultRestoreFlags | format.RestoreTiDBSpecialComment, "DROP INDEX /*T! IF EXISTS */`idx` ON `t`"},
}

extractNodeFunc := func(node Node) Node {
return node
}

for _, ca := range cases {
testCases := []NodeRestoreTestCase{
{sourceSQL, ca.expectSQL},
}
runNodeRestoreTestWithFlags(t, testCases, "%s", extractNodeFunc, ca.flags)
}
}