-
Notifications
You must be signed in to change notification settings - Fork 713
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added db_metadata options to create database command
Summary: Added new option db_metadata to the create database `CREATE DATABASE [IF NOT EXISTS] db_name DB_METADATA [=] metadata` The new option is stored in the db.opt file along with other database options like charset and read_only. The options accepts any string value. If not specified, it writes empty string as the default value in the db.opt file. To retrieve the option value for a particular database, the show command can be used as follows: `SHOW CREATE DATABASE db_name` Example Usage: create database test2 charset utf8 read_only = true db_metadata '{\'shard_name\':\'myshard_for_test2\'}'; show create database test2; Output: +----------+--------------------------------------------------------------------------------------------------------------------------+ | Database | Create Database | +----------+--------------------------------------------------------------------------------------------------------------------------+ | test2 | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET utf8 READ_ONLY DB_METADATA {'shard_name':'myshard_for_test2'} */ | +----------+--------------------------------------------------------------------------------------------------------------------------+ Reviewed By: tianx Differential Revision: D5358178 fbshipit-source-id: 4308ca2
- Loading branch information
1 parent
745ad33
commit 2f08e3a
Showing
10 changed files
with
427 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
drop database if exists test2; | ||
drop database if exists test3; | ||
drop database if exists test4; | ||
drop database if exists test5; | ||
drop database if exists test6; | ||
drop database if exists test7; | ||
drop database if exists test8; | ||
drop database if exists test9; | ||
create database test2; | ||
show create database test2; | ||
Database Create Database | ||
test2 CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET latin1 */ | ||
default-character-set=latin1 | ||
default-collation=latin1_swedish_ci | ||
db-read-only=0 | ||
db-metadata= | ||
create database test3 character set utf8; | ||
show create database test3; | ||
Database Create Database | ||
test3 CREATE DATABASE `test3` /*!40100 DEFAULT CHARACTER SET utf8 */ | ||
default-character-set=utf8 | ||
default-collation=utf8_general_ci | ||
db-read-only=0 | ||
db-metadata= | ||
create database test4 read_only = true; | ||
show create database test4; | ||
Database Create Database | ||
test4 CREATE DATABASE `test4` /*!40100 DEFAULT CHARACTER SET latin1 READ_ONLY */ | ||
default-character-set=latin1 | ||
default-collation=latin1_swedish_ci | ||
db-read-only=1 | ||
db-metadata= | ||
create database test5 db_metadata = "{\"shard\":\"test5_shard\"}"; | ||
show create database test5; | ||
Database Create Database | ||
test5 CREATE DATABASE `test5` /*!40100 DEFAULT CHARACTER SET latin1 DB_METADATA {"shard":"test5_shard"} */ | ||
default-character-set=latin1 | ||
default-collation=latin1_swedish_ci | ||
db-read-only=0 | ||
db-metadata={"shard":"test5_shard"} | ||
create database test6 character set utf8 db_metadata = "{\"shard\":\"test6_shard\"}"; | ||
show create database test6; | ||
Database Create Database | ||
test6 CREATE DATABASE `test6` /*!40100 DEFAULT CHARACTER SET utf8 DB_METADATA {"shard":"test6_shard"} */ | ||
default-character-set=utf8 | ||
default-collation=utf8_general_ci | ||
db-read-only=0 | ||
db-metadata={"shard":"test6_shard"} | ||
create database test7 read_only = true db_metadata = "{\"shard\":\"test7_shard\"}"; | ||
show create database test7; | ||
Database Create Database | ||
test7 CREATE DATABASE `test7` /*!40100 DEFAULT CHARACTER SET latin1 READ_ONLY DB_METADATA {"shard":"test7_shard"} */ | ||
default-character-set=latin1 | ||
default-collation=latin1_swedish_ci | ||
db-read-only=1 | ||
db-metadata={"shard":"test7_shard"} | ||
create database test8 character set utf8 read_only = true; | ||
show create database test8; | ||
Database Create Database | ||
test8 CREATE DATABASE `test8` /*!40100 DEFAULT CHARACTER SET utf8 READ_ONLY */ | ||
default-character-set=utf8 | ||
default-collation=utf8_general_ci | ||
db-read-only=1 | ||
db-metadata= | ||
create database test9 character set utf8 read_only = true db_metadata = "{\"shard\":\"test9_shard\"}"; | ||
show create database test9; | ||
Database Create Database | ||
test9 CREATE DATABASE `test9` /*!40100 DEFAULT CHARACTER SET utf8 READ_ONLY DB_METADATA {"shard":"test9_shard"} */ | ||
default-character-set=utf8 | ||
default-collation=utf8_general_ci | ||
db-read-only=1 | ||
db-metadata={"shard":"test9_shard"} | ||
alter database test3 character set ascii; | ||
show create database test3; | ||
Database Create Database | ||
test3 CREATE DATABASE `test3` /*!40100 DEFAULT CHARACTER SET ascii */ | ||
default-character-set=ascii | ||
default-collation=ascii_general_ci | ||
db-read-only=0 | ||
db-metadata= | ||
alter database test4 read_only = true; | ||
show create database test4; | ||
Database Create Database | ||
test4 CREATE DATABASE `test4` /*!40100 DEFAULT CHARACTER SET latin1 READ_ONLY */ | ||
default-character-set=latin1 | ||
default-collation=latin1_swedish_ci | ||
db-read-only=1 | ||
db-metadata= | ||
alter database test5 db_metadata = "{\"shard\":\"test5_shard_altered\"}"; | ||
show create database test5; | ||
Database Create Database | ||
test5 CREATE DATABASE `test5` /*!40100 DEFAULT CHARACTER SET latin1 DB_METADATA {"shard":"test5_shard_altered"} */ | ||
default-character-set=latin1 | ||
default-collation=latin1_swedish_ci | ||
db-read-only=0 | ||
db-metadata={"shard":"test5_shard_altered"} | ||
alter database test5 character set ascii; | ||
show create database test5; | ||
Database Create Database | ||
test5 CREATE DATABASE `test5` /*!40100 DEFAULT CHARACTER SET ascii DB_METADATA {"shard":"test5_shard_altered"} */ | ||
default-character-set=ascii | ||
default-collation=ascii_general_ci | ||
db-read-only=0 | ||
db-metadata={"shard":"test5_shard_altered"} | ||
alter database test5 read_only = true; | ||
show create database test5; | ||
Database Create Database | ||
test5 CREATE DATABASE `test5` /*!40100 DEFAULT CHARACTER SET ascii READ_ONLY DB_METADATA {"shard":"test5_shard_altered"} */ | ||
default-character-set=ascii | ||
default-collation=ascii_general_ci | ||
db-read-only=1 | ||
db-metadata={"shard":"test5_shard_altered"} | ||
alter database test5 character set utf8 read_only = false; | ||
show create database test5; | ||
Database Create Database | ||
test5 CREATE DATABASE `test5` /*!40100 DEFAULT CHARACTER SET utf8 DB_METADATA {"shard":"test5_shard_altered"} */ | ||
default-character-set=utf8 | ||
default-collation=utf8_general_ci | ||
db-read-only=0 | ||
db-metadata={"shard":"test5_shard_altered"} | ||
alter database test5 db_metadata ""; | ||
show create database test5; | ||
Database Create Database | ||
test5 CREATE DATABASE `test5` /*!40100 DEFAULT CHARACTER SET utf8 */ | ||
default-character-set=utf8 | ||
default-collation=utf8_general_ci | ||
db-read-only=0 | ||
db-metadata= | ||
alter database test5 db_metadata "{\"shard\":\"Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Fin\"}"; | ||
show create database test5; | ||
Database Create Database | ||
test5 CREATE DATABASE `test5` /*!40100 DEFAULT CHARACTER SET utf8 DB_METADATA {"shard":"Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Fin"} */ | ||
default-character-set=utf8 | ||
default-collation=utf8_general_ci | ||
db-read-only=0 | ||
db-metadata={"shard":"Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Fin"} | ||
alter database test5 db_metadata "{\"shard\":\"Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Fin\"}";; | ||
ERROR HY000: Metadata for the database is too long. Max length is 1024 bytes | ||
alter database test6 character set ascii db_metadata = "{\"shard\":\"test6_shard_altered\"}"; | ||
show create database test6; | ||
Database Create Database | ||
test6 CREATE DATABASE `test6` /*!40100 DEFAULT CHARACTER SET ascii DB_METADATA {"shard":"test6_shard_altered"} */ | ||
default-character-set=ascii | ||
default-collation=ascii_general_ci | ||
db-read-only=0 | ||
db-metadata={"shard":"test6_shard_altered"} | ||
alter database test7 read_only = true db_metadata = "{\"shard\":\"test7_shard_altered\"}"; | ||
show create database test7; | ||
Database Create Database | ||
test7 CREATE DATABASE `test7` /*!40100 DEFAULT CHARACTER SET latin1 READ_ONLY DB_METADATA {"shard":"test7_shard_altered"} */ | ||
default-character-set=latin1 | ||
default-collation=latin1_swedish_ci | ||
db-read-only=1 | ||
db-metadata={"shard":"test7_shard_altered"} | ||
alter database test8 character set ascii read_only = true; | ||
show create database test8; | ||
Database Create Database | ||
test8 CREATE DATABASE `test8` /*!40100 DEFAULT CHARACTER SET ascii READ_ONLY */ | ||
default-character-set=ascii | ||
default-collation=ascii_general_ci | ||
db-read-only=1 | ||
db-metadata= | ||
alter database test9 character set ascii read_only = true db_metadata = "{\"shard\":\"test9_shard_altered\"}"; | ||
show create database test9; | ||
Database Create Database | ||
test9 CREATE DATABASE `test9` /*!40100 DEFAULT CHARACTER SET ascii READ_ONLY DB_METADATA {"shard":"test9_shard_altered"} */ | ||
default-character-set=ascii | ||
default-collation=ascii_general_ci | ||
db-read-only=1 | ||
db-metadata={"shard":"test9_shard_altered"} | ||
show create database information_schema; | ||
Database Create Database | ||
information_schema CREATE DATABASE `information_schema` /*!40100 DEFAULT CHARACTER SET utf8 */ | ||
show create database mysql; | ||
Database Create Database | ||
mysql CREATE DATABASE `mysql` /*!40100 DEFAULT CHARACTER SET latin1 */ | ||
alter database information_schema db_metadata "{\"shard\":\"is_shard\"}";; | ||
ERROR 42000: Access denied for user 'root'@'localhost' to database 'information_schema' | ||
drop database if exists test2; | ||
drop database if exists test3; | ||
drop database if exists test4; | ||
drop database if exists test5; | ||
drop database if exists test6; | ||
drop database if exists test7; | ||
drop database if exists test8; | ||
drop database if exists test9; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# Test per-database database-metadata attribute | ||
--source include/have_innodb.inc | ||
connection default; | ||
|
||
--disable_warnings | ||
drop database if exists test2; | ||
drop database if exists test3; | ||
drop database if exists test4; | ||
drop database if exists test5; | ||
drop database if exists test6; | ||
drop database if exists test7; | ||
drop database if exists test8; | ||
drop database if exists test9; | ||
--enable_warnings | ||
|
||
--disable_query_log | ||
let $MYSQLD_DATADIR= `select @@datadir`; | ||
--enable_query_log | ||
|
||
--error 1 | ||
file_exists $MYSQLD_DATADIR/mysql/db.opt; | ||
--error 1 | ||
file_exists $MYSQLD_DATADIR/test/db.opt; | ||
|
||
# create database without any options | ||
create database test2; | ||
show create database test2; | ||
--exec cat $MYSQLD_DATADIR/test2/db.opt; | ||
|
||
# create database with character set | ||
create database test3 character set utf8; | ||
show create database test3; | ||
--exec cat $MYSQLD_DATADIR/test3/db.opt; | ||
|
||
# create database with read only | ||
create database test4 read_only = true; | ||
show create database test4; | ||
--exec cat $MYSQLD_DATADIR/test4/db.opt; | ||
|
||
# create database with db metadata | ||
create database test5 db_metadata = "{\"shard\":\"test5_shard\"}"; | ||
show create database test5; | ||
--exec cat $MYSQLD_DATADIR/test5/db.opt; | ||
|
||
# create database with character set and db metadata | ||
create database test6 character set utf8 db_metadata = "{\"shard\":\"test6_shard\"}"; | ||
show create database test6; | ||
--exec cat $MYSQLD_DATADIR/test6/db.opt; | ||
|
||
# create database with read only and db metadata | ||
create database test7 read_only = true db_metadata = "{\"shard\":\"test7_shard\"}"; | ||
show create database test7; | ||
--exec cat $MYSQLD_DATADIR/test7/db.opt; | ||
|
||
# create database with character set and read only | ||
create database test8 character set utf8 read_only = true; | ||
show create database test8; | ||
--exec cat $MYSQLD_DATADIR/test8/db.opt; | ||
|
||
# create database with character set, read only and db metadata | ||
create database test9 character set utf8 read_only = true db_metadata = "{\"shard\":\"test9_shard\"}"; | ||
show create database test9; | ||
--exec cat $MYSQLD_DATADIR/test9/db.opt; | ||
|
||
# alter database tests | ||
|
||
# alter database character set | ||
alter database test3 character set ascii; | ||
show create database test3; | ||
--exec cat $MYSQLD_DATADIR/test3/db.opt; | ||
|
||
# alter database read only | ||
alter database test4 read_only = true; | ||
show create database test4; | ||
--exec cat $MYSQLD_DATADIR/test4/db.opt; | ||
|
||
# alter database db metadata | ||
alter database test5 db_metadata = "{\"shard\":\"test5_shard_altered\"}"; | ||
show create database test5; | ||
--exec cat $MYSQLD_DATADIR/test5/db.opt; | ||
|
||
# alter database character set but keep db metadata intact | ||
alter database test5 character set ascii; | ||
show create database test5; | ||
--exec cat $MYSQLD_DATADIR/test5/db.opt; | ||
|
||
# alter database read only but keep db metadata intact | ||
alter database test5 read_only = true; | ||
show create database test5; | ||
--exec cat $MYSQLD_DATADIR/test5/db.opt; | ||
|
||
# alter database character set and read only but keep db metadata intact | ||
alter database test5 character set utf8 read_only = false; | ||
show create database test5; | ||
--exec cat $MYSQLD_DATADIR/test5/db.opt; | ||
|
||
# alter database reset db metadata | ||
alter database test5 db_metadata ""; | ||
show create database test5; | ||
--exec cat $MYSQLD_DATADIR/test5/db.opt; | ||
|
||
# alter database set db metadata to max length string | ||
alter database test5 db_metadata "{\"shard\":\"Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Fin\"}"; | ||
show create database test5; | ||
--exec cat $MYSQLD_DATADIR/test5/db.opt; | ||
|
||
# alter database exceed db metadata max length | ||
--error ER_DB_METADATA_TOO_LONG | ||
--eval alter database test5 db_metadata \"{\\\"shard\\\":\\\"Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Really long shard name. Fin\\\"}\"; | ||
|
||
# alter database character set and db metadata | ||
alter database test6 character set ascii db_metadata = "{\"shard\":\"test6_shard_altered\"}"; | ||
show create database test6; | ||
--exec cat $MYSQLD_DATADIR/test6/db.opt; | ||
|
||
# alter database read only and db metadata | ||
alter database test7 read_only = true db_metadata = "{\"shard\":\"test7_shard_altered\"}"; | ||
show create database test7; | ||
--exec cat $MYSQLD_DATADIR/test7/db.opt; | ||
|
||
# alter database character set and read only | ||
alter database test8 character set ascii read_only = true; | ||
show create database test8; | ||
--exec cat $MYSQLD_DATADIR/test8/db.opt; | ||
|
||
# alter database character set, read only and db metadata | ||
alter database test9 character set ascii read_only = true db_metadata = "{\"shard\":\"test9_shard_altered\"}"; | ||
show create database test9; | ||
--exec cat $MYSQLD_DATADIR/test9/db.opt; | ||
|
||
# ensure information_schema and mysql tables are intact | ||
show create database information_schema; | ||
show create database mysql; | ||
--error ER_DBACCESS_DENIED_ERROR | ||
--eval alter database information_schema db_metadata \"{\\"shard\\":\\"is_shard\\"}\"; | ||
|
||
# cleanup | ||
--disable_warnings | ||
drop database if exists test2; | ||
drop database if exists test3; | ||
drop database if exists test4; | ||
drop database if exists test5; | ||
drop database if exists test6; | ||
drop database if exists test7; | ||
drop database if exists test8; | ||
drop database if exists test9; | ||
--enable_warnings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.