# ==== Purpose ====
#
# Test that sql_mode can correct restore before generating the binlog event
#    when creating CREATEable objects.
#
# ==== Method ====
#
# Scan binlog file to check if the sql_mode is still set to 0 before generating binlog event
#

-- source include/master-slave.inc
-- source include/have_log_bin.inc

# BUG#39526 sql_mode not retained in binary log for CREATE PROCEDURE

SET @old_sql_mode=  @@global.sql_mode;
SET @old_binlog_format=@@session.binlog_format;
let $MYSQLD_DATADIR= `select @@datadir`;
SET SESSION sql_mode=8;

--echo Initialization 

RESET MASTER;
CREATE TABLE t1 (id INT);

CREATE PROCEDURE testProc() SELECT * FROM t1;
CREATE VIEW testView as SELECT * from t1;

DELIMITER |;
CREATE FUNCTION testFunc()
  RETURNS INT
  BEGIN
    return 1;
  END;|
DELIMITER ;|

DELIMITER |;
CREATE TRIGGER testTrig BEFORE INSERT ON t1
  FOR EACH ROW BEGIN
    UPDATE t1 SET id = id +1;
  END;|
DELIMITER ;|

DELIMITER |;
CREATE EVENT testEvent ON SCHEDULE
  EVERY 1 DAY
  DO
    BEGIN
      UPDATE t1 SET id = id +1;
    END;|
DELIMITER ;|

--echo Chceck Result

let $MYSQLD_DATADIR= `select @@datadir`;
--exec $MYSQL_BINLOG $MYSQLD_DATADIR/master-bin.000001 > $MYSQLTEST_VARDIR/tmp/mysqlbinlog_bug39526.binlog
--replace_result $MYSQLTEST_VARDIR MYSQLTEST_VARDIR
eval select
(@a:=load_file("$MYSQLTEST_VARDIR/tmp/mysqlbinlog_bug39526.binlog"))
is not null;
let $s_mode_unsigned= `select @a like "%@@session.sql_mode=0%" /* must return 0 */`;
echo *** String sql_mode=0 is found: $s_mode_unsigned ***;

--remove_file $MYSQLTEST_VARDIR/tmp/mysqlbinlog_bug39526.binlog

--echo Clean Up

DROP PROCEDURE testProc;
DROP FUNCTION testFunc;
DROP TRIGGER testTrig;
DROP EVENT testEvent;
DROP VIEW testView;
DROP TABLE t1;

SET @@global.sql_mode= @old_sql_mode;
SET @@session.binlog_format=@old_binlog_format;
