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
+84 -14
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,14 +101,50 @@ function delete_prompt(cb) {
return cb(false);
}
});
} else {
// skip the delete prompt
return cb(true);
}
});
}
function preserve_claimaddress_prompt(cb) {
const ClaimAddress = require('../models/claimaddress');
// 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?');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// 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();
// 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');
}
return cb();
});
} else
return cb();
});
}
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
@@ -113,6 +162,9 @@ delete_prompt(function(continue_process) {
// 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');
@@ -125,6 +177,11 @@ delete_prompt(function(continue_process) {
// 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,6 +190,8 @@ delete_prompt(function(continue_process) {
// loop through all collections
collections.forEach((collection) => {
// 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
@@ -148,6 +207,17 @@ delete_prompt(function(continue_process) {
exit(mongoose, 0);
}
});
} else {
// skipped deleting of the claimaddresses collection
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
@@ -160,6 +230,11 @@ delete_prompt(function(continue_process) {
console.log('Error: Unable to list collections in database: %s', err);
exit(mongoose, 1);
});
} 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);
@@ -174,8 +249,3 @@ delete_prompt(function(continue_process) {
console.log("Delete aborted");
exit(null, 2);
}
} else {
console.log('Process aborted. Nothing was deleted.');
exit(null, 2);
}
});
+5 -46
View File
@@ -1116,41 +1116,6 @@ function block_sync(reindex, stats) {
});
}
function check_reindex_prompt(cb) {
// check if this is a reindex
if (mode == 'reindex') {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('You are about to delete all blockchain data (transactions and addresses)');
console.log('and resync from the genesis block.');
// prompt for reindexing the database
rl.question('Are you sure you want to do this? [y/n]: ', function (reindexAnswer) {
// stop prompting
rl.close();
// determine if the reindex should proceed
switch (reindexAnswer) {
case 'y':
case 'Y':
case 'yes':
case 'YES':
case 'Yes':
return cb(true);
break;
default:
return cb(false);
}
});
} else {
return cb(true);
}
}
// check options
if (process.argv[2] == null || process.argv[2] == 'index' || process.argv[2] == 'update') {
mode = null;
@@ -1196,10 +1161,6 @@ else if (process.argv[2] == 'market')
else
usage();
// check if this is a reindex which requires a prompt before continuing
check_reindex_prompt(function(reindexAnswer) {
// check if the process should continue
if (reindexAnswer) {
// check if this sync option is already running/locked
if (lib.is_locked([database]) == false) {
// create a new sync lock before checking the rest of the locks to minimize problems with running scripts at the same time
@@ -1233,10 +1194,13 @@ check_reindex_prompt(function(reindexAnswer) {
if (mode == 'reindex') {
const { execSync } = require('child_process');
console.log('Deleting database.. Please wait..');
try {
// delete the database
execSync(`node ./scripts/delete_database.js ${mode}`, {stdio : 'inherit'});
} catch (err) {
// delete_database.js was not successful, so exit
exit(1);
}
db.update_db(settings.coin.name, function(stats) {
// check if stats returned properly
@@ -1656,8 +1620,3 @@ check_reindex_prompt(function(reindexAnswer) {
console.log("Sync aborted");
exit(2);
}
} else {
console.log('Process aborted. Nothing was deleted');
exit(2);
}
});