# === Purpose ===
#
# This test verifies that update events are properly applied by the slave when
# same row is updated one than once within an Update_rows_log_event.
#
# === Implementation ===
#
# 1. Intital setup includes creating two tables - with and without primary key
#    and a stored function that updates same row more than once.
# 2. For each allowed value of slave_rows_search_algorithms,
#    2.1 Insert a row and invoke the stored function.
#    2.2 Verify that there is no difference between tables of master and
#        slave.
#
# === References ===
#
# Bug#28846386: RBR + STORED FUNCTION WITHOUT PRIMARY KEY - CAN'T FIND RECORD IN
#

--source include/have_binlog_format_row.inc
--source include/set_privilege_checks_user_as_system_user.inc
--source include/master-slave.inc

--source include/rpl_connection_slave.inc
SET @saved_slave_rows_search_algorithms= @@global.slave_rows_search_algorithms;

--source include/rpl_connection_master.inc
# Create a table, with no primary key.
CREATE TABLE t1 (i INT);
# Create a table, with primary key.
CREATE TABLE t2 (j INT NOT NULL PRIMARY KEY);

# Create a stored function so that only one Update_rows_log_event is generated.
--delimiter |
CREATE FUNCTION f1 () RETURNS INT BEGIN
  UPDATE t1 SET i = 2 WHERE i = 1;
  UPDATE t1 SET i = 3 WHERE i = 2;
  UPDATE t1 SET i = 4 WHERE i = 3;
  UPDATE t1 SET i = 5 WHERE i = 4;
  UPDATE t2 SET j = 2 WHERE j = 1;
  UPDATE t2 SET j = 3 WHERE j = 2;
  UPDATE t2 SET j = 4 WHERE j = 3;
  UPDATE t2 SET j = 5 WHERE j = 4;
  RETURN 0;
END|
--delimiter ;
--source include/sync_slave_sql_with_master.inc

--let $i= 0
while ($i < 7) {

  if ($i == 0){
    SET GLOBAL slave_rows_search_algorithms= 'HASH_SCAN';
  }

  if ($i == 1){
    SET GLOBAL slave_rows_search_algorithms= 'TABLE_SCAN';
  }

  if ($i == 2){
    SET GLOBAL slave_rows_search_algorithms= 'INDEX_SCAN';
  }

  if ($i == 3){
    SET GLOBAL slave_rows_search_algorithms= 'HASH_SCAN,TABLE_SCAN';
  }

  if ($i == 4){
    SET GLOBAL slave_rows_search_algorithms= 'HASH_SCAN,INDEX_SCAN';
  }

  if ($i == 5){
    SET GLOBAL slave_rows_search_algorithms= 'TABLE_SCAN,INDEX_SCAN';
  }

  if ($i == 6){
    SET GLOBAL slave_rows_search_algorithms= 'HASH_SCAN,TABLE_SCAN,INDEX_SCAN';
  }

  --source include/rpl_connection_master.inc

  # Insert a row and invoke the stored function.
  INSERT INTO t1 VALUES (1);
  INSERT INTO t2 VALUES (1);
  SELECT f1();

  --source include/sync_slave_sql_with_master.inc
  --let $assert_text= 'There is only one row in table t1'
  --let $assert_cond= [SELECT COUNT(*) FROM t1] = 1
  --source include/assert.inc

  --let $assert_text= 'There is only one row in table t2'
  --let $assert_cond= [SELECT COUNT(*) FROM t2] = 1
  --source include/assert.inc

  --source include/rpl_connection_master.inc

  # Verify that there is no difference between tables of master and slave.
  --let diff_tables=master:test.t1, slave:test.t1
  --source include/diff_tables.inc

  --let diff_tables=master:test.t2, slave:test.t2
  --source include/diff_tables.inc

  # Truncate the table for the next test.
  TRUNCATE TABLE t1;
  TRUNCATE TABLE t2;
  --source include/sync_slave_sql_with_master.inc
  --inc $i
}

# Cleanup
--source include/rpl_connection_master.inc
DROP FUNCTION f1;
DROP TABLE t1,t2;

--source include/rpl_connection_slave.inc
SET GLOBAL slave_rows_search_algorithms= @saved_slave_rows_search_algorithms;

--source include/rpl_end.inc
