From e8bbaf49db924942a2b65da265a9b80ba066ddba Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Thu, 20 Aug 2020 11:09:10 +0800 Subject: [PATCH] ddl: fix db charset modification panic in an uppercase schema (#19286) (#19302) Signed-off-by: ti-srebot --- ddl/db_integration_test.go | 7 +++++++ infoschema/builder.go | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index e97009cdc65a5..03eff83dc2c42 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -1820,6 +1820,13 @@ func (s *testIntegrationSuite5) TestChangingDBCharset(c *C) { tk.MustExec("ALTER SCHEMA CHARACTER SET = 'utf8mb4' COLLATE = 'utf8mb4_general_ci'") verifyDBCharsetAndCollate("alterdb2", "utf8mb4", "utf8mb4_general_ci") + + // Test changing charset of schema with uppercase name. See https://github.com/pingcap/tidb/issues/19273. + tk.MustExec("drop database if exists TEST_UPPERCASE_DB_CHARSET;") + tk.MustExec("create database TEST_UPPERCASE_DB_CHARSET;") + tk.MustExec("use TEST_UPPERCASE_DB_CHARSET;") + tk.MustExec("alter database TEST_UPPERCASE_DB_CHARSET default character set utf8;") + tk.MustExec("alter database TEST_UPPERCASE_DB_CHARSET default character set utf8mb4;") } func (s *testIntegrationSuite4) TestDropAutoIncrementIndex(c *C) { diff --git a/infoschema/builder.go b/infoschema/builder.go index 1d2e36d3e8c91..964ccf0737589 100644 --- a/infoschema/builder.go +++ b/infoschema/builder.go @@ -187,7 +187,7 @@ func (b *Builder) applyModifySchemaCharsetAndCollate(m *meta.Meta, diff *model.S fmt.Sprintf("(Schema ID %d)", diff.SchemaID), ) } - newDbInfo := b.copySchemaTables(di.Name.O) + newDbInfo := b.copySchemaTables(di.Name.L) newDbInfo.Charset = di.Charset newDbInfo.Collate = di.Collate return nil @@ -362,6 +362,7 @@ func (b *Builder) copySchemasMap(oldIS *infoSchema) { // copySchemaTables creates a new schemaTables instance when a table in the database has changed. // It also does modifications on the new one because old schemaTables must be read-only. +// Note: please make sure the dbName is in lowercase. func (b *Builder) copySchemaTables(dbName string) *model.DBInfo { oldSchemaTables := b.is.schemaMap[dbName] newSchemaTables := &schemaTables{