Skip to content

Commit

Permalink
parser: respect TiDB comment when DROP INDEX IF EXISTS
Browse files Browse the repository at this point in the history
  • Loading branch information
lance6716 committed Nov 26, 2021
1 parent 51b7a19 commit f20e4c0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
9 changes: 8 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,13 @@ type DropIndexStmt struct {
func (n *DropIndexStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("DROP INDEX ")
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
if ctx.Flags.HasTiDBSpecialCommentFlag() {
ctx.WriteWithSpecialComments("", func() {
ctx.WriteKeyWord("IF EXISTS")
})
} else {
ctx.WriteKeyWord("IF EXISTS ")
}
}
ctx.WriteName(n.IndexName)
ctx.WriteKeyWord(" ON ")
Expand Down
27 changes: 26 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,27 @@ 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)
}
}

0 comments on commit f20e4c0

Please sign in to comment.