Files
purple-explorer/scripts/delete_database.js
T
Joe Uhren 119cf3e2c6 Reindex improvements
-Reindex now calls the delete-databse.js script which is much faster and more complete than the previous manual delete code the resync used to use
-The delete-database.js script now accepts a parameter that suppresses the delete prompt when being called from the resync process
-Reindex and normal block sync now reuse the same block sync code
-Reworked the sync script to no longer require the readline-sync dependency
-The readline-sync dependency has been completely removed from package.json since it is no longer used
2023-09-23 17:39:50 -06:00

181 lines
5.7 KiB
JavaScript

const lib = require('../lib/explorer');
const readline = require('readline');
const deleteLockName = 'delete';
var lockCreated = false;
// exit function used to cleanup lock before finishing script
function exit(mongoose, exitCode) {
const db = require('../lib/database');
// check if mongo was connected
if (mongoose != null) {
// check if this is a clean exit
if (exitCode == 0) {
// initialize the database
db.initialize_data_startup(function() {
// disconnect mongo connection
mongoose.disconnect();
// finish exit cleanup
finishExit(db, exitCode);
});
} else {
// disconnect mongo connection
mongoose.disconnect();
// finish exit cleanup
finishExit(db, exitCode);
}
} else {
// finish exit cleanup
finishExit(db, exitCode);
}
}
function finishExit(db, exitCode) {
// always check for and remove the sync msg if exists
db.remove_sync_message();
// only remove delete lock if it was created in this session
if (!lockCreated || lib.remove_lock(deleteLockName) == true) {
// clean exit with previous exit code
process.exit(exitCode);
} else {
// error removing lock
process.exit(1);
}
}
function drop_collection(mongoose, colName, cb) {
// attempt to delete the collection
mongoose.connection.db.dropCollection(colName).then((result) => {
if (!result) {
console.log(`Error: Unable to delete the ${colName} collection`);
exit(mongoose, 1);
} else
return cb(true);
}).catch((err) => {
console.log(err);
exit(mongoose, 1);
});
}
function delete_prompt(cb) {
// Check if the delete prompt should be skipped
if (process.argv[2] == null || process.argv[2] != 'reindex') {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('You are about to delete the entire eIquidus database.');
// prompt for deleting explorer database
rl.question('Are you sure you want to do this? [y/n]: ', function (deleteAnswer) {
// stop prompting
rl.close();
// determine if the explorer database should be deleted
switch (deleteAnswer) {
case 'y':
case 'Y':
case 'yes':
case 'YES':
case 'Yes':
return cb(true);
break;
default:
return cb(false);
}
});
} else {
// skip the delete prompt
return cb(true);
}
}
delete_prompt(function(continue_process) {
if (continue_process) {
// check if the "delete database" process is already running
if (lib.is_locked([deleteLockName]) == false) {
// create a new delete lock before checking the rest of the locks to minimize problems with running scripts at the same time
lib.create_lock(deleteLockName);
// ensure the lock will be deleted on exit
lockCreated = true;
var lock_list = ['backup', 'restore', 'markets', 'peers', 'masternodes'];
// do not check the index lock if this is called from the reindex process
if (process.argv[2] == null || process.argv[2] != 'reindex') {
lock_list.push('index');
}
// check all other possible locks since database deletion should not run at the same time that data is being changed
if (lib.is_locked(lock_list) == false) {
// all tests passed. OK to run delete
console.log("Script launched with pid: " + process.pid);
const settings = require('../lib/settings');
const mongoose = require('mongoose');
const dbString = `mongodb://${encodeURIComponent(settings.dbsettings.user)}:${encodeURIComponent(settings.dbsettings.password)}@${settings.dbsettings.address}:${settings.dbsettings.port}/${settings.dbsettings.database}`;
console.log('Connecting to database..');
mongoose.set('strictQuery', true);
// connect to mongo database
mongoose.connect(dbString).then(() => {
// get the list of collections
mongoose.connection.db.listCollections().toArray().then((collections) => {
// check if there are any collections
if (collections.length > 0) {
var counter = 0;
// loop through all collections
collections.forEach((collection) => {
console.log(`Deleting ${collection.name}..`);
// delete this collection
drop_collection(mongoose, collection.name, function(retVal) {
// check if the collection was successfully deleted
if (retVal)
counter++;
// check if the last collection was deleted
if (counter == collections.length) {
// finish the delete process
console.log('Finished deleting database');
exit(mongoose, 0);
}
});
});
} else {
// nothing to delete
console.log('Nothing to delete, the database is already empty..');
// finish the delete process
exit(mongoose, 0);
}
}).catch((err) => {
console.log('Error: Unable to list collections in database: %s', err);
exit(mongoose, 1);
});
}).catch((err) => {
console.log('Error: Unable to connect to database: %s', err);
exit(mongoose, 999);
});
} else {
// another script process is currently running
console.log("Delete aborted");
exit(null, 2);
}
} else {
// delete process is already running
console.log("Delete aborted");
exit(null, 2);
}
} else {
console.log('Process aborted. Nothing was deleted.');
exit(null, 2);
}
});