|
| 1 | +const pluginManager = require('../../../../plugins/pluginManager.js'); |
| 2 | + |
| 3 | +/********************************************/ |
| 4 | +/* 1) With this script, if there is any cooldown defined before in the APPS collection(in the plugins.content), it will be moved to the APPS collection in the plugins.journey_engine */ |
| 5 | +/* 2) Also, if there is any cooldown defined before in the PLUGINS collection(in the plugins.content), it will be moved to the PLUGINS collection in the plugins.journey_engine */ |
| 6 | +/********************************************/ |
| 7 | + |
| 8 | +console.log("=== Migrating 'content' cooldown to 'journeys' in apps and plugins collections ==="); |
| 9 | + |
| 10 | +pluginManager.dbConnection().then(async(countlyDb) => { |
| 11 | + try { |
| 12 | + /********************************************/ |
| 13 | + /* 1) in the APPS collection content->journey_engine, APP SETTING */ |
| 14 | + /********************************************/ |
| 15 | + console.log("Starting migration in apps collection..."); |
| 16 | + |
| 17 | + const apps = await countlyDb.collection("apps").find({"plugins.content": { $exists: true }}).toArray(); |
| 18 | + if (!apps || apps.length === 0) { |
| 19 | + console.log("No apps require content->journeys migration"); |
| 20 | + } |
| 21 | + else { |
| 22 | + console.log(`Found ${apps.length} apps to migrate`); |
| 23 | + for (let appDoc of apps) { |
| 24 | + appDoc.plugins.journey_engine = appDoc.plugins.content; |
| 25 | + delete appDoc.plugins.content; |
| 26 | + |
| 27 | + await countlyDb.collection("apps").updateOne( |
| 28 | + { _id: appDoc._id }, |
| 29 | + { $set: { plugins: appDoc.plugins }} |
| 30 | + ); |
| 31 | + } |
| 32 | + console.log("Apps content->journeys migration DONE"); |
| 33 | + } |
| 34 | + |
| 35 | + /********************************************/ |
| 36 | + /* 2) in the PLUGINS collection update the document which _id: "plugins, GLOBAL SETTING"*/ |
| 37 | + /********************************************/ |
| 38 | + console.log("Updating '_id: plugins' document in plugins collection..."); |
| 39 | + |
| 40 | + const pluginsDoc = await countlyDb.collection("plugins").findOne({ _id: "plugins" }); |
| 41 | + if (!pluginsDoc) { |
| 42 | + console.log("No document found in plugins collection with _id: plugins"); |
| 43 | + } |
| 44 | + else { |
| 45 | + let needUpdate = false; |
| 46 | + |
| 47 | + // 2.a) Remove "plugins.content" boolean field |
| 48 | + if (pluginsDoc.plugins && Object.prototype.hasOwnProperty.call(pluginsDoc.plugins, "content")) { |
| 49 | + delete pluginsDoc.plugins.content; |
| 50 | + needUpdate = true; |
| 51 | + console.log("Removed plugins.content boolean field."); |
| 52 | + } |
| 53 | + |
| 54 | + // 2.b) Move content.cooldown to journey_engine.cooldown |
| 55 | + if (pluginsDoc.content && typeof pluginsDoc.content === 'object') { |
| 56 | + if (typeof pluginsDoc.content.cooldown !== 'undefined') { |
| 57 | + if (!pluginsDoc.journey_engine || typeof pluginsDoc.journey_engine !== 'object') { |
| 58 | + pluginsDoc.journey_engine = {}; |
| 59 | + } |
| 60 | + pluginsDoc.journey_engine.cooldown = pluginsDoc.content.cooldown; |
| 61 | + console.log(`Copied content.cooldown = ${pluginsDoc.content.cooldown} to journey_engine.cooldown`); |
| 62 | + } |
| 63 | + |
| 64 | + // remove the top-level "content" object |
| 65 | + delete pluginsDoc.content; |
| 66 | + needUpdate = true; |
| 67 | + console.log("Removed top-level content object."); |
| 68 | + } |
| 69 | + |
| 70 | + // if any changes were made, update the document |
| 71 | + if (needUpdate) { |
| 72 | + await countlyDb.collection("plugins").updateOne( |
| 73 | + { _id: "plugins" }, |
| 74 | + { $set: pluginsDoc } |
| 75 | + ); |
| 76 | + console.log("plugins document updated successfully"); |
| 77 | + } |
| 78 | + else { |
| 79 | + console.log("No changes required for _id: plugins document"); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + catch (err) { |
| 84 | + console.error("Error during cooldown setting migration:", err); |
| 85 | + } |
| 86 | + finally { |
| 87 | + countlyDb.close(); |
| 88 | + console.log("Cooldown migration script finished, DB connection closed."); |
| 89 | + } |
| 90 | +}); |
0 commit comments