2021-01-18 09:53:15 +01:00
|
|
|
'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');
|
|
|
|
|
2021-01-18 09:53:15 +01:00
|
|
|
// 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 () => {
|
2015-11-22 08:15:46 +01:00
|
|
|
// 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.
|
|
|
|
|
2018-11-27 13:32:07 +01:00
|
|
|
// It might be necessary to run the script using more memory:
|
2021-02-05 00:43:27 +01:00
|
|
|
// `node --max-old-space-size=4096 src/bin/migrateDirtyDBtoRealDB.js`
|
2018-11-27 13:32:07 +01:00
|
|
|
|
2021-02-03 13:08:43 +01:00
|
|
|
|
2020-11-23 19:21:51 +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
|
2015-11-22 08:15:46 +01:00
|
|
|
};
|
2024-08-08 21:23:10 +02:00
|
|
|
const db = new Database( // eslint-disable-line new-cap
|
2021-01-18 09:53:15 +01:00
|
|
|
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('*', '')
|
2021-01-18 09:53:15 +01:00
|
|
|
|
2024-03-13 20:31:29 +01:00
|
|
|
console.log(`Found ${keys.length} records, processing now.`);
|
|
|
|
const p: Promise<void>[] = [];
|
2021-01-18 09:53:15 +01:00
|
|
|
let numWritten = 0;
|
2024-03-13 20:31:29 +01:00
|
|
|
for (const key of keys) {
|
|
|
|
let value = await dirty.get(key);
|
2021-01-18 09:53:15 +01:00
|
|
|
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) => {
|
2021-01-18 09:53:15 +01:00
|
|
|
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
|
|
|
}
|
2021-01-18 09:53:15 +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);
|
2021-01-18 09:53:15 +01:00
|
|
|
console.log('Finished.');
|
2024-08-08 21:23:10 +02:00
|
|
|
process.exit(0)
|
2021-01-09 08:44:59 +01:00
|
|
|
})();
|