Reindex and delete-database script improvements

-The delete-database script now checks the claimaddresses collection for data and if at least 1 record exists, it will ask an additional question to see if you want to preserve that data or delete everything
-The delete-database script now issues the prompt for the reindex with a slightly different msg than the delete without reindex
-The prompt for deleting now happens after the locks have been properly checked
This commit is contained in:
Joe Uhren
2023-10-01 19:10:52 -06:00
parent 3dca953980
commit 7349560bfa
2 changed files with 528 additions and 499 deletions
+124 -54
View File
@@ -2,6 +2,7 @@ const lib = require('../lib/explorer');
const readline = require('readline');
const deleteLockName = 'delete';
var lockCreated = false;
var preserveClaimAddressNames = false;
// exit function used to cleanup lock before finishing script
function exit(mongoose, exitCode) {
@@ -61,14 +62,26 @@ function drop_collection(mongoose, colName, cb) {
}
function delete_prompt(cb) {
// Check if the delete prompt should be skipped
if (process.argv[2] == null || process.argv[2] != 'reindex') {
preserve_claimaddress_prompt(function() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('You are about to delete the entire eIquidus database.');
// Change the delete prompt based on whether this is a reindex or regular delete
if (process.argv[2] != null && process.argv[2] == 'reindex') {
console.log('You are about to delete all data from the entire eIquidus database');
if (preserveClaimAddressNames)
console.log('(claim address name data will not be deleted)');
console.log('and resync from the genesis block.');
} else {
console.log('You are about to delete all data from the entire eIquidus database.');
if (preserveClaimAddressNames)
console.log('(claim address name data will not be deleted)');
}
// prompt for deleting explorer database
rl.question('Are you sure you want to do this? [y/n]: ', function (deleteAnswer) {
@@ -88,43 +101,87 @@ function delete_prompt(cb) {
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;
function preserve_claimaddress_prompt(cb) {
const ClaimAddress = require('../models/claimaddress');
var lock_list = ['backup', 'restore', 'markets', 'peers', 'masternodes'];
// check how many claim address records there are
ClaimAddress.find({}).countDocuments().then((count) => {
// display an additional prompt in the event the claimaddress collection has data
if (count > 0) {
console.log(`The current database has ${count} custom claim address names`);
console.log('Would you like to preserve this data?');
// 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');
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// 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);
// prompt for deleting claim address data
rl.question('y = keep claim address data, n = delete claim address data [y/n]: ', function (preserveClaimAddresses) {
// stop prompting
rl.close();
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}`;
// determine if the claim address data should be preserved
switch (preserveClaimAddresses) {
case 'y':
case 'Y':
case 'yes':
case 'YES':
case 'Yes':
preserveClaimAddressNames = true;
console.log('Claim address name data will be saved' + '\n');
break;
default:
console.log('Claim address name data will be deleted' + '\n');
}
console.log('Connecting to database..');
return cb();
});
} else
return cb();
});
}
mongoose.set('strictQuery', true);
// 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;
// connect to mongo database
mongoose.connect(dbString).then(() => {
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
// suppress the pid message when doing a reindex
if (process.argv[2] == null || process.argv[2] != 'reindex')
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(() => {
console.log('Database connection successful' + '\n');
// prompt for database delete
delete_prompt(function(continue_process) {
if (continue_process) {
// get the list of collections
mongoose.connection.db.listCollections().toArray().then((collections) => {
// check if there are any collections
@@ -133,13 +190,26 @@ delete_prompt(function(continue_process) {
// loop through all collections
collections.forEach((collection) => {
console.log(`Deleting ${collection.name}..`);
// check if this is the claim addres collection and that data is being preserved
if (!preserveClaimAddressNames || collection.name != 'claimaddresses') {
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++;
// 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 {
// skipped deleting of the claimaddresses collection
counter++;
// check if the last collection was deleted
if (counter == collections.length) {
@@ -147,7 +217,7 @@ delete_prompt(function(continue_process) {
console.log('Finished deleting database');
exit(mongoose, 0);
}
});
}
});
} else {
// nothing to delete
@@ -160,22 +230,22 @@ delete_prompt(function(continue_process) {
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);
}
});
}).catch((err) => {
console.log('Error: Unable to connect to database: %s', err);
exit(mongoose, 999);
});
} else {
console.log('Process aborted. Nothing was deleted.');
// 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);
}