|  | 
|  | 1 | +/* eslint-disable @typescript-eslint/no-require-imports */ | 
|  | 2 | +const convertObsoleteHistoryToGenericHistory = | 
|  | 3 | +  require("../dist/datasets/utils/history.util").convertObsoleteHistoryToGenericHistory; | 
|  | 4 | + | 
|  | 5 | +const convertGenericHistoriesToObsoleteHistories = | 
|  | 6 | +  require("../dist/datasets/utils/history.util").convertGenericHistoriesToObsoleteHistories; | 
|  | 7 | + | 
|  | 8 | +module.exports = { | 
|  | 9 | +  /** | 
|  | 10 | +   * @param db {import('mongodb').Db} | 
|  | 11 | +   * @param client {import('mongodb').MongoClient} | 
|  | 12 | +   * @returns {Promise<void>} | 
|  | 13 | +   */ | 
|  | 14 | +  async up(db, client) { | 
|  | 15 | +    for await (const dataset of db | 
|  | 16 | +      .collection("Dataset") | 
|  | 17 | +      .find({ history: { $exists: true, $type: "array", $ne: [] } })) { | 
|  | 18 | +      console.log( | 
|  | 19 | +        `Migrating history for dataset ${dataset._id}. Entries: ${dataset.history.length}`, | 
|  | 20 | +      ); | 
|  | 21 | +      const genericHistories = dataset.history.map((entry) => | 
|  | 22 | +        convertObsoleteHistoryToGenericHistory(entry, dataset._id), | 
|  | 23 | +      ); | 
|  | 24 | +      result = await db.collection("History").insertMany(genericHistories); | 
|  | 25 | +    } | 
|  | 26 | +    await db.collection("Dataset").updateMany({}, { $unset: { history: "" } }); | 
|  | 27 | +  }, | 
|  | 28 | + | 
|  | 29 | +  /** | 
|  | 30 | +   * @param db {import('mongodb').Db} | 
|  | 31 | +   * @param client {import('mongodb').MongoClient} | 
|  | 32 | +   * @returns {Promise<void>} | 
|  | 33 | +   */ | 
|  | 34 | +  async down(db, client) { | 
|  | 35 | +    for await (const dataset of db | 
|  | 36 | +      .collection("Dataset") | 
|  | 37 | +      .find({ history: { $exists: false } })) { | 
|  | 38 | +      console.log(`Rolling back history for dataset ${dataset._id}`); | 
|  | 39 | +      const genericHistories = await db | 
|  | 40 | +        .collection("History") | 
|  | 41 | +        .find( | 
|  | 42 | +          { documentId: dataset._id, subsystem: "Dataset" }, | 
|  | 43 | +          { sort: { timestamp: "desc" } }, | 
|  | 44 | +        ) | 
|  | 45 | +        .toArray(); | 
|  | 46 | +      console.log( | 
|  | 47 | +        `Found ${genericHistories.length} history entries for dataset ${dataset._id}`, | 
|  | 48 | +      ); | 
|  | 49 | +      dataset.$clone = () => dataset; // Mock the $clone method, as this is not a mongoose document | 
|  | 50 | +      const obsoleteHistories = convertGenericHistoriesToObsoleteHistories( | 
|  | 51 | +        genericHistories, | 
|  | 52 | +        dataset, | 
|  | 53 | +      ); | 
|  | 54 | +      console.log( | 
|  | 55 | +        `Inserting obsolete history entries for dataset ${dataset._id}`, | 
|  | 56 | +      ); | 
|  | 57 | +      await db | 
|  | 58 | +        .collection("Dataset") | 
|  | 59 | +        .updateOne( | 
|  | 60 | +          { _id: dataset._id }, | 
|  | 61 | +          { $set: { history: obsoleteHistories } }, | 
|  | 62 | +        ); | 
|  | 63 | +    } | 
|  | 64 | +  }, | 
|  | 65 | +}; | 
0 commit comments