Skip to content

Commit a941e58

Browse files
robertbindarsvoj
authored andcommitted
MDEV-788 mysqlimport should support the ability to disable foreign keys
1 parent 47637a3 commit a941e58

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

client/mysqlimport.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ static char *add_load_option(char *ptr,const char *object,
4848
const char *statement);
4949

5050
static my_bool verbose=0,lock_tables=0,ignore_errors=0,opt_delete=0,
51-
replace=0,silent=0,ignore=0,opt_compress=0,
52-
opt_low_priority= 0, tty_password= 0;
51+
replace, silent, ignore, ignore_foreign_keys,
52+
opt_compress, opt_low_priority, tty_password;
5353
static my_bool debug_info_flag= 0, debug_check_flag= 0;
5454
static uint opt_use_threads=0, opt_local_file=0, my_end_arg= 0;
5555
static char *opt_password=0, *current_user=0,
@@ -123,6 +123,10 @@ static struct my_option my_long_options[] =
123123
&current_host, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
124124
{"ignore", 'i', "If duplicate unique key was found, keep old row.",
125125
&ignore, &ignore, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
126+
{"ignore-foreign-keys", 'k',
127+
"Disable foreign key checks while importing the data.",
128+
&ignore_foreign_keys, &ignore_foreign_keys, 0, GET_BOOL, NO_ARG,
129+
0, 0, 0, 0, 0, 0},
126130
{"ignore-lines", OPT_IGN_LINES, "Ignore first n lines of data infile.",
127131
&opt_ignore_lines, &opt_ignore_lines, 0, GET_LL,
128132
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
@@ -487,6 +491,9 @@ static MYSQL *db_connect(char *host, char *database,
487491
ignore_errors=0;
488492
db_error(mysql);
489493
}
494+
if (ignore_foreign_keys)
495+
mysql_query(mysql, "set foreign_key_checks= 0;");
496+
490497
return mysql;
491498
}
492499

mysql-test/r/mysqldump.result

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5610,3 +5610,21 @@ DROP FUNCTION f;
56105610
#
56115611
DROP VIEW v1;
56125612
DROP FUNCTION f;
5613+
#
5614+
# MDEV-788 New option to ignore foreign key contraints in mysqlimport
5615+
#
5616+
create table t1 (
5617+
id int primary key
5618+
) engine=InnoDB;
5619+
create table t2 (
5620+
t1_id int,
5621+
CONSTRAINT fk
5622+
FOREIGN KEY (t1_id) REFERENCES t1 (id)
5623+
) ENGINE = InnoDB;
5624+
select count(*) from t2;
5625+
count(*)
5626+
1
5627+
select count(*) from t2;
5628+
count(*)
5629+
2
5630+
drop tables t2, t1;

mysql-test/t/mysqldump.test

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,7 @@ DROP TABLE t1;
24842484
DROP TABLE t2;
24852485
DROP DATABASE db_20772273;
24862486
USE test;
2487+
--remove_file $MYSQLTEST_VARDIR/tmp/t2.txt
24872488

24882489
--echo #
24892490
--echo # Bug #25717383: MYSQLDUMP MAY EXECUTE ANY ARBITRARY QUERY
@@ -2649,3 +2650,31 @@ DROP FUNCTION f;
26492650
--echo #
26502651
DROP VIEW v1;
26512652
DROP FUNCTION f;
2653+
2654+
--echo #
2655+
--echo # MDEV-788 New option to ignore foreign key contraints in mysqlimport
2656+
--echo #
2657+
create table t1 (
2658+
id int primary key
2659+
) engine=InnoDB;
2660+
2661+
create table t2 (
2662+
t1_id int,
2663+
CONSTRAINT fk
2664+
FOREIGN KEY (t1_id) REFERENCES t1 (id)
2665+
) ENGINE = InnoDB;
2666+
2667+
--write_file $MYSQLTEST_VARDIR/tmp/t2.txt
2668+
0
2669+
EOF
2670+
2671+
--error 1
2672+
--exec $MYSQL_IMPORT --silent test $MYSQLTEST_VARDIR/tmp/t2.txt
2673+
--exec $MYSQL_IMPORT --silent -k test $MYSQLTEST_VARDIR/tmp/t2.txt
2674+
select count(*) from t2;
2675+
2676+
--exec $MYSQL_IMPORT --silent --ignore-foreign-keys test $MYSQLTEST_VARDIR/tmp/t2.txt
2677+
select count(*) from t2;
2678+
2679+
--remove_file $MYSQLTEST_VARDIR/tmp/t2.txt
2680+
drop tables t2, t1;

0 commit comments

Comments
 (0)