#
--source include/master-slave.inc
--source include/have_debug.inc
--source include/have_binlog_format_row.inc

# BUG#11753004: 44360: REPLICATION FAILED 

## assert that we get an error when checking the
## identifiers at the slave (instead of a crash or
## different table being updated)

--let $t1= t1_11753004
--let $t2= t2_11753004
--let $t2_ign= t2_11753004_ign

## test #1: assert that we get an error raised when multiple
##          tables in the same RBR statement are mapped with the
##          same identifier

--eval CREATE TABLE $t1 (c1 INT)
--eval CREATE TABLE $t2 (c1 INT)
--eval INSERT INTO $t1 VALUES (1)
--eval INSERT INTO $t2 VALUES (2)

--sync_slave_with_master
call mtr.add_suppression(".*Found table map event mapping table id 0 which was already mapped but with different settings.*");

# stop the slave and inject corruption
--source include/stop_slave.inc
SET @save_debug= @@global.debug;
SET GLOBAL debug="+d,inject_tblmap_same_id_maps_diff_table";
--source include/start_slave.inc
--connection master
# both tables get mapped to 0 (in a way, simulating scenario 
# originated by BUG#56226)
--eval UPDATE $t1, $t2 SET $t1.c1=3, $t2.c1=4 WHERE $t1.c1=1 OR $t2.c1=2
--connection slave

# wait for error 1593 (ER_SLAVE_FATAL_ERROR)
--let $slave_sql_errno=1593 
--source include/wait_for_slave_sql_error.inc
--source include/stop_slave.inc

# clean up
SET GLOBAL debug="-d,inject_tblmap_same_id_maps_diff_table";
--source include/start_slave.inc
--connection master
--source include/rpl_reset.inc
--eval DROP TABLE $t1, $t2
--sync_slave_with_master

## test #2: assert that ignored tables that may have been mapped
##          with the same identifier are skipped, thus no error
##          is raised.

--connection slave
--source include/stop_slave.inc
SET GLOBAL debug="+d,inject_tblmap_same_id_maps_diff_table";
--source include/start_slave.inc
--source include/rpl_reset.inc
--connection master
--eval CREATE TABLE $t1 (c1 INT)
--eval CREATE TABLE $t2_ign (c1 INT)
--eval INSERT INTO $t1 VALUES (1)
--eval INSERT INTO $t2_ign VALUES (2)
--eval UPDATE $t1, $t2_ign SET $t1.c1=3, $t2_ign.c1=4 WHERE $t1.c1=1 OR $t2_ign.c1=2

# must not raise error as second table is filtered
--sync_slave_with_master


## test #3: check that BINLOG statements will also raise an
##          error if containing table map events mapping different
##          tables to same table identifier.

CREATE TABLE t1 (c1 INT);
CREATE TABLE t2 (c1 INT);

INSERT INTO t1 VALUES (1);
INSERT INTO t2 VALUES (1);

# FD event
BINLOG '
SOgWTg8BAAAAbgAAAHIAAAAAAAQANS42LjMtbTUtZGVidWctbG9nAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAABI6BZOEzgNAAgAEgAEBAQEEgAAVgAEGggAAAAICAgCAAAAAAVAYI8=
'/*!*/;

#110708 12:21:44 server id 1  end_log_pos 774   Table_map: `test`.`t1` mapped to number 66
# at 774
#110708 12:21:44 server id 1  end_log_pos 815   Table_map: `test`.`t2` mapped to number 67
# at 815
#110708 12:21:44 server id 1  end_log_pos 855   Update_rows: table id 66
# at 855
#110708 12:21:44 server id 1  end_log_pos 895   Update_rows: table id 67 flags: STMT_END_F
SET GLOBAL debug="+d,inject_tblmap_same_id_maps_diff_table";
--error ER_SLAVE_FATAL_ERROR
BINLOG '
SOgWThMBAAAAKQAAAAYDAAAAAEIAAAAAAAEABHRlc3QAAnQxAAEDAAE=
SOgWThMBAAAAKQAAAC8DAAAAAEMAAAAAAAEABHRlc3QAAnQyAAEDAAE=
SOgWThgBAAAAKAAAAFcDAAAAAEIAAAAAAAAAAf///gEAAAD+AwAAAA==
SOgWThgBAAAAKAAAAH8DAAAAAEMAAAAAAAEAAf///gEAAAD+BAAAAA==
'/*!*/;


# clean up
DROP TABLE t1,t2;
--connection slave
SET GLOBAL debug="-d,inject_tblmap_same_id_maps_diff_table";
--connection master
--eval DROP TABLE $t1
--eval DROP TABLE $t2_ign
--sync_slave_with_master
SET GLOBAL debug= @save_debug;

--source include/rpl_end.inc
