Skip to content

Commit

Permalink
*: fix "create or replace view" infoschema is inconsistent (pingcap#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored Feb 20, 2020
1 parent d0ad254 commit 3cd5fb6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
20 changes: 17 additions & 3 deletions ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,20 +735,34 @@ func updateSchemaVersion(t *meta.Meta, job *model.Job) (int64, error) {
Type: job.Type,
SchemaID: job.SchemaID,
}
if job.Type == model.ActionTruncateTable {
switch job.Type {
case model.ActionTruncateTable:
// Truncate table has two table ID, should be handled differently.
err = job.DecodeArgs(&diff.TableID)
if err != nil {
return 0, errors.Trace(err)
}
diff.OldTableID = job.TableID
} else if job.Type == model.ActionRenameTable {
case model.ActionCreateView:
tbInfo := &model.TableInfo{}
var orReplace bool
var oldTbInfoID int64
if err := job.DecodeArgs(tbInfo, &orReplace, &oldTbInfoID); err != nil {
return 0, errors.Trace(err)
}
// When the statement is "create or replace view " and we need to drop the old view,
// it has two table IDs and should be handled differently.
if oldTbInfoID > 0 && orReplace {
diff.OldTableID = oldTbInfoID
}
diff.TableID = tbInfo.ID
case model.ActionRenameTable:
err = job.DecodeArgs(&diff.OldSchemaID)
if err != nil {
return 0, errors.Trace(err)
}
diff.TableID = job.TableID
} else {
default:
diff.TableID = job.TableID
}
err = t.SetSchemaDiff(diff)
Expand Down
7 changes: 7 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ func (s *testSuite6) TestCreateDropView(c *C) {
tk.MustExec("create table t_v(a int)")
_, err = tk.Exec("drop view t_v")
c.Assert(err.Error(), Equals, "[ddl:1347]'test.t_v' is not VIEW")

tk.MustExec("create table t_v1(a int, b int);")
tk.MustExec("create table t_v2(a int, b int);")
tk.MustExec("create view v as select * from t_v1;")
tk.MustExec("create or replace view v as select * from t_v2;")
tk.MustQuery("select * from information_schema.views where table_name ='v';").Check(
testkit.Rows("def test v SELECT `test`.`t_v2`.`a`,`test`.`t_v2`.`b` FROM `test`.`t_v2` CASCADED NO @ DEFINER utf8mb4 utf8mb4_bin"))
}

func (s *testSuite6) TestCreateDropIndex(c *C) {
Expand Down
2 changes: 1 addition & 1 deletion infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro
newTableID = diff.TableID
case model.ActionDropTable, model.ActionDropView, model.ActionDropSequence:
oldTableID = diff.TableID
case model.ActionTruncateTable:
case model.ActionTruncateTable, model.ActionCreateView:
oldTableID = diff.OldTableID
newTableID = diff.TableID
default:
Expand Down

0 comments on commit 3cd5fb6

Please sign in to comment.