SET STORAGE_ENGINE = Falcon;
#
# Mantis bug #138: Error "Can't find record"
#   Slightly modified not to output randon numbers.
#
# Note: The actual test is to loop 1 million times. I reduced it 
#   to 100000, but it takes still to much time on our internal
#   servers. Therefore the loop is reduced to 2500. However, we
#   should test it with 1 million from time to time.
#
--echo *** Bug #138 ***
--disable_warnings
DROP TABLE IF EXISTS t1;
DROP PROCEDURE IF EXISTS p1;
--enable_warnings

--echo # Establish connection conn1 (user = root)
connect (conn1,localhost,root,,);
SET @@autocommit = 1;

--echo # Switch to connection default
connection default;
SET @@autocommit = 1;
CREATE TABLE t1 (a int(11), KEY a (a));

delimiter //;
CREATE PROCEDURE p1()
begin declare v1 int default 0;
declare v2 int;
declare continue handler for 1020 begin end;
declare continue handler for 1213 begin end;
while v1 < 2500 do
  /* SELECT 'insert', v1; */
  INSERT INTO t1 VALUES (v1);
  SET v2 = rand() * 10000;
  UPDATE t1 SET a = v2 WHERE a = v1;
  /* SELECT ' update', v1; */
  SET v1 = v1 + 1;
  if v1 mod 50 = 0 then
    /* SELECT ' delete'; */
    DELETE FROM t1;
  end if;
  end while;
end//
delimiter ;//

--echo # Sent call p1() to the server but do not pull the results
--send call p1()

--echo # Switch to connection conn1
connection conn1;
--real_sleep 1
call p1();

--echo # Switch to connection default
connection default;
--echo # Pull the results of the preceeding call p1()
reap;
--echo # Sent call p1() to the server but do not pull the results
--send call p1()

--echo # Switch to connection conn1
connection conn1;
call p1();

--echo # Switch to connection default
connection default;
--echo # Pull the results of the preceeding call p1()
reap;

# Final cleanup.
--echo # Disconnect conn1
disconnect conn1;
DROP TABLE t1;
DROP PROCEDURE p1;
