Skip to content

Commit

Permalink
executor: let information_schema be the first database in ShowDatabases
Browse files Browse the repository at this point in the history
  • Loading branch information
parastorli committed Oct 23, 2018
1 parent edaec7b commit 50c287b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
30 changes: 30 additions & 0 deletions executor/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,33 @@ func (s *pkgTestSuite) TestNestedLoopApply(c *C) {
}
}
}

func (s *pkgTestSuite) TestMoveInfoSchemaToFront(c *C) {
dbss := [][]string{
{},
{"A", "B", "C", "a", "b", "c"},
{"A", "B", "C", "INFORMATION_SCHEMA"},
{"A", "B", "INFORMATION_SCHEMA", "a"},
{"INFORMATION_SCHEMA"},
{"A", "B", "C", "INFORMATION_SCHEMA", "a", "b"},
}
wanted := [][]string{
{},
{"A", "B", "C", "a", "b", "c"},
{"INFORMATION_SCHEMA", "A", "B", "C"},
{"INFORMATION_SCHEMA", "A", "B", "a"},
{"INFORMATION_SCHEMA"},
{"INFORMATION_SCHEMA", "A", "B", "C", "a", "b"},
}

for _, dbs := range dbss {
moveInfoSchemaToFront(dbs)
}

for i, dbs := range wanted {
c.Check(len(dbss[i]), Equals, len(dbs))
for j, db := range dbs {
c.Check(dbss[i][j], Equals, db)
}
}
}
16 changes: 15 additions & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,25 @@ func (e *ShowExec) fetchShowEngines() error {
return nil
}

// moveInfoSchemaToFront moves information_schema to the first, and the others are sorted in the origin ascending order.
func moveInfoSchemaToFront(dbs []string) {
if len(dbs) > 0 && strings.EqualFold(dbs[0], "INFORMATION_SCHEMA") {
return
}

i := sort.SearchStrings(dbs, "INFORMATION_SCHEMA")
if i < len(dbs) && strings.EqualFold(dbs[i], "INFORMATION_SCHEMA") {
copy(dbs[1:i+1], dbs[0:i])
dbs[0] = "INFORMATION_SCHEMA"
}
}

func (e *ShowExec) fetchShowDatabases() error {
dbs := e.is.AllSchemaNames()
checker := privilege.GetPrivilegeManager(e.ctx)
// TODO: let information_schema be the first database
sort.Strings(dbs)
// let information_schema be the first database
moveInfoSchemaToFront(dbs)
for _, d := range dbs {
if checker != nil && !checker.DBIsVisible(d) {
continue
Expand Down
24 changes: 24 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,30 @@ func (s *testSuite) TestShowVisibility(c *C) {
tk.MustExec("drop database showdatabase")
}

func (s *testSuite) TestShowDatabasesInfoSchemaFirst(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustQuery("show databases").Check(testkit.Rows("INFORMATION_SCHEMA"))
tk.MustExec(`create user 'show'@'%'`)
tk.MustExec(`flush privileges`)

tk.MustExec(`create database AAAA`)
tk.MustExec(`create database BBBB`)
tk.MustExec(`grant select on AAAA.* to 'show'@'%'`)
tk.MustExec(`grant select on BBBB.* to 'show'@'%'`)
tk.MustExec(`flush privileges`)

tk1 := testkit.NewTestKit(c, s.store)
se, err := session.CreateSession4Test(s.store)
c.Assert(err, IsNil)
c.Assert(se.Auth(&auth.UserIdentity{Username: "show", Hostname: "%"}, nil, nil), IsTrue)
tk1.Se = se
tk1.MustQuery("show databases").Check(testkit.Rows("INFORMATION_SCHEMA", "AAAA", "BBBB"))

tk.MustExec(`drop user 'show'@'%'`)
tk.MustExec(`drop database AAAA`)
tk.MustExec(`drop database BBBB`)
}

// mockSessionManager is a mocked session manager that wraps one session
// it returns only this session's current process info as processlist for test.
type mockSessionManager struct {
Expand Down

0 comments on commit 50c287b

Please sign in to comment.