pad.libre-service.eu-etherpad/bin/importSqlFile.ts

105 lines
2.9 KiB
TypeScript
Raw Normal View History

'use strict';
// As of v14, Node.js does not exit when there is an unhandled Promise rejection. Convert an
// unhandled rejection into an uncaught exception, which does cause Node.js to exit.
2024-03-15 20:59:28 +01:00
import util from "node:util";
2024-03-13 20:31:29 +01:00
const fs = require('fs');
import log4js from 'log4js';
import readline from 'readline';
2024-08-08 21:23:10 +02:00
import {Database} from "ueberdb2";
import process from "node:process";
2024-03-13 20:31:29 +01:00
const settings = require('ep_etherpad-lite/node/utils/Settings');
process.on('unhandledRejection', (err) => { throw err; });
const startTime = Date.now();
2024-03-13 20:31:29 +01:00
const log = (str:string) => {
console.log(`${(Date.now() - startTime) / 1000}\t${str}`);
};
2024-03-13 20:31:29 +01:00
const unescape = (val: string) => {
// value is a string
2024-03-13 20:31:29 +01:00
if (val.substring(0, 1) === "'") {
val = val.substring(0, val.length - 1).substring(1);
return val.replace(/\\[0nrbtZ\\'"]/g, (s) => {
switch (s) {
case '\\0': return '\0';
case '\\n': return '\n';
case '\\r': return '\r';
case '\\b': return '\b';
case '\\t': return '\t';
case '\\Z': return '\x1a';
2024-03-13 20:31:29 +01:00
default: return s.substring(1);
}
});
}
// value is a boolean or NULL
if (val === 'NULL') {
return null;
}
if (val === 'true') {
return true;
}
if (val === 'false') {
return false;
}
// value is a number
return val;
};
2021-01-09 08:44:59 +01:00
(async () => {
const dbWrapperSettings = {
cache: 0,
writeInterval: 100,
json: false, // data is already json encoded
};
2024-08-08 21:23:10 +02:00
const db = new Database( // eslint-disable-line new-cap
settings.dbType,
settings.dbSettings,
dbWrapperSettings,
log4js.getLogger('ueberDB'));
2013-03-06 22:08:14 +01:00
const sqlFile = process.argv[2];
2013-03-06 22:08:14 +01:00
// stop if the settings file is not set
if (!sqlFile) throw new Error('Use: node importSqlFile.js $SQLFILE');
2013-03-06 22:08:14 +01:00
log('initializing db');
2024-03-13 20:31:29 +01:00
const initDb = await util.promisify(db.init.bind(db));
await initDb(null);
log('done');
log(`Opening ${sqlFile}...`);
const stream = fs.createReadStream(sqlFile, {encoding: 'utf8'});
log(`Reading ${sqlFile}...`);
let keyNo = 0;
for await (const l of readline.createInterface({input: stream, crlfDelay: Infinity})) {
2024-03-13 20:31:29 +01:00
if (l.substring(0, 27) === 'REPLACE INTO store VALUES (') {
const pos = l.indexOf("', '");
2024-03-13 20:31:29 +01:00
const key = l.substring(28, pos - 28);
let value = l.substring(pos + 3);
value = value.substring(0, value.length - 2);
console.log(`key: ${key} val: ${value}`);
console.log(`unval: ${unescape(value)}`);
2024-03-13 20:31:29 +01:00
// @ts-ignore
db.set(key, unescape(value), null);
keyNo++;
if (keyNo % 1000 === 0) log(` ${keyNo}`);
}
}
process.stdout.write('\n');
process.stdout.write('done. waiting for db to finish transaction. ' +
'depended on dbms this may take some time..\n');
2024-03-13 20:31:29 +01:00
const closeDB = util.promisify(db.close.bind(db));
2024-08-08 21:23:10 +02:00
// @ts-ignore
2024-03-13 20:31:29 +01:00
await closeDB(null);
log(`finished, imported ${keyNo} keys.`);
2024-08-08 21:23:10 +02:00
process.exit(0)
2021-01-09 08:44:59 +01:00
})();