Mysql – csv import fails without throwing an error

csvimportMySQL

Ι am trying to import a csv file into an existing mysql 5.7 table.

Here is the command (the db is in docker with bind mount for persistence)

mysql -h 127.0.0.1 -uroot -prootpass --local_infile=1 jira_statistics -e "LOAD DATA LOCAL INFILE '/home/centos/bugs.csv' INTO TABLE bugs FIELDS TERMINATED BY ','"

However:

mysql> select * from bugs;
Empty set (0.05 sec)

The file cannot be loaded because there is a foreign key constraint. I need to be able to catch errors like this (and any others that might occur) as an exit code since I will be scripting a large number of imports like this.

There is no error returned to the command line, and the docker logs of the MySQL container show no errors.

What is more, the exit code of the mysql command is 0!

Any ideas?

Best Answer

Encapsulate the LOAD in a Stored Procedure. In there, capture the error and expose it to your script.

However, if LOAD DATA is not allowed in a Stored Proc, then you really need to write application code, not depend on on-line shell scripts.