From 1654f37d6b94a48c9359ab851fb2a86bccd83909 Mon Sep 17 00:00:00 2001 From: sylzd Date: Mon, 22 Nov 2021 16:55:47 +0800 Subject: [PATCH] forbit alter table cache --- ddl/db_test.go | 24 ++++++++++++++++++++++++ ddl/ddl_api.go | 5 ++++- ddl/error.go | 1 + 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/ddl/db_test.go b/ddl/db_test.go index e2b70c1791225..173ffa2679e4b 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -5664,6 +5664,30 @@ func (s *testSerialDBSuite) TestSetTableFlashReplica(c *C) { c.Assert(err.Error(), Equals, "the tiflash replica count: 2 should be less than the total tiflash server count: 0") } +func (s *testSerialDBSuite) TestForbitCacheTableForSystemTable(c *C) { + tk := testkit.NewTestKit(c, s.store) + sysTables := make([]string, 0, 24) + memOrSysDB := []string{"MySQL", "INFORMATION_SCHEMA", "PERFORMANCE_SCHEMA", "METRICS_SCHEMA"} + for _, db := range memOrSysDB { + tk.MustExec("use " + db) + tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil) + rows := tk.MustQuery("show tables").Rows() + for i := 0; i < len(rows); i++ { + sysTables = append(sysTables, rows[i][0].(string)) + } + for _, one := range sysTables { + _, err := tk.Exec(fmt.Sprintf("alter table `%s` cache", one)) + if db == "MySQL" { + c.Assert(err.Error(), Equals, "[ddl:8200]ALTER table cache for tables in system database is currently unsupported") + } else { + c.Assert(err.Error(), Equals, fmt.Sprintf("[planner:1142]ALTER command denied to user 'root'@'%%' for table '%s'", strings.ToLower(one))) + } + + } + sysTables = sysTables[:0] + } +} + func (s *testSerialDBSuite) TestSetTableFlashReplicaForSystemTable(c *C) { tk := testkit.NewTestKit(c, s.store) sysTables := make([]string, 0, 24) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 76d9a786988cd..fd12090bc8b19 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -6606,7 +6606,10 @@ func (d *ddl) AlterTableCache(ctx sessionctx.Context, ti ast.Ident) (err error) return nil } - if t.Meta().TempTableType != model.TempTableNone { + // forbit cache table in system database. + if util.IsMemOrSysDB(schema.Name.L) { + return errors.Trace(errUnsupportedAlterCacheForSysTable) + } else if t.Meta().TempTableType != model.TempTableNone { return errors.Trace(ErrOptOnTemporaryTable.GenWithStackByArgs("alter temporary table cache")) } diff --git a/ddl/error.go b/ddl/error.go index 995f8ce5db1e7..b3eeb9655fc77 100644 --- a/ddl/error.go +++ b/ddl/error.go @@ -50,6 +50,7 @@ var ( errUnsupportedAlterTableWithoutValidation = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("ALTER TABLE WITHOUT VALIDATION is currently unsupported", nil)) errUnsupportedAlterTableOption = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("This type of ALTER TABLE is currently unsupported", nil)) errUnsupportedAlterReplicaForSysTable = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("ALTER table replica for tables in system database is currently unsupported", nil)) + errUnsupportedAlterCacheForSysTable = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("ALTER table cache for tables in system database is currently unsupported", nil)) errBlobKeyWithoutLength = dbterror.ClassDDL.NewStd(mysql.ErrBlobKeyWithoutLength) errKeyPart0 = dbterror.ClassDDL.NewStd(mysql.ErrKeyPart0) errIncorrectPrefixKey = dbterror.ClassDDL.NewStd(mysql.ErrWrongSubKey)