Skip to content

Commit

Permalink
infoschema: fix load drop database schema bug and refine db-table api…
Browse files Browse the repository at this point in the history
… error. (#11573) (#11585)
  • Loading branch information
crazycs520 authored and sre-bot committed Aug 5, 2019
1 parent 785cb24 commit 9d15c8d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
12 changes: 6 additions & 6 deletions infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,22 @@ func (b *Builder) applyDropSchema(schemaID int64) []int64 {
delete(b.is.schemaMap, di.Name.L)

// Copy the sortedTables that contain the table we are going to drop.
tableIDs := make([]int64, 0, len(di.Tables))
bucketIdxMap := make(map[int]struct{})
for _, tbl := range di.Tables {
bucketIdxMap[tableBucketIdx(tbl.ID)] = struct{}{}
// TODO: If the table ID doesn't exist.
tableIDs = append(tableIDs, tbl.ID)
}
for bucketIdx := range bucketIdxMap {
b.copySortedTablesBucket(bucketIdx)
}

ids := make([]int64, 0, len(di.Tables))
di = di.Clone()
for _, tbl := range di.Tables {
b.applyDropTable(di, tbl.ID)
// TODO: If the table ID doesn't exist.
ids = append(ids, tbl.ID)
for _, id := range tableIDs {
b.applyDropTable(di, id)
}
return ids
return tableIDs
}

func (b *Builder) copySortedTablesBucket(bucketIdx int) {
Expand Down
20 changes: 20 additions & 0 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/parser/auth"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -502,3 +504,21 @@ func (s *testTableSuite) TestForAnalyzeStatus(c *C) {
c.Assert(result.Rows()[1][5], NotNil)
c.Assert(result.Rows()[1][6], Equals, "finished")
}

func (s *testTableSuite) TestReloadDropDatabase(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create database test_dbs")
tk.MustExec("use test_dbs")
tk.MustExec("create table t1 (a int)")
tk.MustExec("create table t2 (a int)")
tk.MustExec("create table t3 (a int)")
is := domain.GetDomain(tk.Se).InfoSchema()
t2, err := is.TableByName(model.NewCIStr("test_dbs"), model.NewCIStr("t2"))
c.Assert(err, IsNil)
tk.MustExec("drop database test_dbs")
is = domain.GetDomain(tk.Se).InfoSchema()
_, err = is.TableByName(model.NewCIStr("test_dbs"), model.NewCIStr("t2"))
c.Assert(terror.ErrorEqual(infoschema.ErrTableNotExists, err), IsTrue)
_, ok := is.TableByID(t2.Meta().ID)
c.Assert(ok, IsFalse)
}
4 changes: 2 additions & 2 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1534,8 +1534,8 @@ func (h dbTableHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
dbTblInfo.TableInfo = tbl.Meta()
dbInfo, ok := schema.SchemaByTable(dbTblInfo.TableInfo)
if !ok {
log.Warnf("can not find the database of table id: %v, table name: %v", dbTblInfo.TableInfo.ID, dbTblInfo.TableInfo.Name)
writeData(w, dbTblInfo)
logutil.Logger(context.Background()).Error("can not find the database of the table", zap.Int64("table id", dbTblInfo.TableInfo.ID), zap.String("table name", dbTblInfo.TableInfo.Name.L))
writeError(w, infoschema.ErrTableNotExists.GenWithStack("Table which ID = %s does not exist.", tableID))
return
}
dbTblInfo.DBInfo = dbInfo
Expand Down

0 comments on commit 9d15c8d

Please sign in to comment.