Taichi Masuyama 4 лет назад
Родитель
Сommit
bf6b3d86c4

+ 20 - 6
packages/app/src/migrations/20220131001218-convert-redirect-to-pages-to-page-redirect-documents.js

@@ -14,10 +14,10 @@ const BATCH_SIZE = 100;
 module.exports = {
   async up(db, client) {
     mongoose.connect(getMongoUri(), mongoOptions);
-    const Page = getModelSafely('Page') || getPageModel();
+    const pageCollection = await db.collection('pages');
     const PageRedirect = getModelSafely('PageRedirect') || PageRedirectModel;
 
-    const cursor = Page.find({ redirectTo: { $exists: true, $ne: null } }, { path: 1, redirectTo: 1, _id: 0 }).lean().cursor();
+    const cursor = pageCollection.find({ redirectTo: { $exists: true, $ne: null } }, { path: 1, redirectTo: 1, _id: 0 }).stream();
     const batchStream = createBatchStream(BATCH_SIZE);
 
     // redirectTo => PageRedirect
@@ -33,17 +33,24 @@ module.exports = {
         };
       });
 
-      await PageRedirect.bulkWrite(insertPageRedirectOperations);
+      try {
+        await PageRedirect.bulkWrite(insertPageRedirectOperations);
+      }
+      catch (err) {
+        if (err.code !== 11000) {
+          throw Error(`Failed to migrate: ${err}`);
+        }
+      }
     }
 
-    await Page.deleteMany({ redirectTo: { $ne: null } });
+    await pageCollection.deleteMany({ redirectTo: { $ne: null } });
 
     logger.info('Migration has successfully applied');
   },
 
   async down(db, client) {
     mongoose.connect(getMongoUri(), mongoOptions);
-    const Page = getModelSafely('Page') || getPageModel();
+    const pageCollection = await db.collection('pages');
     const PageRedirect = getModelSafely('PageRedirect') || PageRedirectModel;
 
     const cursor = PageRedirect.find().lean().cursor();
@@ -62,7 +69,14 @@ module.exports = {
         };
       });
 
-      await Page.bulkWrite(insertPageOperations);
+      try {
+        await pageCollection.bulkWrite(insertPageOperations);
+      }
+      catch (err) {
+        if (err.code !== 11000) {
+          throw Error(`Failed to migrate: ${err}`);
+        }
+      }
     }
 
     await PageRedirect.deleteMany();

+ 0 - 2
packages/app/src/server/models/page.ts

@@ -92,8 +92,6 @@ const schema = new Schema<PageDocument, PageModel>({
   deleteUser: { type: ObjectId, ref: 'User' },
   deletedAt: { type: Date },
 }, {
-  strict: true,
-  strictQuery: false,
   toJSON: { getters: true },
   toObject: { getters: true },
 });