# BUG#45574:
# SP: CREATE DATABASE|TABLE IF NOT EXISTS not binlogged if routine exists.
#
#   There is an inconsistency with DROP DATABASE|TABLE|EVENT IF EXISTS and
#   CREATE DATABASE|TABLE|EVENT IF NOT EXISTS. DROP IF EXISTS statements are
#   binlogged even if either the DB, TABLE or EVENT does not exist. In
#   contrast, Only the CREATE EVENT IF NOT EXISTS is binlogged when the EVENT
#   exists.  
#
#   This problem caused some of the tests to fail randomly on PB or PB2.
#
# Description: 
#   Fixed this bug by adding calls to write_bin_log in: 
#   mysql_create_db 
#   mysql_create_table_no_lock 
#   mysql_create_like_table 
#   create_table_from_items 
#
#   Test is implemented as follows: 
#   i) test each "CREATE IF NOT EXISTS" (DDL), found in MySQL 5.1 manual
#   exclude CREATE TEMPORARY TABLE, on existent objects; 
#
#  Note: 
#  rpl_create_tmp_table_if_not_exists.test tests CREATE TEMPORARY TABLE cases.
#
#  References:
#  http://dev.mysql.com/doc/refman/5.1/en/sql-syntax-data-definition.html
#

source include/master-slave.inc;
disable_warnings;
DROP DATABASE IF EXISTS mysqltest;

CREATE DATABASE IF NOT EXISTS mysqltest;
USE mysqltest;
CREATE TABLE IF NOT EXISTS t(c1 int);
CREATE TABLE IF NOT EXISTS t1 LIKE t;
CREATE TABLE IF NOT EXISTS t2 SELECT * FROM t;
CREATE EVENT IF NOT EXISTS e 
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR 
DO SELECT now();
sync_slave_with_master;

connection slave;
#DROP database from slave.
#The database and all tables can be recreated in slave 
#if binlog of the second CREATE command is recorded and sent from master to slave.
DROP DATABASE mysqltest;

connection master;
CREATE DATABASE IF NOT EXISTS mysqltest;
USE mysqltest;
CREATE TABLE IF NOT EXISTS t(c1 int);
CREATE TABLE IF NOT EXISTS t1 LIKE t;
CREATE TABLE IF NOT EXISTS t2 SELECT * FROM t;
CREATE EVENT IF NOT EXISTS e 
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR 
DO SELECT now(); 
sync_slave_with_master;

connection slave;
SHOW TABLES in mysqltest;
#Execution time changes in each run. So we disregard it by calling replace_column.
replace_column 6 #;
SHOW EVENTS in mysqltest;


connection master;
DROP DATABASE IF EXISTS mysqltest;

#
# BUG#47418 RBR fails, failure with mixup of base/temporary/view TABLE DDL
# 
# Before the patch for this bug, 'CREATE TABLE IF NOT EXIST ... SELECT'
# statement was binlogged as a TEMPORARY table if the object existed as
# a temporary table.  This was caused by that the temporary table was opened
# and the results of the 'SELECT' was inserted into the temporary table if
# a temporary table existed with the same name.
# 
# After the patch for this bug, the base table is created and the results of
# the 'SELECT' are inserted into it, even though a temporary table exists with
# the same name, and the statement is still binlogged as a base table.
#

echo -------------BUG#47418-------------;
connection master;
USE test;
DROP TABLE IF EXISTS t3;
--enable_warnings
CREATE TABLE t3(c1 INTEGER);
INSERT INTO t3 VALUES(33);

CREATE TEMPORARY TABLE t1(c1 INTEGER);
CREATE TEMPORARY TABLE t2(c1 INTEGER);
INSERT INTO t1 VALUES(1);
INSERT INTO t2 VALUES(1);

CREATE TABLE IF NOT EXISTS t1(c1 INTEGER) SELECT c1 FROM t3;
CREATE TABLE t2(c1 INTEGER) SELECT c1 FROM t3;

# In these two statements, t1 and t2 are the temporary table. there is only
# value '1' in them.  The records of t2 are not inserted into them.  
SELECT * FROM t1; 
SELECT * FROM t2; 
sync_slave_with_master; 

# In these two statements, t1 and t2 are the base table. The recoreds of t2
# are inserted into it when CREATE TABLE ...  SELECT was executed.  
SELECT * FROM t1;
SELECT * FROM t2;

connection master; 
DROP TEMPORARY TABLE t1; 
DROP TEMPORARY TABLE t2; 
#In these two statements, t1 and t2 are the base table.
SELECT * FROM t1;
SELECT * FROM t2;

DROP TABLE t1;
DROP TABLE t2;
DROP TABLE t3;
sync_slave_with_master;

--echo
--echo # Bug#55616 Killing thread or query during CREATE IF NOT EXISTS makes
--echo # slave SQL thread abort
--echo

--connection master1
let $con_id = `SELECT CONNECTION_ID()`;

CREATE TABLE t1 ( i INT );
send CREATE TABLE IF NOT EXISTS t1
  AS SELECT SLEEP(3);

connection master;
let $wait_timeout = 3;
let $show_statement = SHOW PROCESSLIST;
let $field = State;
let $condition = = 'User sleep';
source include/wait_show_condition.inc;

--replace_result $con_id master1
eval KILL QUERY $con_id;
sync_slave_with_master;

connection master;
DROP TABLE t1;

source include/master-slave-end.inc;
