diff --git a/infoschema/builder.go b/infoschema/builder.go index 4276d9ae6eb4a..91df02829545e 100644 --- a/infoschema/builder.go +++ b/infoschema/builder.go @@ -155,22 +155,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) { diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index 4eb31e49f0f22..e9fd1ca220334 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -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" @@ -533,3 +535,21 @@ select * from t_slim;`)) rows := re.Rows() c.Assert(rows[0][0], Equals, sql) } + +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) +} diff --git a/server/http_handler.go b/server/http_handler.go index bd380afb9d979..0a048dc4bb134 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -1443,8 +1443,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