bin scripts: Promisify db.init and db.close

pull/4656/head
Richard Hansen 2021-01-09 03:08:50 -05:00 committed by John McLear
parent 0a61767901
commit c622894fe0
1 changed files with 26 additions and 33 deletions

View File

@ -71,42 +71,35 @@ const unescape = (val) => {
if (!sqlFile) throw new Error('Use: node importSqlFile.js $SQLFILE'); if (!sqlFile) throw new Error('Use: node importSqlFile.js $SQLFILE');
log('initializing db'); log('initializing db');
db.init((err) => { await util.promisify(db.init.bind(db))();
// there was an error while initializing the database, output it and stop log('done');
if (err) {
throw err;
} else {
log('done');
log('open output file...'); log('open output file...');
const lines = fs.readFileSync(sqlFile, 'utf8').split('\n'); const lines = fs.readFileSync(sqlFile, 'utf8').split('\n');
const count = lines.length; const count = lines.length;
let keyNo = 0; let keyNo = 0;
process.stdout.write(`Start importing ${count} keys...\n`); process.stdout.write(`Start importing ${count} keys...\n`);
lines.forEach((l) => { lines.forEach((l) => {
if (l.substr(0, 27) === 'REPLACE INTO store VALUES (') { if (l.substr(0, 27) === 'REPLACE INTO store VALUES (') {
const pos = l.indexOf("', '"); const pos = l.indexOf("', '");
const key = l.substr(28, pos - 28); const key = l.substr(28, pos - 28);
let value = l.substr(pos + 3); let value = l.substr(pos + 3);
value = value.substr(0, value.length - 2); value = value.substr(0, value.length - 2);
console.log(`key: ${key} val: ${value}`); console.log(`key: ${key} val: ${value}`);
console.log(`unval: ${unescape(value)}`); console.log(`unval: ${unescape(value)}`);
db.set(key, unescape(value), null); db.set(key, unescape(value), null);
keyNo++; keyNo++;
if (keyNo % 1000 === 0) { if (keyNo % 1000 === 0) {
process.stdout.write(` ${keyNo}/${count}\n`); process.stdout.write(` ${keyNo}/${count}\n`);
} }
}
});
process.stdout.write('\n');
process.stdout.write('done. waiting for db to finish transaction. ' +
'depended on dbms this may take some time..\n');
db.close(() => {
log(`finished, imported ${keyNo} keys.`);
});
} }
}); });
process.stdout.write('\n');
process.stdout.write('done. waiting for db to finish transaction. ' +
'depended on dbms this may take some time..\n');
await util.promisify(db.close.bind(db))();
log(`finished, imported ${keyNo} keys.`);
})(); })();