# 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;
source include/not_gtid_enabled.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();
--source include/sync_slave_sql_with_master.inc

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(); 
--source include/sync_slave_sql_with_master.inc

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; 
--source include/sync_slave_sql_with_master.inc 

# 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;

--echo # WL#5370
--echo # The behavior of statement 'CREATE TABLE SELECT IF NOT EXISTS' is changed.
--echo # After the worklog, it will insert nothing and the statement will not be
--echo # binlogged if the table already exists.
--echo # After the worklog, some bugs will disappear automotically.
--source include/rpl_reset.inc

--echo
--echo # Case 1: BUG#47132
connection master;
call mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT.*");

CREATE TABLE t1 (id int);
CREATE TABLE t2 (id int);
INSERT INTO t1 VALUES (1), (1);
INSERT INTO t2 VALUES (2), (2);

CREATE VIEW v1 AS SELECT id FROM t2;
--let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1)
CREATE TABLE IF NOT EXISTS v1(a int, b int) SELECT id, id FROM t1;
--source include/show_binlog_events.inc

SHOW CREATE TABLE v1;
SELECT * FROM t2;
SELECT * FROM v1;
DROP VIEW v1;

# the warning only happens on SBR, so we disable it.
--disable_warnings
CREATE TEMPORARY TABLE tt1 AS SELECT id FROM t2;
--enable_warnings

--let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1)
CREATE TEMPORARY TABLE IF NOT EXISTS tt1(a int, b int) SELECT id, id FROM t1;
--source include/show_binlog_events.inc
SELECT * FROM t2;
SELECT * FROM tt1;
DROP TEMPORARY TABLE tt1;

--echo
--echo # Case 1: BUG#47132
--echo # RBR breaks on CREATE TABLE IF EXISTS <existing VIEW> AS SELECT
CREATE VIEW v1 AS SELECT 1 as a;
--let binlog_start= query_get_value(SHOW MASTER STATUS, Position, 1)
CREATE TABLE IF NOT EXISTS v1 SELECT 2 as a;
--source include/show_binlog_events.inc
--source include/sync_slave_sql_with_master.inc

connection master;
DROP VIEW v1;

DROP TABLE t1, t2;


--echo #
--echo # Test case which has failed on assertion after refactoring which was
--echo # made as part of fix for bug #27480 "Extend CREATE TEMPORARY TABLES
--echo # privilege to allow temp table operations".
--echo #
CREATE TEMPORARY TABLE t1 (id int);
CREATE TABLE IF NOT EXISTS t2 LIKE t1;
--echo # The below statement should succeed with warning and
--echo # should not crash due to failing assertion.
CREATE TABLE IF NOT EXISTS t2 LIKE t1;
--echo # Clean-up.
DROP TABLE t1, t2;
--source include/sync_slave_sql_with_master.inc
connection master;


--source include/rpl_end.inc
