Fix migration error handling and add data validation

This commit is contained in:
Gabriel Pastori 2023-12-20 00:11:51 -03:00
parent c6b743a9b8
commit 2d54cec268
7 changed files with 85 additions and 44 deletions

View File

@ -83,9 +83,17 @@ module.exports = async () => {
}
];
// get folders from /instances
const answers = await inquirer.prompt(questions);
// check if there is any data to migrate
if (answers.saveData.length === 0) {
console.log('🌱 No data selected to migrate!\n\n');
process.exit(0);
}
if (answers.selectedInstances.length === 0) {
console.log('🌱 No instances selected to migrate!\n\n');
process.exit(0);
}
// connect to mongodb
console.log('\n\n🌱 Connecting to MongoDB...');
@ -102,6 +110,7 @@ module.exports = async () => {
}, cliProgress.Presets.shades_classic);
instancesBar.start(answers.selectedInstances.length, 0);
try {
for (const instanceName of answers.selectedInstances) {
instancesBar.update({ instanceName });
@ -117,12 +126,19 @@ module.exports = async () => {
instanceBars.stop();
instancesBar.increment();
}
} catch (err) {
instancesBar.stop();
console.log(err.file);
console.log(err.err);
process.exit(1);
}
instancesBar.stop();
// disconnect from mongodb
console.log('\n\n🌱 Disconnecting from MongoDB...');
await conn.close();
if (connInstance) await connInstance.close();
console.log('🌱 Disconnected from MongoDB!\n\n');
console.log('🌱 Migration completed!\n\n');

View File

@ -16,6 +16,7 @@ module.exports = async (instanceName, options, progressBars, conn) => {
const progress = progressBars.create(files.length, 0)
progress.update({ process: 'Chats' })
for (const file of files) {
try {
const collectionName = file.key
const collection = conn.collection(collectionName)
@ -24,6 +25,10 @@ module.exports = async (instanceName, options, progressBars, conn) => {
await collection.findOneAndUpdate({ _id: data._id }, { $set: data }, { upsert: true })
progress.increment()
} catch (err) {
progress.stop()
throw { err, file }
}
}
}

View File

@ -16,6 +16,7 @@ module.exports = async (instanceName, options, progressBars, conn) => {
const progress = progressBars.create(files.length, 0)
progress.update({ process: 'Contacts' })
for (const file of files) {
try {
const collectionName = file.key
const collection = conn.collection(collectionName)
@ -24,6 +25,10 @@ module.exports = async (instanceName, options, progressBars, conn) => {
await collection.findOneAndUpdate({ _id: data._id }, { $set: data }, { upsert: true })
progress.increment()
} catch (err) {
progress.stop()
throw { err, file }
}
}
}

View File

@ -30,6 +30,7 @@ module.exports = async (instanceName, options, progressBars, conn, connInstance)
const progress = progressBars.create(files.length, 0)
progress.update({ process: 'Instance' })
for (const file of files) {
try {
const collectionName = file.key || instanceName
const collection = (!file.secondaryConnection ? conn : connInstance).collection(collectionName)
@ -37,6 +38,10 @@ module.exports = async (instanceName, options, progressBars, conn, connInstance)
data._id = file.path.split('\\').pop().split('.')[0]
await collection.findOneAndUpdate({ _id: data._id }, { $set: data }, { upsert: true })
progress.increment()
} catch (err) {
progress.stop()
throw { err, file }
}
}
}

View File

@ -16,6 +16,7 @@ module.exports = async (instanceName, options, progressBars, conn) => {
const progress = progressBars.create(files.length, 0)
progress.update({ process: 'Message Update' })
for (const file of files) {
try {
const collectionName = file.key
const collection = conn.collection(collectionName)
@ -24,6 +25,10 @@ module.exports = async (instanceName, options, progressBars, conn) => {
await collection.findOneAndUpdate({ _id: data._id }, { $set: data }, { upsert: true })
progress.increment()
} catch (err) {
progress.stop()
throw { err, file }
}
}
}

View File

@ -16,6 +16,7 @@ module.exports = async (instanceName, options, progressBars, conn) => {
const progress = progressBars.create(files.length, 0)
progress.update({ process: 'Messages' })
for (const file of files) {
try {
const collectionName = file.key
const collection = conn.collection(collectionName)
@ -24,6 +25,10 @@ module.exports = async (instanceName, options, progressBars, conn) => {
await collection.findOneAndUpdate({ _id: data._id }, { $set: data }, { upsert: true })
progress.increment()
} catch (err) {
progress.stop()
throw { err, file }
}
}
}

View File

@ -1,7 +1,7 @@
{
"name": "evolution-manager",
"description": "Evolution Manager is an open-source interface for managing the Evolution API, simplifying the creation and administration of API instances with advanced features and diverse integrations.",
"version": "0.4.8",
"version": "0.4.9",
"main": "dist",
"engines": {
"node": ">=16.0.0"