--source include/have_debug.inc
--source include/have_innodb.inc
--source include/have_csv.inc
--source include/not_valgrind.inc
--source include/not_embedded.inc

# Speed up wait_until_connected_again.inc
let NO_WSREP=1;

#
# Testing of atomic rename of table with triggers when table rename works but
# rename of trigger fails.
# This test can't be combined with rename_table.test as we need to simulate
# an error in sql_create_definition_file()
#

let $crash_count=3;
let $crash_points='ddl_log_rename_after_failed_rename_trigger','ddl_log_rename_after_revert_rename_table', 'ddl_log_rename_after_disable_entry';

let $engine_count=5;
let $engines='myisam','aria','aria_notrans','innodb','csv';

# Number times sql_create_definition_file() is called during one statement.
# This is number of renames (5)*2
let $renames=10;

let $old_debug=`select @@debug_dbug`;

let $e=0;
--disable_query_log
while ($e < $engine_count)
{
  inc $e;
  let $engine=`select ELT($e, $engines)`;
  let $default_engine=$engine;
  let $extra_option=;

  if ($engine == "aria")
  {
    let $extra_option=transactional=1;
  }
  if ($engine == "aria_notrans")
  {
    let $default_engine="aria";
    let $extra_option=transactional=0;
  }

  --eval set @@default_storage_engine=$default_engine
  --eval create table t1 (a int not null) $extra_option;
  --eval create table t2 (b int not null) $extra_option;
  --eval create table t3 (c int not null) $extra_option;
  --eval create table t4 (d int not null) $extra_option;
  insert into t1 values(1);
  insert into t2 values(2);
  insert into t3 values(3);
  insert into t4 values(4);

  delimiter |;
  create trigger t1_trg before insert on t1 for each row
  begin
    if isnull(new.a) then
      set new.a:= 1000;
    end if;
  end|
  create trigger t2_trg before insert on t2 for each row
  begin
    if isnull(new.b) then
      set new.b:= 2000;
    end if;
  end|
  create trigger t3_trg before insert on t3 for each row
  begin
    if isnull(new.c) then
      set new.c:= 4000;
    end if;
  end|
  create trigger t4_trg before insert on t4 for each row
  begin
    if isnull(new.d) then
      set new.d:= 8000;
    end if;
  end|
  delimiter ;|
  let $c=0;
  while ($c < $crash_count)
  {
    inc $c;
    let $crash=`select ELT($c, $crash_points)`;
    let $r=0;
    while ($r < $renames)
    {
      inc $r;
      echo "engine: $engine  crash point: $crash  position: $r";
      flush tables;

    --exec echo "restart" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
    --disable_reconnect
    --eval set @@debug_dbug="+d,$crash,definition_file_simulate_write_error",@debug_crash_counter=1;
    --eval set @@debug_dbug="+d,definition_file_simulate_write_error",@debug_error_counter=$r;
      let $errno=0;
      --error 0,2013
      rename table t1 to t5, t2 to t1, t5 to t2, t4 to t5, t3 to t4;
      let $error=$errno;
      --enable_reconnect
      --source include/wait_until_connected_again.inc
      --disable_query_log
      --eval set @@debug_dbug="$old_debug"

      if ($error == 0)
      {
        echo "No crash!";
        # No crash, rename things back
        rename table t4 to t3, t5 to t4, t2 to t5, t1 to t2, t5 to t1;
      }

      # Ensure that the tables are back to original
      let $res=`select t1.a+t2.b+t3.c+t4.d from t1,t2,t3,t4`;
      if ($res != 10)
      {
        die "Got result $res when 10 was expected";
      }

      # Ensure that triggers work
      insert into t1 values(null);
      insert into t2 values(null);
      insert into t3 values(null);
      insert into t4 values(null);
      let $res=`select (select sum(t1.a) from t1)+ (select sum(t2.b) from t2) + (select sum(t3.c) from t3)+ (select sum(t4.d) from t4)`;
      if ($res != 15010)
      {
        die "Got result $res when 15010 was expected";
      }
      # Restore tables
      delete from t1 where a > 100;
      delete from t2 where b > 100;
      delete from t3 where c > 100;
      delete from t4 where d > 100;
    }
  }

  # Last test, check that rename really worked
  rename table t1 to t5, t2 to t1, t5 to t2, t4 to t5, t3 to t4;
  insert into t1 values(null);
  insert into t2 values(null);
  insert into t5 values(null);
  insert into t4 values(null);
  let $res=`select (select sum(t1.b) from t1)+ (select sum(t2.a) from t2) + (select sum(t4.c) from t4)+ (select sum(t5.d) from t5)`;
  if ($res != 15010)
  {
    die "Got result $res when 15010 was expected";
  }
  let $res=`select (select count(*)=2 from t1) + (select count(*)=2 from t2) + (select count(*)=2 from t4)+ (select count(*)=2 from t5)`;
  if ($res != 4)
  {
    die "Got result $res when 4 was expected";
  }

  drop table t1,t2,t4,t5;
}
--enable_query_log
--disable_query_log
