#  ==== Purpose ====
#
# The purpose of this test is to verify that a DELETE statement is binary
# logged with correct GTID when a replicated memory table is fisrt updated
# after a server crash or a server restart.
#
#  ==== Requirements ====
#
# R1. When there is a replicated memory table, and the slave crashes, the next
# transaction that updates the memory table shall generate a DELETE statement
# in the binary log. This test verifies that this works in the below cases:

# R1.1. When the update is applied on a replication channel.
# R1.2. When the update is applied in a client session having
#       gtid_next=UUID:NUMBER.
#
# R2. The DELETE statement shall have a new GTID generated by the slave.
#
# R3. There shall not be a crash.
#
# ==== Implementation ====
#
# 1. Try to update the memory table on master after restarting the master:
#
# 1.1. A DML on the memory table
# 1.2. A DDL on the memory table.
# 1.3. A statement that reads from the memory table.
# 1.4. A transaction containing update on the memory table along with other
#      table updates.
# 1.5. A statement containing update on the memory table along with other
#      table updates.
# 1.6. A DML on the memory table applied in a client session having
#      gtid_next=UUID:NUMBER.
#
# In each of the above cases:
# - Verify that DELETE statement is binary logged on the master.
#
# 2. Try to update the memory table on master after restarting the slave:
#
# 2.1. A DML on the memory table
# 2.2. A DDL on the memory table.
# 2.3. A statement that reads from the memory table
# 2.4. A transaction containing update on the memory table along with other
#      table updates.
# 2.5. A statement containing update on the memory table along with other
#      table updates.
# 2.6. A DML on the memory table applied in a client session having
#      gtid_next=UUID:NUMBER.
#
# In each of the above cases:
# - Verify that DELETE statement is binary logged on the slave.
#
# ==== References ====
#
# Bug#30527929 - RESTART CREATES ANONYMOUS TRANSACTION FOR MEMORY TABLE
#

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

#Scenario 1
--source include/rpl_connection_master.inc
CREATE TABLE mem_t1 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t2 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t3 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t4 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t5 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t6 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t7 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t8 (c1 INT) ENGINE= MEMORY;
INSERT INTO mem_t1 VALUES (1), (2), (3), (4);
INSERT INTO mem_t2 VALUES (1), (2), (3), (4);
INSERT INTO mem_t3 VALUES (1), (2), (3), (4);
INSERT INTO mem_t4 VALUES (1), (2), (3), (4);
INSERT INTO mem_t5 VALUES (1), (2), (3), (4);
INSERT INTO mem_t6 VALUES (1), (2), (3), (4);
INSERT INTO mem_t7 VALUES (1), (2), (3), (4);
INSERT INTO mem_t8 VALUES (1), (2), (3), (4);
--source include/sync_slave_sql_with_master.inc

# Restart master
--source include/stop_slave.inc
--let $rpl_server_number=1
--source include/rpl_restart_server.inc
--source include/start_slave.inc
--source include/rpl_connection_master.inc
--source include/save_binlog_position.inc

# Scenario 1.1
INSERT INTO mem_t1 VALUES (1), (2), (3), (4);
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_master.inc
--let $limit= 0,9
--let $event_sequence= #Gtid # !Begin # !Delete # !Commit #Gtid # !Begin # !Insert # !Commit
--source include/assert_binlog_events.inc
--source include/save_binlog_position.inc

# Scenario 1.2
ALTER TABLE mem_t2 ADD COLUMN (c2 INT);
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_master.inc
--let $limit= 0,6
--let $event_sequence= #Gtid # !Begin # !Delete # !Commit #Gtid # !Q(ALTER.*)
--source include/assert_binlog_events.inc
--source include/save_binlog_position.inc

# Scenario 1.3
CREATE TABLE mem_t9 LIKE mem_t3;
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_master.inc
--let $limit= 0,6
--let $event_sequence= #Gtid # !Begin # !Delete # !Commit #Gtid # !Q(CREATE.*)
--source include/assert_binlog_events.inc
--source include/save_binlog_position.inc

# Scenario 1.4:
BEGIN;
INSERT INTO mem_t4 VALUES (1), (2), (3), (4);
INSERT INTO mem_t5 VALUES (1), (2), (3), (4);
COMMIT;
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_master.inc
--let $limit= 0,18
--let $event_sequence= #Gtid # !Begin # !Delete # !Commit #Gtid # !Begin # !Insert # !Commit #Gtid # !Begin # !Delete # !Commit #Gtid # !Begin # !Insert # !Commit
--source include/assert_binlog_events.inc
--source include/save_binlog_position.inc

# Scenario 1.5:
UPDATE mem_t6, mem_t7 SET mem_t6.c1 = mem_t6.c1 + 1, mem_t7.c1 = mem_t7.c1 + 1;
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_master.inc
--let $limit= 0,8
--let $event_sequence= #Gtid # !Begin # !Delete # !Commit #Gtid # !Begin # !Delete # !Commit
--source include/assert_binlog_events.inc
--source include/save_binlog_position.inc

#Scenario 1.6:
--let $explicit_gtid=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1
--eval SET @@session.gtid_next = '$explicit_gtid'
INSERT INTO mem_t8 VALUES (1), (2), (3), (4);
SET @@session.gtid_next = 'AUTOMATIC';
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_master.inc
--let $limit= 0,9
--let $event_sequence= #Gtid # !Begin # !Delete # !Commit #Gtid/(.*'$explicit_gtid')# !Begin # !Insert # !Commit
--source include/assert_binlog_events.inc

# Cleanup
DROP TABLE mem_t1, mem_t2, mem_t3, mem_t4, mem_t5, mem_t6, mem_t7, mem_t8, mem_t9;
--source include/rpl_reset.inc

# Scenario 2:
--source include/rpl_connection_master.inc
CREATE TABLE mem_t1 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t2 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t3 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t4 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t5 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t6 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t7 (c1 INT) ENGINE= MEMORY;
CREATE TABLE mem_t8 (c1 INT) ENGINE= MEMORY;
INSERT INTO mem_t1 VALUES (1), (2), (3), (4);
INSERT INTO mem_t2 VALUES (1), (2), (3), (4);
INSERT INTO mem_t3 VALUES (1), (2), (3), (4);
INSERT INTO mem_t4 VALUES (1), (2), (3), (4);
INSERT INTO mem_t5 VALUES (1), (2), (3), (4);
INSERT INTO mem_t6 VALUES (1), (2), (3), (4);
INSERT INTO mem_t7 VALUES (1), (2), (3), (4);
INSERT INTO mem_t8 VALUES (1), (2), (3), (4);
--source include/sync_slave_sql_with_master.inc

# Restart slave
--let $rpl_server_number=2
--source include/rpl_restart_server.inc
--source include/start_slave.inc
--source include/save_binlog_position.inc

# Save the UUID of master and slave
--source include/rpl_connection_master.inc
--let $master_uuid = `SELECT @@global.server_uuid`
--source include/rpl_connection_slave.inc
--let $slave_uuid = `SELECT @@global.server_uuid`
--let $dont_print_pattern = 1

# Scenario 2.1
--source include/rpl_connection_master.inc
INSERT INTO mem_t1 VALUES (1), (2), (3), (4);
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_slave.inc
--let $limit= 0,9
--let $event_sequence= #Gtid/(.*'$slave_uuid:1') # !Begin # !Delete # !Commit #Gtid/(.*'$master_uuid:17') # !Begin # !Insert # !Commit
--source include/assert_binlog_events.inc
--source include/save_binlog_position.inc

# Scenario 2.2
--source include/rpl_connection_master.inc
ALTER TABLE mem_t2 ADD COLUMN (c2 INT);
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_slave.inc
--let $limit= 0,6
--let $event_sequence= #Gtid/(.*'$slave_uuid:2') # !Begin # !Delete # !Commit #Gtid/(.*'$master_uuid:18') # !Q(ALTER.*)
--source include/assert_binlog_events.inc
--source include/save_binlog_position.inc

# Scenario 2.3
--source include/rpl_connection_master.inc
CREATE TABLE mem_t9 LIKE mem_t3;
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_slave.inc
--let $limit= 0,6
--let $event_sequence= #Gtid/(.*'$slave_uuid:3') # !Begin # !Delete # !Commit #Gtid/(.*'$master_uuid:19') # !Q(CREATE.*)
--source include/assert_binlog_events.inc
--source include/save_binlog_position.inc

# Scenario 2.4:
--source include/rpl_connection_master.inc
BEGIN;
INSERT INTO mem_t4 VALUES (1), (2), (3), (4);
INSERT INTO mem_t5 VALUES (1), (2), (3), (4);
COMMIT;
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_slave.inc
--let $limit= 0,18
--let $event_sequence= #Gtid/(.*'$slave_uuid:4') # !Begin # !Delete # !Commit #Gtid/(.*'$master_uuid:20') # !Begin # !Insert # !Commit #Gtid/(.*'$slave_uuid:5') # !Begin # !Delete # !Commit #Gtid/(.*'$master_uuid:21') # !Begin # !Insert # !Commit
--source include/assert_binlog_events.inc
--source include/save_binlog_position.inc

# Scenario 2.5:
--source include/rpl_connection_master.inc
DELIMITER |;
CREATE FUNCTION f () RETURNS INT BEGIN
INSERT INTO mem_t6 VALUES (1);
RETURN 2;
END|
DELIMITER ;|
INSERT INTO mem_t7 VALUES (f());
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_slave.inc
--let $limit= 2,16
--let $event_sequence= #Gtid/(.*'$slave_uuid:6') # !Begin # !Delete # !Commit #Gtid/(.*'$slave_uuid:7') # !Begin # !Delete # !Commit #Gtid/(.*'$master_uuid:23') # !Begin # !Multi_DML # !Commit
--source include/assert_binlog_events.inc
--source include/save_binlog_position.inc

#Scenario 2.6:
--source include/rpl_connection_master.inc
--eval SET @@session.gtid_next = '$explicit_gtid'
INSERT INTO mem_t8 VALUES (1), (2), (3), (4);
SET @@session.gtid_next = 'AUTOMATIC';
--source include/sync_slave_sql_with_master.inc
--source include/rpl_connection_slave.inc
--let $limit= 0,9
--let $event_sequence= #Gtid/(.*'$slave_uuid:8') # !Begin # !Delete # !Commit #Gtid/(.*'$explicit_gtid')# !Begin # !Insert # !Commit
--source include/assert_binlog_events.inc

# Cleanup
--source include/rpl_connection_master.inc
DROP FUNCTION f;
DROP TABLE mem_t1, mem_t2, mem_t3, mem_t4, mem_t5, mem_t6, mem_t7, mem_t8, mem_t9;
--let $dont_print_pattern = 0
--source include/rpl_end.inc
