Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: let information_schema be the first database in ShowDatabases #7938

Merged
merged 3 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'@'%'`)
parastorli marked this conversation as resolved.
Show resolved Hide resolved
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)
parastorli marked this conversation as resolved.
Show resolved Hide resolved
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