Skip to content

Commit

Permalink
ddl: fix a bug that create two views with same name success (#58770)
Browse files Browse the repository at this point in the history
close #58769
  • Loading branch information
tiancaiamao authored Jan 15, 2025
1 parent b16d342 commit aa21818
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/ddl/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ func onCreateView(jobCtx *jobContext, job *model.Job) (ver int64, _ error) {

metaMut := jobCtx.metaMut
oldTableID, err := findTableIDByName(jobCtx.infoCache, metaMut, schemaID, tbInfo.Name.L)
if err == nil && oldTableID > 0 {
err = infoschema.ErrTableExists
}
if infoschema.ErrTableNotExists.Equal(err) {
err = nil
}
Expand All @@ -329,6 +332,7 @@ func onCreateView(jobCtx *jobContext, job *model.Job) (ver int64, _ error) {
return ver, errors.Trace(err)
}
}

ver, err = updateSchemaVersion(jobCtx, job)
if err != nil {
return ver, errors.Trace(err)
Expand Down
24 changes: 24 additions & 0 deletions pkg/ddl/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,27 @@ func TestDropTableAccessibleInInfoSchema(t *testing.T) {
}
require.True(t, len(errs) > 0)
}

func TestCreateViewTwice(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk2 := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t_raw (id int)")
tk2.MustExec("use test")

var wg sync.WaitGroup
wg.Add(1)
first := true
testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/beforeDeliveryJob", func(job *model.Job) {
if first {
first = false
go func() {
defer wg.Done()
tk2.MustExecToErr("create view v as select * from t_raw where id > 666")
}()
}
})
tk.MustExec("create view v as select * from t_raw")
wg.Wait()
}

0 comments on commit aa21818

Please sign in to comment.