#
# Test multi-source dynamically setting of replication filter
# "replicate_rewrite_db"
#
# This test ensures that setting a replica's replicate_rewrite_db works on a
# per-master basis. To ensure this, this test connects a replica to two
# different primary server instances. Each primary uses the same database
# name and table name. To ensure the data operations don't interfere with one
# another on the replica, it sets replicate_rewrite_db individually per
# connection to apply the events from each primary into a database specific to
# that connection.
#

--source include/not_embedded.inc
--source include/have_innodb.inc

--connect (server_1,127.0.0.1,root,,,$SERVER_MYPORT_1)
--connect (server_2,127.0.0.1,root,,,$SERVER_MYPORT_2)
--connect (server_3,127.0.0.1,root,,,$SERVER_MYPORT_3)


--echo # Connect the slave (server_3) to two masters (server_1 and server_2)
--connection server_3
--replace_result $SERVER_MYPORT_1 MYPORT_1
eval CHANGE MASTER 'm1' TO master_port=$SERVER_MYPORT_1, master_host='127.0.0.1', master_user='root', master_ssl_verify_server_cert=0;
--replace_result $SERVER_MYPORT_2 MYPORT_2
eval CHANGE MASTER 'm2' TO master_port=$SERVER_MYPORT_2, master_host='127.0.0.1', master_user='root', master_ssl_verify_server_cert=0;

--echo # Apply events from server_1 (m1) into m1_test
create database m1_test;
SET @@global.'m1'.replicate_rewrite_db='test->m1_test';

--echo # Apply events from server_2 (m2) into m2_test
create database m2_test;
SET @@global.'m2'.replicate_rewrite_db='test->m2_test';

start all slaves;
set default_master_connection = 'm1';
--source include/wait_for_slave_to_start.inc
set default_master_connection = 'm2';
--source include/wait_for_slave_to_start.inc

--echo # Create test data for servers 1 and 2 with different data
--connection server_1
create table t (a int);
insert into t values (1);
insert into t values (2);
insert into t values (3);
--source include/save_master_gtid.inc
--connection server_3
--source include/sync_with_master_gtid.inc

--connection server_2
create table t (a int);
insert into t values (4);
insert into t values (5);
insert into t values (6);
--source include/save_master_gtid.inc
--connection server_3
--source include/sync_with_master_gtid.inc

--echo # Ensure the slave correctly replicates data from each master into its
--echo # respective database
--let $diff_tables=server_1:test.t,server_3:m1_test.t
--source include/diff_tables.inc

--let $diff_tables=server_2:test.t,server_3:m2_test.t
--source include/diff_tables.inc


--echo #
--echo # Cleanup

--connection server_1
DROP TABLE t;
--source include/save_master_gtid.inc
--connection server_3
--source include/sync_with_master_gtid.inc

--connection server_2
DROP TABLE t;
--source include/save_master_gtid.inc
--connection server_3
--source include/sync_with_master_gtid.inc

--connection server_3
stop all slaves;
SET default_master_connection = "m1";
--source include/wait_for_slave_to_stop.inc
SET default_master_connection = "m2";
--source include/wait_for_slave_to_stop.inc
RESET SLAVE ALL;
DROP DATABASE m1_test;
DROP DATABASE m2_test;

--echo # End of replicate_rewrite_db_dynamic.test
