1# Test how replication of tables work when the definition on the
2# master and slave differs.
3
4# Consider making these part of the basic RBR tests.
5
6connection master;
7--disable_warnings
8--disable_query_log
9DROP TABLE IF EXISTS t1_int,t1_bit,t1_char,t1_nodef;
10DROP TABLE IF EXISTS t2,t3,t4,t5,t6,t9;
11--enable_query_log
12--enable_warnings
13sync_slave_with_master;
14STOP SLAVE;
15SET @my_sql_mode= @@global.sql_mode;
16SET GLOBAL SQL_MODE='STRICT_ALL_TABLES';
17START SLAVE;
18
19connection master;
20eval CREATE TABLE t1_int (a INT PRIMARY KEY, b INT) ENGINE=$engine_type;
21eval CREATE TABLE t1_bit (a INT PRIMARY KEY, b INT) ENGINE=$engine_type;
22eval CREATE TABLE t1_char (a INT PRIMARY KEY, b INT) ENGINE=$engine_type;
23eval CREATE TABLE t1_nodef (a INT PRIMARY KEY, b INT) ENGINE=$engine_type;
24eval CREATE TABLE t2 (a INT PRIMARY KEY, b INT) ENGINE=$engine_type;
25eval CREATE TABLE t3 (a INT PRIMARY KEY, b INT) ENGINE=$engine_type;
26eval CREATE TABLE t4 (a INT) ENGINE=$engine_type;
27eval CREATE TABLE t5 (a INT, b INT, c INT) ENGINE=$engine_type;
28eval CREATE TABLE t6 (a INT, b INT, c INT) ENGINE=$engine_type;
29eval CREATE TABLE t7 (a INT NOT NULL) ENGINE=$engine_type;
30eval CREATE TABLE t8 (a INT NOT NULL) ENGINE=$engine_type;
31
32# Table used to detect that slave is running
33eval CREATE TABLE t9 (a INT) ENGINE=$engine_type;
34
35sync_slave_with_master;
36
37# On the slave, we add one INT column last in table 't1_int',
38ALTER TABLE t1_int ADD x INT DEFAULT 42;
39# ... and add BIT columns last in table 't1_bit' to ensure that we
40# have at least one extra null byte on the slave,
41ALTER TABLE t1_bit
42  ADD x BIT(3) DEFAULT b'011',
43  ADD y BIT(5) DEFAULT b'10101',
44  ADD z BIT(2) DEFAULT b'10';
45# ... and add one CHAR column last in table 't1_char',
46ALTER TABLE t1_char ADD x CHAR(20) DEFAULT 'Just a test';
47# ... and add one non-nullable INT column last in table 't1_text'
48#     with no default,
49ALTER TABLE t1_nodef ADD x INT NOT NULL, ADD y INT NOT NULL, ADD z INT NOT NULL;
50# ... and remove the last column in t2
51ALTER TABLE t2 DROP b;
52# ... change the type of the single column in table 't4'
53ALTER TABLE t4 MODIFY a FLOAT;
54# ... change the type of the middle column of table 't5'
55ALTER TABLE t5 MODIFY b FLOAT;
56# ... change the type of the last column of table 't6'
57ALTER TABLE t6 MODIFY c FLOAT;
58
59# ... add one byte worth of null bytes to the table on the slave
60ALTER TABLE t7 ADD e1 INT, ADD e2 INT, ADD e3 INT, ADD e4 INT,
61               ADD e5 INT, ADD e6 INT, ADD e7 INT, ADD e8 INT;
62
63# ... add 8 columns that are nullable: t8 will not be entirely
64#     nullable and have no null bits (just an X bit)
65ALTER TABLE t8 ADD e1 INT NOT NULL DEFAULT 0, ADD e2 INT NOT NULL DEFAULT 0,
66               ADD e3 INT NOT NULL DEFAULT 0, ADD e4 INT NOT NULL DEFAULT 0,
67               ADD e5 INT NOT NULL DEFAULT 0, ADD e6 INT NOT NULL DEFAULT 0,
68               ADD e7 INT NOT NULL DEFAULT 0, ADD e8 INT NOT NULL DEFAULT 0;
69
70# Insert some values for tables on slave side. These should not be
71# modified when the row from the master is applied.
72# since bug#31552/31609 idempotency is not default any longer. In order
73# the following INSERTs to pass the mode is switched temprorarily
74set @@global.slave_exec_mode= 'IDEMPOTENT';
75
76# so the inserts are going to be overriden
77INSERT INTO t1_int  VALUES (2, 4, 4711);
78INSERT INTO t1_char VALUES (2, 4, 'Foo is a bar');
79INSERT INTO t1_bit  VALUES (2, 4, b'101', b'11100', b'01');
80
81--echo **** On Master ****
82connection master;
83INSERT INTO t1_int VALUES (1,2);
84INSERT INTO t1_int VALUES (2,5);
85INSERT INTO t1_bit VALUES (1,2);
86INSERT INTO t1_bit VALUES (2,5);
87INSERT INTO t1_char VALUES (1,2);
88INSERT INTO t1_char VALUES (2,5);
89SELECT * FROM t1_int ORDER BY a;
90SELECT * FROM t1_bit ORDER BY a;
91SELECT * FROM t1_char ORDER BY a;
92--echo **** On Slave ****
93sync_slave_with_master;
94set @@global.slave_exec_mode= default;
95
96SELECT a,b,x FROM t1_int ORDER BY a;
97SELECT a,b,HEX(x),HEX(y),HEX(z) FROM t1_bit ORDER BY a;
98SELECT a,b,x FROM t1_char ORDER BY a;
99
100--echo **** On Master ****
101connection master;
102UPDATE t1_int  SET b=2*b WHERE a=2;
103UPDATE t1_char SET b=2*b WHERE a=2;
104UPDATE t1_bit  SET b=2*b WHERE a=2;
105SELECT * FROM t1_int ORDER BY a;
106SELECT * FROM t1_bit ORDER BY a;
107SELECT * FROM t1_char ORDER BY a;
108--echo **** On Slave ****
109sync_slave_with_master;
110SELECT a,b,x FROM t1_int ORDER BY a;
111SELECT a,b,HEX(x),HEX(y),HEX(z) FROM t1_bit ORDER BY a;
112SELECT a,b,x FROM t1_char ORDER BY a;
113
114connection master;
115INSERT INTO t9 VALUES (2);
116sync_slave_with_master;
117# Now slave is guaranteed to be running
118connection master;
119INSERT INTO t1_nodef VALUES (1,2);
120
121# Last insert on wider slave table succeeds while slave sql sql_mode permits.
122# The previous version of the above test expected slave sql to stop.
123# bug#38173 relaxed conditions to stop only with the strict mode.
124sync_slave_with_master;
125select count(*) from t1_nodef;
126
127#
128# Replicating to tables with fewer columns at the end works as of WL#3228
129#
130connection master;
131INSERT INTO t9 VALUES (2);
132sync_slave_with_master;
133# Now slave is guaranteed to be running
134connection master;
135--echo **** On Master ****
136INSERT INTO t2 VALUES (2,4);
137SELECT * FROM t2;
138sync_slave_with_master;
139--echo **** On Slave ****
140SELECT * FROM t2;
141--source include/check_slave_is_running.inc
142
143connection master;
144INSERT INTO t9 VALUES (4);
145sync_slave_with_master;
146# Now slave is guaranteed to be running
147connection master;
148INSERT INTO t4 VALUES (4);
149connection slave;
150call mtr.add_suppression("Slave SQL.*Table definition on master and slave does not match: Column [012] type mismatch.* Error_code: 1535");
151call mtr.add_suppression("Slave SQL.*Column [0-9] of table .test.t[0-9]. cannot be converted from type.* Error_code: 1677");
152call mtr.add_suppression("The slave coordinator and worker threads are stopped, possibly leaving data in inconsistent state");
153
154--let $slave_skip_counter= 2
155--let $slave_sql_errno= convert_error(ER_SLAVE_CONVERSION_FAILED) # 1677
156--source include/wait_for_slave_sql_error_and_skip.inc
157
158connection master;
159INSERT INTO t9 VALUES (5);
160sync_slave_with_master;
161# Now slave is guaranteed to be running
162connection master;
163INSERT INTO t5 VALUES (5,10,25);
164connection slave;
165--let $slave_skip_counter= 2
166--let $slave_sql_errno= convert_error(ER_SLAVE_CONVERSION_FAILED) # 1677
167--source include/wait_for_slave_sql_error_and_skip.inc
168
169connection master;
170INSERT INTO t9 VALUES (6);
171sync_slave_with_master;
172# Now slave is guaranteed to be running
173connection master;
174INSERT INTO t6 VALUES (6,12,36);
175connection slave;
176--let $slave_skip_counter= 2
177--let $slave_sql_errno= convert_error(ER_SLAVE_CONVERSION_FAILED) # 1677
178--source include/wait_for_slave_sql_error_and_skip.inc
179
180connection master;
181INSERT INTO t9 VALUES (6);
182sync_slave_with_master;
183--source include/check_slave_is_running.inc
184
185# Testing some tables extra field that can be null and cannot be null
186# (but have default values)
187
188connection master;
189INSERT INTO t7 VALUES (1),(2),(3);
190INSERT INTO t8 VALUES (1),(2),(3);
191SELECT * FROM t7 ORDER BY a;
192SELECT * FROM t8 ORDER BY a;
193sync_slave_with_master;
194SELECT * FROM t7 ORDER BY a;
195SELECT * FROM t8 ORDER BY a;
196
197# We will now try to update and then delete a row on the master where
198# the extra field on the slave does not have a default value. This
199# update should not generate an error even though there is no default
200# for the extra column.
201
202--echo **** On Master ****
203connection master;
204TRUNCATE t1_nodef;
205SET SQL_LOG_BIN=0;
206INSERT INTO t1_nodef VALUES (1,2);
207INSERT INTO t1_nodef VALUES (2,4);
208SET SQL_LOG_BIN=1;
209sync_slave_with_master;
210
211--echo **** On Slave ****
212connection slave;
213INSERT INTO t1_nodef VALUES (1,2,3,4,5);
214INSERT INTO t1_nodef VALUES (2,4,6,8,10);
215
216--echo **** On Master ****
217connection master;
218UPDATE t1_nodef SET b=2*b WHERE a=1;
219SELECT * FROM t1_nodef ORDER BY a;
220
221--echo **** On Slave ****
222sync_slave_with_master;
223SELECT * FROM t1_nodef ORDER BY a;
224
225--echo **** On Master ****
226connection master;
227DELETE FROM t1_nodef WHERE a=2;
228SELECT * FROM t1_nodef ORDER BY a;
229
230--echo **** On Slave ****
231sync_slave_with_master;
232SELECT * FROM t1_nodef ORDER BY a;
233
234--echo **** Cleanup ****
235connection master;
236--disable_warnings
237DROP TABLE IF EXISTS t1_int,t1_bit,t1_char,t1_nodef;
238DROP TABLE IF EXISTS t2,t3,t4,t5,t6,t7,t8,t9;
239--enable_warnings
240sync_slave_with_master;
241
242# Restore sql_mode
243SET @@global.sql_mode= @my_sql_mode;
244