فهرست منبع

Merge pull request #5515 from weseek/fix/normalize-parent-with-empty-pages

fix: Normalize parent with empty pages
Yuki Takei 4 سال پیش
والد
کامیت
dd6625b6b9
2فایلهای تغییر یافته به همراه33 افزوده شده و 11 حذف شده
  1. 24 3
      packages/app/src/server/service/page.ts
  2. 9 8
      packages/app/test/integration/service/v5.migration.test.js

+ 24 - 3
packages/app/src/server/service/page.ts

@@ -2571,17 +2571,38 @@ class PageService {
       async write(pages, encoding, callback) {
         const parentPaths = Array.from(new Set<string>(pages.map(p => pathlib.dirname(p.path))));
 
-        // 1. Remove unnecessary empty pages
+        // 1. Remove unnecessary empty pages & reset parent for pages which had had those empty pages
         const pageIdsToNotDelete = pages.map(p => p._id);
         const emptyPagePathsToDelete = pages.map(p => p.path);
+
+        const builder1 = new PageQueryBuilder(Page.find({ isEmpty: true }, { _id: 1 }), true);
+        builder1.addConditionToListByPathsArray(emptyPagePathsToDelete);
+        builder1.addConditionToExcludeByPageIdsArray(pageIdsToNotDelete);
+
+        const emptyPagesToDelete = await builder1.query.lean().exec();
+        const resetParentOperations = emptyPagesToDelete.map((p) => {
+          return {
+            updateOne: {
+              filter: {
+                parent: p._id,
+              },
+              update: {
+                parent: null,
+              },
+            },
+          };
+        });
+
+        await Page.bulkWrite(resetParentOperations);
+
         await Page.removeEmptyPages(pageIdsToNotDelete, emptyPagePathsToDelete);
 
         // 2. Create lacking parents as empty pages
         await Page.createEmptyPagesByPaths(parentPaths, user, false);
 
         // 3. Find parents
-        const builder = new PageQueryBuilder(Page.find({}, { _id: 1, path: 1 }), true);
-        const parents = await builder
+        const builder2 = new PageQueryBuilder(Page.find({}, { _id: 1, path: 1 }), true);
+        const parents = await builder2
           .addConditionToListByPathsArray(parentPaths)
           .query
           .lean()

+ 9 - 8
packages/app/test/integration/service/v5.migration.test.js

@@ -282,11 +282,13 @@ describe('V5 page migration', () => {
     });
 
     test("should replace empty page with same path with new non-empty page and update all related children's parent", async() => {
-      const page1 = await Page.findOne({ path: '/normalize_10', isEmpty: true });
-      const page2 = await Page.findOne({ path: '/normalize_10/normalize_11_gA', _id: pageId8, isEmpty: true });
-      const page3 = await Page.findOne({ path: '/normalize_10/normalize_11_gA', _id: pageId9 }); // not v5
-      const page4 = await Page.findOne({ path: '/normalize_10/normalize_11_gA/normalize_11_gB' });
-      const page5 = await Page.findOne({ path: '/normalize_10/normalize_12_gC' });
+      const page1 = await Page.findOne({ path: '/normalize_10', isEmpty: true, parent: { $ne: null } });
+      const page2 = await Page.findOne({
+        path: '/normalize_10/normalize_11_gA', _id: pageId8, isEmpty: true, parent: { $ne: null },
+      });
+      const page3 = await Page.findOne({ path: '/normalize_10/normalize_11_gA', _id: pageId9, parent: null }); // not v5
+      const page4 = await Page.findOne({ path: '/normalize_10/normalize_11_gA/normalize_11_gB', parent: { $ne: null } });
+      const page5 = await Page.findOne({ path: '/normalize_10/normalize_12_gC', parent: { $ne: null } });
       expectAllToBeTruthy([page1, page2, page3, page4, page5]);
 
       await normalizeParentRecursivelyByPages([page3], testUser1);
@@ -302,11 +304,10 @@ describe('V5 page migration', () => {
 
       expect(page1AM.isEmpty).toBeTruthy();
       expect(page3AM.parent).toStrictEqual(page1AM._id);
-      // Todo: enable the code below after this is solved: https://redmine.weseek.co.jp/issues/90060
-      // expect(page4AM.parent).toStrictEqual(page3AF._id);
+      expect(page4AM.parent).toStrictEqual(page3AM._id);
       expect(page5AM.parent).toStrictEqual(page1AM._id);
 
-      expect(page3AM.isEmpty).toBeFalsy();
+      expect(page3AM.isEmpty).toBe(false);
     });
   });