Taichi Masuyama 4 лет назад
Родитель
Сommit
95f75d61eb
2 измененных файлов с 38 добавлено и 14 удалено
  1. 12 1
      packages/app/src/server/routes/apiv3/pages.js
  2. 26 13
      packages/app/src/server/service/page.js

+ 12 - 1
packages/app/src/server/routes/apiv3/pages.js

@@ -681,11 +681,22 @@ module.exports = (crowi) => {
 
 
   });
   });
 
 
+  // TODO: use job to show progress
   router.get('/v5-schema-migration', /* accessTokenParser, loginRequired, adminRequired, csrf, */ async(req, res) => {
   router.get('/v5-schema-migration', /* accessTokenParser, loginRequired, adminRequired, csrf, */ async(req, res) => {
+    const Page = crowi.model('Page');
+    await Page.insertMany([
+      { path: '/XSAUNMAX' },
+      { path: '/XSAUNMAX/B1' },
+      { path: '/XSAUNMAX/B2' },
+      { path: '/XSAUNMAX/B1/C1' },
+      { path: '/XSAUNMAX/B2/C1' },
+      { path: '/XSAUNMAX/B2/C2/D1' },
+      { path: '/XSAUNMAX/B3/f/f/f/f' },
+    ]);
 
 
     try {
     try {
       const Page = crowi.model('Page');
       const Page = crowi.model('Page');
-      // not await
+      // not await but should be dealed as a job
       crowi.pageService.v5RecursiveMigration(Page.GRANT_PUBLIC);
       crowi.pageService.v5RecursiveMigration(Page.GRANT_PUBLIC);
     }
     }
     catch (err) {
     catch (err) {

+ 26 - 13
packages/app/src/server/service/page.js

@@ -742,15 +742,17 @@ class PageService {
     const BATCH_SIZE = 100;
     const BATCH_SIZE = 100;
     const Page = this.crowi.model('Page');
     const Page = this.crowi.model('Page');
 
 
-    let randomPagesStream = await Page
+    const randomPagesStream = await Page
       .aggregate([
       .aggregate([
-        {
-          $sample: {
-            size: BATCH_SIZE,
-          },
-        },
+        // TODO: randomize somehow sample does not work when the result is under 100?
+        // {
+        //   $sample: {
+        //     size: BATCH_SIZE,
+        //   },
+        // },
         {
         {
           $match: {
           $match: {
+            path: /^\/XSAUNMAX/g,
             grant,
             grant,
             parent: null,
             parent: null,
           },
           },
@@ -770,11 +772,15 @@ class PageService {
     const migratePagesStream = new Writable({
     const migratePagesStream = new Writable({
       objectMode: true,
       objectMode: true,
       async write(pages, encoding, callback) {
       async write(pages, encoding, callback) {
+        console.log('ぺーじす', pages, 'ぺーじす');
         // bulkWrite to update parent
         // bulkWrite to update parent
-        const updateManyOperations = await Promise.all(pages.map(async(page) => {
+        const updateManyOperations = await Promise.all(pages.map(async(page, index) => {
           const parentPath = pathlib.dirname(page.path);
           const parentPath = pathlib.dirname(page.path);
+          console.log(`PARENT_PATH${index}`, parentPath);
 
 
-          let parent = await Page.findOne({ path: parentPath }).select({ _id: 1 }).lean().exec();
+
+          let parent = await Page.findOne({ path: parentPath }).select({ _id: 1, path: 1 }).lean().exec(); // かえる(path)
+          console.log(`PARENT${index}`, parent);
           if (parent == null) {
           if (parent == null) {
             try {
             try {
               parent = await (new Page({ path: parentPath, isEmpty: true })).save();
               parent = await (new Page({ path: parentPath, isEmpty: true })).save();
@@ -785,13 +791,22 @@ class PageService {
             }
             }
           }
           }
 
 
+          let parentPathForRegexp = parentPath;
+          if (parentPath === '/') {
+            parentPathForRegexp = '';
+          }
+
+          const parentId = parent._id;
+
+          console.log(`LAST_PARENTID${index}`, parentId);
+
           return {
           return {
             updateMany: {
             updateMany: {
               filter: {
               filter: {
-                path: new RegExp(`^\\/${parentPath}\\/[^/]+\\/?$`), // ex. /parent/any_child
+                path: { $regex: new RegExp(`^${parentPath}(\\/[^/]+)\\/?$`, 'g') }, // ex. /parent/any_child OR /any_level1
               },
               },
               update: {
               update: {
-                parent,
+                parent: parentId,
               },
               },
             },
             },
           };
           };
@@ -812,9 +827,7 @@ class PageService {
 
 
     await streamToPromise(migratePagesStream);
     await streamToPromise(migratePagesStream);
 
 
-    randomPagesStream = null;
-
-    if ((await Page.exists({ grant, parent: null, path: { $ne: '/' } }))) {
+    if (false && (await Page.exists({ grant, parent: null }))) {
       await this.v5RecursiveMigration(grant, rootPath);
       await this.v5RecursiveMigration(grant, rootPath);
     }
     }
   }
   }