Skip to content

Commit 2cc8f33

Browse files
author
Dave MacFarlane
committed
Convert from insert statement to load data local infile
1 parent e2fb98e commit 2cc8f33

File tree

2 files changed

+19
-53
lines changed

2 files changed

+19
-53
lines changed

tools/exporters/DB_dump_table_data.php

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -67,61 +67,25 @@
6767
die();
6868
}
6969

70-
/*
71-
* Definitions of the flags used in the command below (ORDERING is IMPORTANT):
72-
* --complete-insert -> Use complete INSERT statements that include column names
73-
* --no-create-db -> Do not write CREATE DATABASE statements
74-
* --no-create-info -> Do not write CREATE TABLE statements that re-create each
75-
* dumped table
76-
* --skip-opt -> Do not add any of the --opt options (--opt is shorthand for:
77-
* --add-drop-table --add-locks --create-options --disable-keys
78-
* --extended-insert --lock-tables --quick --set-charset)
79-
* --compact -> This option enables the --skip-add-drop-table,
80-
* --skip-add-locks, --skip-comments, --skip-disable-keys,
81-
* and --skip-set-charset
82-
* --add-locks -> To undo part of --compact. Surround each table dump with
83-
* LOCK TABLES and UNLOCK TABLES statements
84-
* --verbose -> Print more information about what the program does.
85-
* --skip-tz-utc -> This option prevents MySQL from modifying TIMESTAMP
86-
* values to accommodate for timezone differences.
87-
*/
88-
89-
90-
// Check MySQL version
91-
$mysqlVersionOutput = shell_exec(
92-
'mysql -u '.escapeshellarg($adminUser).' -p'.escapeshellarg($adminPassword).
93-
' -h '.escapeshellarg($dbHost).' -e "SELECT VERSION();" 2>/dev/null'
94-
);
95-
96-
preg_match('/\d+\.\d+\.\d+/', $mysqlVersionOutput, $matches);
97-
$mysqlVersion = $matches[0] ?? '0.0.0';
98-
99-
// Determine whether --column-statistics=0 is needed
100-
$columnStatisticsFlag = version_compare($mysqlVersion, '8.0.0', '<=') ?
101-
'--column-statistics=0 ' : '';
102-
10370
// Loop through all tables to generate insert statements for each.
10471
foreach ($tableNames as $tableName) {
10572
$paths = \NDB_Config::singleton()->getSetting('paths');
106-
$filename = $paths['base'] . "/raisinbread/RB_files/RB_$tableName.sql";
73+
$filenamebase = $paths['base'] . "/raisinbread/RB_files/$tableName";
10774
exec(
108-
'mysqldump -u '.escapeshellarg($adminUser).
75+
'mysql --raw -u '.escapeshellarg($adminUser).
10976
' -p'.escapeshellarg($adminPassword).' -h '.escapeshellarg($dbHost).' '.
110-
escapeshellarg($databaseInfo['database']).' '.
111-
$columnStatisticsFlag.
112-
'--complete-insert '.
113-
'--no-create-db '.
114-
'--no-create-info '.
115-
'--skip-opt '.
116-
'--compact '.
117-
'--add-locks '.
118-
'--verbose '.
119-
'--skip-tz-utc '.
120-
$tableName .
121-
' | sed -E \'s/LOCK TABLES (`[^`]+`)/SET FOREIGN_KEY_CHECKS=0;\n'.
122-
'TRUNCATE TABLE \1;\n'.
123-
'LOCK TABLES \1/g\''.
124-
' > '. $filename .
125-
'&& echo "SET FOREIGN_KEY_CHECKS=1;" >> '. $filename
77+
" -e 'SELECT * FROM $tableName' "
78+
. escapeshellarg($databaseInfo['database'])
79+
. " | sed -e 's/NULL/\\\\N/g'"
80+
. "> $filenamebase.tsv"
12681
);
82+
83+
$fd = fopen($filenamebase . ".sql", "w");
84+
fwrite($fd, "SET FOREIGN_KEY_CHECKS=0;\n");
85+
fwrite($fd, "TRUNCATE TABLE $tableName;\n");
86+
fwrite($fd, "LOCK TABLE $tableName WRITE;\n");
87+
fwrite($fd, "LOAD DATA LOCAL INFILE '$tableName.tsv' INTO TABLE $tableName\n");
88+
fwrite($fd, " IGNORE 1 LINES;\n");
89+
fwrite($fd, "SET FOREIGN_KEY_CHECKS=1;\n");
90+
fclose($fd);
12791
}

tools/raisinbread_refresh.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
$username = $dbInfo['adminUser'];
7575
$password = $dbInfo['adminPassword'];
7676

77+
chdir($baseConfigSetting . '/raisinbread/RB_files/');
78+
7779
if (! $config->getSetting('dev')['sandbox']) {
7880
fwrite(
7981
STDERR,
@@ -93,7 +95,7 @@
9395
die(printWarning('Input did not match database name. Exiting.'));
9496
}
9597

96-
$mysqlCommand = sprintf("mysql -A %s", escapeshellarg($dbname));
98+
$mysqlCommand = sprintf("mysql --local-infile -A %s", escapeshellarg($dbname));
9799

98100
echo 'Checking connection via MySQL configuration file...' . PHP_EOL;
99101
// Test whether a connection to MySQL is possible via a MySQL config file.
@@ -122,7 +124,7 @@
122124

123125
// Try connecting by supplying parameters on command line.
124126
$mysqlCommand = sprintf(
125-
"mysql -A %s -u %s -h %s -p%s",
127+
"mysql --local-infile -A %s -u %s -h %s -p%s",
126128
escapeshellarg($dbname),
127129
escapeshellarg($username),
128130
escapeshellarg($host),

0 commit comments

Comments
 (0)