########################################################################################
# This test verifies if the server migrates correctly from a file repository to a
# table repository and vice-versa. In particular, it checks if the information in the
# relay log info is correctly migrated between the different types (i.e. FILE or TABLE)
# of repositories. The algorithm and the function used to migrate the master info is
# the same and for that reason, we do not test its migration.
# 
# If a FILE repository is used, the following assertions are valid:
#   AF1. SELECT COUNT(*) FROM mysql.slave_relay_log_info == 0
#   AF2. file_exists $MYSQLD_DATADIR/relay-log.info == 1
#
# If a TABLE repository is used, the following assertions are valid:
#   AT1. SELECT COUNT(*) FROM mysql.slave_relay_log_info == 1
#   AT2. file_exists $MYSQLD_DATADIR/relay-log.info == 0
#
# The test case is organized as follows:
#
# 1. Preparation:
#   1.1. The slave is started with a FILE repository enabled and the replication
#   stopped.
#   1.2. A table is created and populated in order to check at the end of the test
#   if data is replicated correctly.
#   1.3. Assertions AF1 and AF2 are verified.
#
# 2. Migration from FILE to TABLE by restarting the SERVER
#   2.1. The slave is stopped and restarted with --relay-log-info-repository=TABLE
#   2.2. Assertions AT1 and AT2 are verified.
#
# 3. Migration from TABLE to FILE by restarting the SERVER
#   4.1. The slave is stopped and restarted with --relay-log-info-repository=FILE
#   4.2. Assertions AF1 and AF2 are verified.
#
# 4. Migration from FILE to TALBE by using SET
#   4.1. SET @GLOBAL.relay_log_info_repository=TABLE is exectued.
#   4.2. Assertions AT1 and AT2 are verified.
#
# 5. Migration from TABLE to FILE by using SET
#   5.1. SET @GLOBAL.relay_log_info_repository=FILE is executed.
#   5.2. Assertions AF1 and AF2 are verified.
#
# 6. Migration from FILE to FILE by using SET
#   6.1. SET @GLOBAL.relay_log_info_repository=FILE is executed.
#   6.2. Assertions AF1 and AF2 are verified.
#
# 7. Migration while slave is running by using SET
#   7.1  Slave is started.
#   7.2. SET @GLOBAL.relay_log_info_repository=FILE is executed and fails.
#   7.3. Assertions AF1 and AF2 are verified.
#
# 8. Check consistency
#   8.1. The replication is started and the master is compared to the slave.
########################################################################################
########################################################################################
# 1. Preparation
########################################################################################
--source include/master-slave.inc
--source include/not_embedded.inc
--source include/not_valgrind.inc
--source include/not_relay_log_info_table.inc
--source include/not_mts_slave_parallel_workers.inc

--connection slave

let $MYSQLD_DATADIR= `SELECT @@datadir`;

let $exp_slave= 0;
let $got_slave= `SELECT COUNT(*) FROM mysql.slave_relay_log_info`;
if ($got_slave != $exp_slave)
{
  --echo "The mysql.slave_relay_log_info has information and this is not expected."
  --die
}
file_exists $MYSQLD_DATADIR/relay-log.info;

--source include/stop_slave.inc

--connection master

CREATE TABLE test(id INTEGER NOT NULL PRIMARY KEY);
INSERT INTO test VALUES (1), (2), (3);

########################################################################################
# 2. Migration from FILE to TABLE by restarting the SERVER
########################################################################################
--connection slave

--let $rpl_server_number= 2
--let $rpl_server_parameters= --relay-log-info-repository=TABLE --skip-slave-start
--source include/rpl_restart_server.inc

let $exp_slave= 1;
let $got_slave= `SELECT COUNT(*) FROM mysql.slave_relay_log_info`;
if ($got_slave != $exp_slave)
{
  --echo "The mysql.slave_relay_log_info has no information and this is not expected."
  --die
}
--error 1
file_exists $MYSQLD_DATADIR/relay-log.info;

########################################################################################
# 3. Migration from TABLE to FILE by restarting the SERVER
########################################################################################
--connection slave

--let $rpl_server_number= 2
--let $rpl_server_parameters= --relay-log-info-repository=FILE --skip-slave-start
--source include/rpl_restart_server.inc

let $exp_slave= 0;
let $got_slave= `SELECT COUNT(*) FROM mysql.slave_relay_log_info`;
if ($got_slave != $exp_slave)
{
  --echo "The mysql.slave_relay_log_info has information and this is not expected."
  --die
}
file_exists $MYSQLD_DATADIR/relay-log.info;

########################################################################################
# 4. Migration FROM FILE TO TABLE by using SET
########################################################################################
--connection slave

SET @@GLOBAL.relay_log_info_repository= "TABLE";

let $exp_slave= 1;
let $got_slave= `SELECT COUNT(*) FROM mysql.slave_relay_log_info`;
if ($got_slave != $exp_slave)
{
  --echo "The mysql.slave_relay_log_info has no information and this is not expected."
  --die
}
--error 1
file_exists $MYSQLD_DATADIR/relay-log.info;

########################################################################################
# 5. Migration FROM TABLE TO FILE by using SET
########################################################################################
--connection slave

SET @@GLOBAL.relay_log_info_repository= "FILE";

let $exp_slave= 0;
let $got_slave= `SELECT COUNT(*) FROM mysql.slave_relay_log_info`;
if ($got_slave != $exp_slave)
{
  --echo "The mysql.slave_relay_log_info has information and this is not expected."
  --die
}
file_exists $MYSQLD_DATADIR/relay-log.info;

########################################################################################
# 6. Migration FROM FILE TO FILE by using SET
########################################################################################
--connection slave

SET @@GLOBAL.relay_log_info_repository= "FILE";

let $exp_slave= 0;
let $got_slave= `SELECT COUNT(*) FROM mysql.slave_relay_log_info`;
if ($got_slave != $exp_slave)
{
  --echo "The mysql.slave_relay_log_info has information and this is not expected."
  --die
}
file_exists $MYSQLD_DATADIR/relay-log.info;

########################################################################################
# 7. Migration while slave is running by using SET
########################################################################################
--connection slave

--source include/start_slave.inc

--error ER_SLAVE_MUST_STOP
SET @@GLOBAL.relay_log_info_repository= "TABLE";

########################################################################################
# 8. Check consistency
########################################################################################
--connection master

--source include/sync_slave_sql_with_master.inc
--exec $MYSQL_DUMP --compact --order-by-primary --skip-extended-insert --no-create-info test > $MYSQLD_DATADIR/test-migration-master.sql
--exec $MYSQL_DUMP_SLAVE --compact --order-by-primary --skip-extended-insert --no-create-info test > $MYSQLD_DATADIR/test-migration-slave.sql
--diff_files $MYSQLD_DATADIR/test-migration-master.sql $MYSQLD_DATADIR/test-migration-slave.sql

--connection master

DROP TABLE test;
--source include/sync_slave_sql_with_master.inc
