From 35f4b1748e0cf72e36ba33e8b00fa0d3dbd6e4da Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Tue, 12 May 2020 18:02:11 +0200 Subject: [PATCH] Add import stats Signed-off-by: Alexandre Bodin --- .../lib/commands/configurationRestore.js | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/strapi/lib/commands/configurationRestore.js b/packages/strapi/lib/commands/configurationRestore.js index c7778732a4..706164bcac 100644 --- a/packages/strapi/lib/commands/configurationRestore.js +++ b/packages/strapi/lib/commands/configurationRestore.js @@ -32,8 +32,9 @@ module.exports = async function({ file: filePath, strategy = 'replace' }) { } console.log( - `Successfully imported ${dataToImport.length} configuration entries using the "${strategy}" strategy.` + `Successfully imported configuration with ${strategy} strategy. Statistics: ${importer.printStatistics()}.` ); + process.exit(0); }; @@ -78,12 +79,23 @@ const createImporter = (db, strategy) => { * @param {Object} db - DatabaseManager instance */ const createReplaceImporter = db => { + const stats = { + created: 0, + replaced: 0, + }; + return { + printStatistics() { + return `${stats.created} created, ${stats.replaced} replaced`; + }, + async import(conf) { const matching = await db.query('core_store').count({ key: conf.key }); if (matching > 0) { + stats.replaced += 1; await db.query('core_store').update({ key: conf.key }, conf); } else { + stats.created += 1; await db.query('core_store').create(conf); } }, @@ -95,12 +107,23 @@ const createReplaceImporter = db => { * @param {Object} db - DatabaseManager instance */ const createMergeImporter = db => { + const stats = { + created: 0, + merged: 0, + }; + return { + printStatistics() { + return `${stats.created} created, ${stats.merged} merged`; + }, + async import(conf) { const existingConf = await db.query('core_store').find({ key: conf.key }); if (existingConf) { + stats.merged += 1; await db.query('core_store').update({ key: conf.key }, _.merge(existingConf, conf)); } else { + stats.created += 1; await db.query('core_store').create(conf); } }, @@ -112,14 +135,25 @@ const createMergeImporter = db => { * @param {Object} db - DatabaseManager instance */ const createKeepImporter = db => { + const stats = { + created: 0, + untouched: 0, + }; + return { + printStatistics() { + return `${stats.created} created, ${stats.untouched} untouched`; + }, + async import(conf) { const matching = await db.query('core_store').count({ key: conf.key }); if (matching > 0) { + stats.untouched += 1; // if configuration already exists do not overwrite it return; } + stats.created += 1; await db.query('core_store').create(conf); }, };