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

62 lines
2.1 KiB
TypeScript
Raw Normal View History

'use strict';
2024-03-13 20:31:29 +01:00
import process from 'node:process';
2024-08-08 21:23:10 +02:00
import {Database} from "ueberdb2";
2024-03-13 20:31:29 +01:00
import log4js from 'log4js';
import util from 'util';
const settings = require('ep_etherpad-lite/node/utils/Settings');
// 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.
process.on('unhandledRejection', (err) => { throw err; });
2021-01-09 08:44:59 +01:00
(async () => {
// This script requires that you have modified your settings.json file
// to work with a real database. Please make a backup of your dirty.db
// file before using this script, just to be safe.
// It might be necessary to run the script using more memory:
// `node --max-old-space-size=4096 src/bin/migrateDirtyDBtoRealDB.js`
2021-02-03 13:08:43 +01:00
const dbWrapperSettings = {
cache: '0', // The cache slows things down when you're mostly writing.
writeInterval: 0, // Write directly to the database, don't buffer
};
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'));
await db.init();
console.log('Waiting for dirtyDB to parse its file.');
2024-08-08 21:23:10 +02:00
const dirty = new Database('dirty', `${__dirname}/../var/dirty.db`);
2024-03-13 20:31:29 +01:00
await dirty.init();
const keys = await dirty.findKeys('*', '')
2024-03-13 20:31:29 +01:00
console.log(`Found ${keys.length} records, processing now.`);
const p: Promise<void>[] = [];
let numWritten = 0;
2024-03-13 20:31:29 +01:00
for (const key of keys) {
let value = await dirty.get(key);
let bcb, wcb;
p.push(new Promise((resolve, reject) => {
2024-03-13 20:31:29 +01:00
bcb = (err:any) => { if (err != null) return reject(err); };
wcb = (err:any) => {
if (err != null) return reject(err);
if (++numWritten % 100 === 0) console.log(`Wrote record ${numWritten} of ${length}`);
resolve();
};
}));
db.set(key, value, bcb, wcb);
2024-03-13 20:31:29 +01:00
}
await Promise.all(p);
console.log(`Wrote all ${numWritten} records`);
2024-03-13 20:31:29 +01:00
await db.close(null);
await dirty.close(null);
console.log('Finished.');
2024-08-08 21:23:10 +02:00
process.exit(0)
2021-01-09 08:44:59 +01:00
})();