1
###################################
3
# Test of replicating user variables
5
###################################
7
-- source include/master-slave.inc
8
# Disable PS as the log positions differs
12
# Clean up old slave's binlogs.
13
# The slave is started with --log-slave-updates
14
# and this test does SHOW BINLOG EVENTS on the slave's
15
# binlog. But previous tests can influence the current test's
16
# binlog (e.g. a temporary table in the previous test has not
17
# been explicitly deleted, or it has but the slave hasn't had
18
# enough time to catch it before STOP SLAVE,
19
# and at the beginning of the current
20
# test the slave immediately writes DROP TEMPORARY TABLE this_old_table).
21
# We wait for the slave to have written all he wants to the binlog
22
# (otherwise RESET MASTER may come too early).
29
create table t1(n char(30));
30
set @i1:=12345678901234, @i2:=-12345678901234, @i3:=0, @i4:=-1;
31
set @s1:='This is a test', @r1:=12.5, @r2:=-12.5;
33
set @s2:='', @s3:='abc\'def', @s4:= 'abc\\def', @s5:= 'abc''def';
34
insert into t1 values (@i1), (@i2), (@i3), (@i4);
35
insert into t1 values (@r1), (@r2);
36
insert into t1 values (@s1), (@s2), (@s3), (@s4), (@s5);
37
insert into t1 values (@n1);
38
insert into t1 values (@n2); # not explicitely set before
39
insert into t1 values (@a:=0), (@a:=@a+1), (@a:=@a+1);
40
insert into t1 values (@a+(@b:=@a+1));
42
insert t1 values (@q), (@q:=concat(@q, 'n1')), (@q:=concat(@q, 'n2'));
44
insert into t1 values (@a),(@a);
45
# To flush the pending event, we add the following statement. RBR can
46
# concatenate the result of several statements, which SBR cannot.
47
select * from t1 where n = '<nonexistant>';
48
connection master1; # see if variable is reset in binlog when thread changes
49
insert into t1 values (@a),(@a),(@a*5);
50
SELECT * FROM t1 ORDER BY n;
51
sync_slave_with_master;
52
SELECT * FROM t1 ORDER BY n;
54
insert into t1 select * FROM (select @var1 union select @var2) AS t2;
56
--echo End of 4.1 tests.
59
# The following tests ensure that if user-defined variables are used in SF/Triggers
60
# that they are replicated correctly. These tests should be run in both SBR and RBR
63
# This test uses a procedure that inserts data values based on the value of a
64
# user-defined variable. It also has a trigger that inserts data based on the
65
# same variable. Successful test runs show that the @var is replicated
66
# properly and that the procedure and trigger insert the correct data on the
69
# The test of stored procedure was included for completeness. Replication of stored
70
# procedures was not directly affected by BUG#20141.
72
# This test was constructed for BUG#20141
75
DROP TABLE IF EXISTS t20;
76
DROP TABLE IF EXISTS t21;
77
DROP PROCEDURE IF EXISTS test.insert;
80
CREATE TABLE t20 (a VARCHAR(20));
81
CREATE TABLE t21 (a VARCHAR(20));
84
# Create a procedure that uses the @var for flow control
86
CREATE PROCEDURE test.insert()
90
INSERT INTO test.t20 VALUES ('SP_TRUE');
92
INSERT INTO test.t20 VALUES ('SP_FALSE');
96
# Create a trigger that uses the @var for flow control
98
CREATE TRIGGER test.insert_bi BEFORE INSERT
99
ON test.t20 FOR EACH ROW
103
INSERT INTO test.t21 VALUES ('TRIG_TRUE');
105
INSERT INTO test.t21 VALUES ('TRIG_FALSE');
110
sync_slave_with_master;
113
# Set @var and call the procedure, repeat with different values
120
--echo On master: Check the tables for correct data
125
sync_slave_with_master;
127
--echo On slave: Check the tables for correct data and it matches master
137
DROP PROCEDURE test.insert;
139
# This test uses a stored function that uses user-defined variables to return data
140
# This test was constructed for BUG#20141
143
DROP TABLE IF EXISTS t1;
144
DROP FUNCTION IF EXISTS test.square;
147
CREATE TABLE t1 (i INT);
149
# Create function that returns a value from @var. In this case, the square function
151
CREATE FUNCTION test.square() RETURNS INTEGER DETERMINISTIC RETURN
154
# Set the @var to different values and insert them into a table
157
INSERT INTO t1 VALUES (square());
159
INSERT INTO t1 VALUES (square());
161
INSERT INTO t1 VALUES (square());
163
INSERT INTO t1 VALUES (square());
165
INSERT INTO t1 VALUES (square());
167
--echo On master: Retrieve the values from the table
171
sync_slave_with_master;
173
--echo On slave: Retrieve the values from the table and verify they are the same as on master
182
DROP FUNCTION test.square;
184
# This test uses stored functions that uses user-defined variables to return data
185
# based on the use of @vars inside a function body.
186
# This test was constructed for BUG#14914
189
DROP TABLE IF EXISTS t1;
190
DROP FUNCTION IF EXISTS f1;
191
DROP FUNCTION IF EXISTS f2;
194
CREATE TABLE t1(a int);
197
# Create a function that simply returns the value of an @var.
198
# Create a function that uses an @var for flow control, creates and uses another
199
# @var and sets its value to a value based on another @var.
201
CREATE FUNCTION f1() returns int deterministic BEGIN
205
CREATE FUNCTION f2() returns int deterministic BEGIN
215
sync_slave_with_master;
218
# Set an @var to a value and insert data into a table using the first function.
219
# Set two more @vars to some values and insert data into a table using the second function.
222
INSERT INTO t1 values(f1());
225
INSERT INTO t1 values(f2());
227
--echo On master: Retrieve the values from the table
229
sync_slave_with_master;
236
--echo On slave: Check the tables for correct data and it matches master
248
# This test uses a function that changes a user-defined variable in its body. This test
249
# will ensure the @vars are replicated when needed and not interrupt the normal execution
250
# of the function on the slave. This also applies to procedures and triggers.
252
# This test was constructed for BUG#25167
255
DROP TABLE IF EXISTS t1;
256
DROP TABLE IF EXISTS t2;
258
CREATE TABLE t1 (i int);
259
CREATE TABLE t2 (k int);
262
# Create a trigger that inserts data into another table, changes the @var then inserts
263
# another row with the modified value.
265
CREATE trigger t1_bi before INSERT on t1 for each row BEGIN
266
INSERT INTO t2 values (@a);
268
INSERT INTO t2 values (@a);
272
sync_slave_with_master;
275
# Set the @var to a value then insert data into first table.
278
INSERT INTO t1 values (5);
280
--echo On master: Check to see that data was inserted correctly in both tables
285
sync_slave_with_master;
287
--echo On slave: Check the tables for correct data and it matches master
296
# Bug #12826: Possible to get inconsistent slave using SQL syntax Prepared Statements
299
create table t1(a int, b int);
300
prepare s1 from 'insert into t1 values (@x:=@x+1, ?)';
301
set @x=1; execute s1 using @x;
303
sync_slave_with_master;
310
# Bug#33851: Passing UNSIGNED param to EXECUTE returns ERROR 1210
314
create table t1(a int);
315
insert into t1 values (1),(2);
316
prepare s1 from 'insert into t1 select a from t1 limit ?';
322
sync_slave_with_master;
328
--echo End of 5.0 tests.
330
# This test uses a stored function that uses user-defined variables to return data
331
# The test ensures the value of the user-defined variable is replicated correctly
332
# and in the correct order of assignment.
334
# This test was constructed for BUG#20141
337
DROP FUNCTION IF EXISTS f1;
338
DROP FUNCTION IF EXISTS f2;
341
CREATE TABLE t1 (i INT);
343
# Create two functions. One simply returns the user-defined variable. The other
344
# returns a value based on the user-defined variable.
346
CREATE FUNCTION f1() RETURNS INT RETURN @a; DELIMITER |; CREATE
347
FUNCTION f2() RETURNS INT BEGIN
348
INSERT INTO t1 VALUES (10 + @a);
353
sync_slave_with_master;
356
# Set the variable and execute the functions.
361
--echo On master: Check to see that data was inserted correctly
363
INSERT INTO t1 VALUES(f1());
366
sync_slave_with_master;
368
--echo On slave: Check the table for correct data and it matches master
380
sync_slave_with_master;