|
67 | 67 | die(); |
68 | 68 | } |
69 | 69 |
|
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 | | - |
103 | 70 | // Loop through all tables to generate insert statements for each. |
104 | 71 | foreach ($tableNames as $tableName) { |
105 | 72 | $paths = \NDB_Config::singleton()->getSetting('paths'); |
106 | | - $filename = $paths['base'] . "/raisinbread/RB_files/RB_$tableName.sql"; |
| 73 | + $filenamebase = $paths['base'] . "/raisinbread/RB_files/$tableName"; |
107 | 74 | exec( |
108 | | - 'mysqldump -u '.escapeshellarg($adminUser). |
| 75 | + 'mysql --raw -u '.escapeshellarg($adminUser). |
109 | 76 | ' -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" |
126 | 81 | ); |
| 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); |
127 | 91 | } |
0 commit comments