Skip to content

Commit

Permalink
*: Support Reset database placement options by ALTER (pingcap#28463)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylzd authored Oct 18, 2021
1 parent 46b87ac commit 271df16
Show file tree
Hide file tree
Showing 7 changed files with 7,601 additions and 7,532 deletions.
3 changes: 3 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6491,6 +6491,9 @@ func buildPolicyInfo(name model.CIStr, options []*ast.PlacementOption) (*model.P

func (d *ddl) CreatePlacementPolicy(ctx sessionctx.Context, stmt *ast.CreatePlacementPolicyStmt) (err error) {
policyName := stmt.PolicyName
if policyName.L == "default" {
return errors.Trace(infoschema.ErrReservedSyntax.GenWithStackByArgs(policyName))
}
is := d.GetInfoSchemaWithInterceptor(ctx)
// Check policy existence.
_, ok := is.PolicyByName(policyName)
Expand Down
54 changes: 54 additions & 0 deletions ddl/placement_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testutil"
)

func (s *testDBSuite6) TestPlacementPolicy(c *C) {
Expand Down Expand Up @@ -193,6 +194,59 @@ func (s *testDBSuite6) TestPlacementValidation(c *C) {
tk.MustExec("drop placement policy x")
}

func (s *testDBSuite6) TestResetSchemaPlacement(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("drop database if exists TestResetPlacementDB;")
tk.MustExec("create placement policy `TestReset` followers=4;")
tk.MustGetErrCode("create placement policy `default` followers=4;", mysql.ErrReservedSyntax)
tk.MustGetErrCode("create placement policy default followers=4;", mysql.ErrParse)

tk.MustExec("create database TestResetPlacementDB placement policy `TestReset`;")
tk.MustExec("use TestResetPlacementDB")
// Test for `=default`
tk.MustQuery(`show create database TestResetPlacementDB`).Check(testutil.RowsWithSep("|",
"TestResetPlacementDB CREATE DATABASE `TestResetPlacementDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+
"/*T![placement] PLACEMENT POLICY=`TestReset` */",
))
tk.MustExec("ALTER DATABASE TestResetPlacementDB PLACEMENT POLICY=default;")
tk.MustQuery(`show create database TestResetPlacementDB`).Check(testutil.RowsWithSep("|",
"TestResetPlacementDB CREATE DATABASE `TestResetPlacementDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */",
))
// Test for `SET DEFAULT`
tk.MustExec("ALTER DATABASE TestResetPlacementDB PLACEMENT POLICY=`TestReset`;")
tk.MustQuery(`show create database TestResetPlacementDB`).Check(testutil.RowsWithSep("|",
"TestResetPlacementDB CREATE DATABASE `TestResetPlacementDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+
"/*T![placement] PLACEMENT POLICY=`TestReset` */",
))
tk.MustExec("ALTER DATABASE TestResetPlacementDB PLACEMENT POLICY SET DEFAULT")
tk.MustQuery(`show create database TestResetPlacementDB`).Check(testutil.RowsWithSep("|",
"TestResetPlacementDB CREATE DATABASE `TestResetPlacementDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */",
))
// Test for `= 'DEFAULT'`
tk.MustExec("ALTER DATABASE TestResetPlacementDB PLACEMENT POLICY=`TestReset`;")
tk.MustQuery(`show create database TestResetPlacementDB`).Check(testutil.RowsWithSep("|",
"TestResetPlacementDB CREATE DATABASE `TestResetPlacementDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+
"/*T![placement] PLACEMENT POLICY=`TestReset` */",
))
tk.MustExec("ALTER DATABASE TestResetPlacementDB PLACEMENT POLICY = 'DEFAULT'")
tk.MustQuery(`show create database TestResetPlacementDB`).Check(testutil.RowsWithSep("|",
"TestResetPlacementDB CREATE DATABASE `TestResetPlacementDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */",
))
// Test for "= `DEFAULT`"
tk.MustExec("ALTER DATABASE TestResetPlacementDB PLACEMENT POLICY=`TestReset`;")
tk.MustQuery(`show create database TestResetPlacementDB`).Check(testutil.RowsWithSep("|",
"TestResetPlacementDB CREATE DATABASE `TestResetPlacementDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+
"/*T![placement] PLACEMENT POLICY=`TestReset` */",
))
tk.MustExec("ALTER DATABASE TestResetPlacementDB PLACEMENT POLICY = `DEFAULT`")
tk.MustQuery(`show create database TestResetPlacementDB`).Check(testutil.RowsWithSep("|",
"TestResetPlacementDB CREATE DATABASE `TestResetPlacementDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */",
))

tk.MustExec("drop placement policy `TestReset`;")
tk.MustExec("drop database TestResetPlacementDB;")
}

func (s *testDBSuite6) TestAlterPlacementPolicy(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
5 changes: 5 additions & 0 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,11 @@ error = '''
'%-.192s.%-.192s' is not %s
'''

["schema:1382"]
error = '''
The '%-.64s' syntax is reserved for purposes internal to the MySQL server
'''

["schema:1831"]
error = '''
Duplicate index '%-.64s' defined on the table '%-.64s.%-.64s'. This is deprecated and will be disallowed in a future release.
Expand Down
2 changes: 2 additions & 0 deletions infoschema/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var (
ErrPlacementPolicyExists = dbterror.ClassSchema.NewStd(mysql.ErrPlacementPolicyExists)
// ErrPlacementPolicyNotExists return for placement_policy policy not exists.
ErrPlacementPolicyNotExists = dbterror.ClassSchema.NewStd(mysql.ErrPlacementPolicyNotExists)
// ErrReservedSyntax for internal syntax.
ErrReservedSyntax = dbterror.ClassSchema.NewStd(mysql.ErrReservedSyntax)
// ErrTableExists returns for table already exists.
ErrTableExists = dbterror.ClassSchema.NewStd(mysql.ErrTableExists)
// ErrTableDropExists returns for dropping a non-existent table.
Expand Down
Loading

0 comments on commit 271df16

Please sign in to comment.