Skip to content

Commit 332be3b

Browse files
committed
Bug #34325 : --add-drop-trigger option for mysqldump
Implemented the --add-drop-trigger option to prepend each CREATE TRIGGER in the dump file with DROP TRIGGER. The option is off by default. Added a test case.
1 parent 7cc09ac commit 332be3b

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

client/mysqldump.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static my_bool verbose= 0, opt_no_create_info= 0, opt_no_data= 0,
101101
opt_slave_apply= 0,
102102
opt_include_master_host_port= 0,
103103
opt_events= 0,
104-
opt_alltspcs=0, opt_notspcs= 0;
104+
opt_alltspcs=0, opt_notspcs= 0, opt_drop_trigger= 0;
105105
static my_bool insert_pat_inited= 0, debug_info_flag= 0, debug_check_flag= 0;
106106
static ulong opt_max_allowed_packet, opt_net_buffer_length;
107107
static MYSQL mysql_connection,*mysql=0;
@@ -202,6 +202,9 @@ static struct my_option my_long_options[] =
202202
{"add-drop-table", OPT_DROP, "Add a DROP TABLE before each create.",
203203
&opt_drop, &opt_drop, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0,
204204
0},
205+
{"add-drop-trigger", 0, "Add a DROP TRIGGER before each create.",
206+
(uchar**) &opt_drop_trigger, (uchar**) &opt_drop_trigger, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
207+
0},
205208
{"add-locks", OPT_LOCKS, "Add locks around INSERT statements.",
206209
&opt_lock, &opt_lock, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0,
207210
0},
@@ -2764,6 +2767,9 @@ static void dump_trigger_old(FILE *sql_file, MYSQL_RES *show_triggers_rs,
27642767
if (opt_compact)
27652768
fprintf(sql_file, "/*!50003 SET @OLD_SQL_MODE=@@SQL_MODE*/;\n");
27662769

2770+
if (opt_drop_trigger)
2771+
fprintf(sql_file, "/*!50032 DROP TRIGGER IF EXISTS %s */;\n", (*show_trigger_row)[0]);
2772+
27672773
fprintf(sql_file,
27682774
"DELIMITER ;;\n"
27692775
"/*!50003 SET SESSION SQL_MODE=\"%s\" */;;\n"
@@ -2840,6 +2846,9 @@ static int dump_trigger(FILE *sql_file, MYSQL_RES *show_create_trigger_rs,
28402846

28412847
switch_sql_mode(sql_file, ";", row[1]);
28422848

2849+
if (opt_drop_trigger)
2850+
fprintf(sql_file, "/*!50032 DROP TRIGGER IF EXISTS %s */;\n", row[0]);
2851+
28432852
fprintf(sql_file,
28442853
"DELIMITER ;;\n"
28452854
"/*!50003 %s */;;\n"

mysql-test/r/mysqldump.result

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4578,3 +4578,53 @@ DROP TABLE t1, t2;
45784578
#
45794579
# End of 5.1 tests
45804580
#
4581+
#
4582+
# Test for --add-drop-trigger
4583+
#
4584+
CREATE TABLE t1 (a int, b int);
4585+
CREATE TRIGGER tt1_t1 BEFORE INSERT ON t1 FOR EACH ROW
4586+
SET NEW.b=NEW.a + 10;
4587+
INSERT INTO t1 (a) VALUES (1),(2),(3);
4588+
4589+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
4590+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
4591+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
4592+
/*!40101 SET NAMES utf8 */;
4593+
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
4594+
/*!40103 SET TIME_ZONE='+00:00' */;
4595+
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
4596+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
4597+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
4598+
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
4599+
4600+
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFAULT CHARACTER SET latin1 */;
4601+
4602+
USE `test`;
4603+
/*!50003 SET @saved_cs_client = @@character_set_client */ ;
4604+
/*!50003 SET @saved_cs_results = @@character_set_results */ ;
4605+
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
4606+
/*!50003 SET character_set_client = latin1 */ ;
4607+
/*!50003 SET character_set_results = latin1 */ ;
4608+
/*!50003 SET collation_connection = latin1_swedish_ci */ ;
4609+
/*!50003 SET @saved_sql_mode = @@sql_mode */ ;
4610+
/*!50003 SET sql_mode = '' */ ;
4611+
/*!50032 DROP TRIGGER IF EXISTS tt1_t1 */;
4612+
DELIMITER ;;
4613+
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER tt1_t1 BEFORE INSERT ON t1 FOR EACH ROW
4614+
SET NEW.b=NEW.a + 10 */;;
4615+
DELIMITER ;
4616+
/*!50003 SET sql_mode = @saved_sql_mode */ ;
4617+
/*!50003 SET character_set_client = @saved_cs_client */ ;
4618+
/*!50003 SET character_set_results = @saved_cs_results */ ;
4619+
/*!50003 SET collation_connection = @saved_col_connection */ ;
4620+
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
4621+
4622+
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
4623+
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
4624+
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
4625+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
4626+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
4627+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
4628+
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
4629+
4630+
DROP TABLE t1;

mysql-test/t/mysqldump.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,5 +2173,17 @@ DROP TABLE t1, t2;
21732173
--echo # End of 5.1 tests
21742174
--echo #
21752175

2176+
--echo #
2177+
--echo # Test for --add-drop-trigger
2178+
--echo #
2179+
2180+
CREATE TABLE t1 (a int, b int);
2181+
CREATE TRIGGER tt1_t1 BEFORE INSERT ON t1 FOR EACH ROW
2182+
SET NEW.b=NEW.a + 10;
2183+
2184+
INSERT INTO t1 (a) VALUES (1),(2),(3);
2185+
--exec $MYSQL_DUMP --triggers --no-data --no-create-info --add-drop-trigger --skip-comments --databases test
2186+
DROP TABLE t1;
2187+
21762188
# Wait till we reached the initial number of concurrent sessions
21772189
--source include/wait_until_count_sessions.inc

0 commit comments

Comments
 (0)