You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.8 KiB

4 years ago
  1. const startTime = Date.now();
  2. require('ep_etherpad-lite/node_modules/npm').load({}, (er, npm) => {
  3. const fs = require('fs');
  4. const ueberDB = require('ep_etherpad-lite/node_modules/ueberdb2');
  5. const settings = require('ep_etherpad-lite/node/utils/Settings');
  6. const log4js = require('ep_etherpad-lite/node_modules/log4js');
  7. const dbWrapperSettings = {
  8. cache: 0,
  9. writeInterval: 100,
  10. json: false, // data is already json encoded
  11. };
  12. const db = new ueberDB.database(settings.dbType, settings.dbSettings, dbWrapperSettings, log4js.getLogger('ueberDB'));
  13. const sqlFile = process.argv[2];
  14. // stop if the settings file is not set
  15. if (!sqlFile) {
  16. console.error('Use: node importSqlFile.js $SQLFILE');
  17. process.exit(1);
  18. }
  19. log('initializing db');
  20. db.init((err) => {
  21. // there was an error while initializing the database, output it and stop
  22. if (err) {
  23. console.error('ERROR: Problem while initializing the database');
  24. console.error(err.stack ? err.stack : err);
  25. process.exit(1);
  26. } else {
  27. log('done');
  28. log('open output file...');
  29. const lines = fs.readFileSync(sqlFile, 'utf8').split('\n');
  30. const count = lines.length;
  31. let keyNo = 0;
  32. process.stdout.write(`Start importing ${count} keys...\n`);
  33. lines.forEach((l) => {
  34. if (l.substr(0, 27) == 'REPLACE INTO store VALUES (') {
  35. const pos = l.indexOf("', '");
  36. const key = l.substr(28, pos - 28);
  37. let value = l.substr(pos + 3);
  38. value = value.substr(0, value.length - 2);
  39. console.log(`key: ${key} val: ${value}`);
  40. console.log(`unval: ${unescape(value)}`);
  41. db.set(key, unescape(value), null);
  42. keyNo++;
  43. if (keyNo % 1000 == 0) {
  44. process.stdout.write(` ${keyNo}/${count}\n`);
  45. }
  46. }
  47. });
  48. process.stdout.write('\n');
  49. process.stdout.write('done. waiting for db to finish transaction. depended on dbms this may take some time...\n');
  50. db.doShutdown(() => {
  51. log(`finished, imported ${keyNo} keys.`);
  52. process.exit(0);
  53. });
  54. }
  55. });
  56. });
  57. function log(str) {
  58. console.log(`${(Date.now() - startTime) / 1000}\t${str}`);
  59. }
  60. unescape = function (val) {
  61. // value is a string
  62. if (val.substr(0, 1) == "'") {
  63. val = val.substr(0, val.length - 1).substr(1);
  64. return val.replace(/\\[0nrbtZ\\'"]/g, (s) => {
  65. switch (s) {
  66. case '\\0': return '\0';
  67. case '\\n': return '\n';
  68. case '\\r': return '\r';
  69. case '\\b': return '\b';
  70. case '\\t': return '\t';
  71. case '\\Z': return '\x1a';
  72. default: return s.substr(1);
  73. }
  74. });
  75. }
  76. // value is a boolean or NULL
  77. if (val == 'NULL') {
  78. return null;
  79. }
  80. if (val == 'true') {
  81. return true;
  82. }
  83. if (val == 'false') {
  84. return false;
  85. }
  86. // value is a number
  87. return val;
  88. };