Commit e1b773cb authored by Nirbhay Choubey's avatar Nirbhay Choubey

Bug #54899 : --one-database option cannot handle DROP/CREATE DATABASE commands

After dropping and recreating the database specified along with --one-database
option at command line, mysql client keeps filtering the statements even after
the execution of a 'USE' command on the same database.

--one-database option enables the filtering of statements when the current
database is not the one specified at the command line. However, when the same
database is dropped and recreated the variable (current_db) that holds the
inital database name gets altered. This bug exploits the fact that current_db
initially gets set to null value (0) when a 'use db_name' follows the recreation
of same database db_name (speficied at the command line) and hence skip_updates
gets set to 1, which inturn triggers the further filtering of statements.

Fixed by making get_current_db() a no-op function when one_database is set,
and hence, under that condition current_db will not get altered.
Note, however the value of current_db can change when we execute 'connect'
command with a differnet database to reconnect to the server, in which case,
the behavior of --one-database will be formulated using this new database.


client/mysql.cc:
  Bug #54899 : --one-database option cannot handle DROP/CREATE DATABASE commands
  
  Added an if statement at the beginnning of get_current_db() , which makes it
  a no-op function if one-database option is specified, and hence current_db
  remains unchanged.
  
  Changed the help message for one-database option to a more appropriate message
  as specified in mysql documentation.
mysql-test/r/mysql.result:
  Added a test case for bug#54899 and some more test cases to
  check other one-database option related behaviors.
mysql-test/t/mysql.test:
  Added a test case for bug#54899 and some more test cases to
  check other one-database option related behaviors.
parent 01e0be66
......@@ -1449,8 +1449,8 @@ static struct my_option my_long_options[] =
&opt_sigint_ignore, &opt_sigint_ignore, 0, GET_BOOL,
NO_ARG, 0, 0, 0, 0, 0, 0},
{"one-database", 'o',
"Only update the default database. This is useful for skipping updates "
"to other database in the update log.",
"Ignore statements except those that occur while the default "
"database is the one named at the command line.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
#ifdef USE_POPEN
{"pager", OPT_PAGER,
......@@ -2736,6 +2736,10 @@ static void get_current_db()
{
MYSQL_RES *res;
/* If one_database is set, current_db is not supposed to change. */
if (one_database)
return;
my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
current_db= NULL;
/* In case of error below current_db will be NULL */
......
......@@ -235,4 +235,75 @@ Bug #47147: mysql client option --skip-column-names does not apply to vertical o
*************************** 1. row ***************************
1
#
# Bug #54899: --one-database option cannot handle DROP/CREATE DATABASE
# commands.
#
CREATE DATABASE connected_db;
USE connected_db;
SHOW TABLES;
Tables_in_connected_db
table_in_connected_db
DROP DATABASE connected_db;
#
# Testing --one-database option
#
CREATE DATABASE connected_db;
SHOW TABLES IN connected_db;
Tables_in_connected_db
t1
SHOW TABLES IN test;
Tables_in_test
t1
USE test;
DROP TABLE t1;
DROP DATABASE connected_db;
SHOW TABLES IN test;
Tables_in_test
SHOW TABLES IN test1;
Tables_in_test1
DROP DATABASE test1;
#
# Checking --one-database option followed by the execution of
# connect command.
#
CREATE DATABASE connected_db;
SHOW TABLES IN connected_db;
Tables_in_connected_db
t1
t2
SHOW TABLES IN test;
Tables_in_test
t1
t2
DROP TABLE test.t1;
DROP TABLE test.t2;
DROP DATABASE connected_db;
#
# Checking --one-database option with no database specified
# at command-line.
#
SHOW TABLES IN test;
Tables_in_test
#
# Checking --one-database option with non_existent_db
# specified with USE command
#
SHOW TABLES IN test;
Tables_in_test
table_in_test
DROP DATABASE test;
CREATE DATABASE test;
SHOW TABLES IN test;
Tables_in_test
table_in_test
DROP DATABASE test;
CREATE DATABASE test;
End of tests
......@@ -412,5 +412,149 @@ drop table t1;
--echo
--exec $MYSQL --skip-column-names --vertical test -e "select 1 as a"
--echo
--echo #
--echo # Bug #54899: --one-database option cannot handle DROP/CREATE DATABASE
--echo # commands.
--echo #
--write_file $MYSQLTEST_VARDIR/tmp/bug54899.sql
DROP DATABASE connected_db;
CREATE DATABASE connected_db;
USE connected_db;
CREATE TABLE `table_in_connected_db`(a INT);
EOF
CREATE DATABASE connected_db;
--exec $MYSQL --one-database connected_db < $MYSQLTEST_VARDIR/tmp/bug54899.sql
USE connected_db;
SHOW TABLES;
DROP DATABASE connected_db;
--remove_file $MYSQLTEST_VARDIR/tmp/bug54899.sql
--echo
--echo #
--echo # Testing --one-database option
--echo #
--write_file $MYSQLTEST_VARDIR/tmp/one_db.sql
CREATE TABLE t1 (i INT);
CREATE TABLE test.t1 (i INT);
USE test;
# Following statements should be filtered.
CREATE TABLE connected_db.t2 (i INT);
CREATE TABLE t2 (i INT);
EOF
CREATE DATABASE connected_db;
--exec $MYSQL --one-database connected_db < $MYSQLTEST_VARDIR/tmp/one_db.sql
SHOW TABLES IN connected_db;
SHOW TABLES IN test;
USE test;
DROP TABLE t1;
DROP DATABASE connected_db;
--remove_file $MYSQLTEST_VARDIR/tmp/one_db.sql
--echo
--write_file $MYSQLTEST_VARDIR/tmp/one_db.sql
CREATE DATABASE test1;
USE test1;
USE test1;
# Following statements should be filtered.
CREATE TABLE connected_db.t1 (i INT);
EOF
--exec $MYSQL --one-database test < $MYSQLTEST_VARDIR/tmp/one_db.sql
SHOW TABLES IN test;
SHOW TABLES IN test1;
DROP DATABASE test1;
--remove_file $MYSQLTEST_VARDIR/tmp/one_db.sql
--echo
--echo #
--echo # Checking --one-database option followed by the execution of
--echo # connect command.
--echo #
--write_file $MYSQLTEST_VARDIR/tmp/one_db.sql
CREATE TABLE t1 (i INT);
CREATE TABLE test.t1 (i INT);
CONNECT test;
CREATE TABLE connected_db.t2 (i INT);
CREATE TABLE t2 (i INT);
USE connected_db;
# Following statements should be filtered.
CREATE TABLE connected_db.t3 (i INT);
CREATE TABLE t3 (i INT);
EOF
CREATE DATABASE connected_db;
--exec $MYSQL --one-database connected_db < $MYSQLTEST_VARDIR/tmp/one_db.sql
SHOW TABLES IN connected_db;
SHOW TABLES IN test;
DROP TABLE test.t1;
DROP TABLE test.t2;
DROP DATABASE connected_db;
--remove_file $MYSQLTEST_VARDIR/tmp/one_db.sql
--echo
--echo #
--echo # Checking --one-database option with no database specified
--echo # at command-line.
--echo #
--write_file $MYSQLTEST_VARDIR/tmp/one_db.sql
# All following statements should be filtered.
CREATE TABLE t1 (i INT);
CREATE TABLE test.t1 (i INT);
USE test;
CREATE TABLE test.t2 (i INT);
CREATE TABLE t2 (i INT);
EOF
--exec $MYSQL --one-database < $MYSQLTEST_VARDIR/tmp/one_db.sql
SHOW TABLES IN test;
--remove_file $MYSQLTEST_VARDIR/tmp/one_db.sql
--echo
--echo #
--echo # Checking --one-database option with non_existent_db
--echo # specified with USE command
--echo #
# CASE 1 : When 'test' database exists and passed at commandline.
--write_file $MYSQLTEST_VARDIR/tmp/one_db_1.sql
CREATE TABLE `table_in_test`(i INT);
USE non_existent_db;
# Following statement should be filtered out.
CREATE TABLE `table_in_non_existent_db`(i INT);
EOF
# CASE 2 : When 'test' database exists but dropped and recreated in load file.
--write_file $MYSQLTEST_VARDIR/tmp/one_db_2.sql
DROP DATABASE test;
CREATE DATABASE test;
USE non_existent_db;
# Following statements should be filtered out.
CREATE TABLE `table_in_non_existent_db`(i INT);
USE test;
# Following statements should not be filtered out.
CREATE TABLE `table_in_test`(i INT);
EOF
--exec $MYSQL --one-database test < $MYSQLTEST_VARDIR/tmp/one_db_1.sql
SHOW TABLES IN test;
DROP DATABASE test;
--echo
CREATE DATABASE test;
--exec $MYSQL --one-database test < $MYSQLTEST_VARDIR/tmp/one_db_2.sql
SHOW TABLES IN test;
DROP DATABASE test;
CREATE DATABASE test;
--remove_file $MYSQLTEST_VARDIR/tmp/one_db_1.sql
--remove_file $MYSQLTEST_VARDIR/tmp/one_db_2.sql
--echo
--echo End of tests
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment