ソースを参照

migrate(Reports): migrated from reports v2 to v4 following recent changes

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 3 年 前
コミット
9aa1077e41
1 ファイル変更67 行追加0 行削除
  1. 67 0
      backend/logic/migration/migrations/migration12.js

+ 67 - 0
backend/logic/migration/migrations/migration12.js

@@ -0,0 +1,67 @@
+import async from "async";
+
+/**
+ * Migration 12
+ *
+ * Migration for updated style of reports
+ *
+ * @param {object} MigrationModule - the MigrationModule
+ * @returns {Promise} - returns promise
+ */
+export default async function migrate(MigrationModule) {
+	const reportModel = await MigrationModule.runJob("GET_MODEL", { modelName: "report" }, this);
+
+	return new Promise((resolve, reject) => {
+		async.waterfall(
+			[
+				next => {
+					this.log("INFO", `Migration 12. Finding reports with document version 2.`);
+					reportModel.find({ documentVersion: 2 }, (err, reports) => {
+						if (err) next(err);
+						else {
+							async.eachLimit(
+								reports.map(reporti => reporti._doc),
+								1,
+								(reporti, next) => {
+									const issues = [];
+
+									if (reporti.description !== "")
+										issues.push({ category: "custom", info: reporti.description });
+
+									reporti.issues.forEach(category =>
+										category.reasons.forEach(info => issues.push({ category: category.name, info }))
+									);
+
+									reportModel.updateOne(
+										{ _id: reporti._id },
+										{
+											$set: {
+												documentVersion: 4,
+												issues
+											},
+											$unset: {
+												description: ""
+											}
+										},
+										next
+									);
+								},
+								err => {
+									if (err) next(err);
+									else {
+										this.log("INFO", `Migration 12. Reports found: ${reports.length}.`);
+										next();
+									}
+								}
+							);
+						}
+					});
+				}
+			],
+			err => {
+				if (err) reject(new Error(err));
+				else resolve();
+			}
+		);
+	});
+}