2
0
Taichi Masuyama 4 жил өмнө
parent
commit
afc5de4649

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

@@ -683,20 +683,9 @@ module.exports = (crowi) => {
 
 
   // TODO: use job to show progress
   // 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 but should be dealed as a job
+      // TODO: 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) {

+ 10 - 16
packages/app/src/server/service/page.js

@@ -752,7 +752,6 @@ class PageService {
         // },
         // },
         {
         {
           $match: {
           $match: {
-            path: /^\/XSAUNMAX/g,
             grant,
             grant,
             parent: null,
             parent: null,
           },
           },
@@ -767,20 +766,19 @@ class PageService {
       .cursor({ batchSize: BATCH_SIZE }) // get stream
       .cursor({ batchSize: BATCH_SIZE }) // get stream
       .exec();
       .exec();
 
 
+    // use batch stream
     const batchStream = createBatchStream(BATCH_SIZE);
     const batchStream = createBatchStream(BATCH_SIZE);
 
 
+    // migrate all siblings for each page
     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, index) => {
+        const updateManyOperations = await Promise.all(pages.map(async(page) => {
           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, path: 1 }).lean().exec(); // かえる(path)
-          console.log(`PARENT${index}`, parent);
+          // get parentId for updating siblings
+          let parent = await Page.findOne({ path: parentPath }).select({ _id: 1, path: 1 }).lean().exec();
           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();
@@ -790,20 +788,16 @@ class PageService {
               throw err;
               throw err;
             }
             }
           }
           }
-
-          let parentPathForRegexp = parentPath;
-          if (parentPath === '/') {
-            parentPathForRegexp = '';
-          }
-
           const parentId = parent._id;
           const parentId = parent._id;
 
 
-          console.log(`LAST_PARENTID${index}`, parentId);
+          // modify to adjust for RegExp
+          const parentPathForRegexp = parentPath === '/' ? parentPath : '';
 
 
+          // TODO: consider filter to improve the target selection
           return {
           return {
             updateMany: {
             updateMany: {
               filter: {
               filter: {
-                path: { $regex: new RegExp(`^${parentPath}(\\/[^/]+)\\/?$`, 'g') }, // ex. /parent/any_child OR /any_level1
+                path: { $regex: new RegExp(`^${parentPathForRegexp}(\\/[^/]+)\\/?$`, 'g') }, // ex. /parent/any_child OR /any_level1
               },
               },
               update: {
               update: {
                 parent: parentId,
                 parent: parentId,
@@ -827,7 +821,7 @@ class PageService {
 
 
     await streamToPromise(migratePagesStream);
     await streamToPromise(migratePagesStream);
 
 
-    if (false && (await Page.exists({ grant, parent: null }))) {
+    if ((await Page.exists({ grant, parent: null }))) {
       await this.v5RecursiveMigration(grant, rootPath);
       await this.v5RecursiveMigration(grant, rootPath);
     }
     }
   }
   }