Просмотр исходного кода

Splitting the processor into module units

Shun Miyazawa 2 лет назад
Родитель
Сommit
f3045049f5

+ 8 - 10
bin/data-migrations/src/index.js

@@ -8,20 +8,18 @@ var revisionsCollection = db.getCollection('revisions');
 var batchSize = process.env.BATCH_SIZE ?? 100; // default 100 revisions in 1 bulkwrite
 var batchSizeInterval = process.env.BATCH_INTERVAL ?? 3000; // default 3 sec
 
-var migrationFileName = process.env.MIGRATION_FILE_NAME;
-var migrationType = process.env.MIGRATION_TYPE;
+var migrationModule = process.env.MIGRATION_MODULE;
 
-var getProcessorArray = require(`./migrations/${migrationFileName}`);
-var processors = getProcessorArray(migrationType);
+var migrationModules = require(`./migrations/${migrationModule}`);
 
-if (processors.length === 0) {
-  throw Error('No valid processors found. Please enter a valid environment variable');
+if (migrationModules.length === 0) {
+  throw Error('No valid migrationModules found. Please enter a valid environment variable');
 }
 
-function replaceLatestRevisions(body, processors) {
+function replaceLatestRevisions(body, migrationModules) {
   var replacedBody = body;
-  processors.forEach((processor) => {
-    replacedBody = processor(replacedBody);
+  migrationModules.forEach((migrationModule) => {
+    replacedBody = migrationModule(replacedBody);
   });
   return replacedBody;
 }
@@ -30,7 +28,7 @@ var operations = [];
 pagesCollection.find({}).forEach((doc) => {
   if (doc.revision) {
     var revision = revisionsCollection.findOne({ _id: doc.revision });
-    var replacedBody = replaceLatestRevisions(revision.body, [...processors]);
+    var replacedBody = replaceLatestRevisions(revision.body, [...migrationModules]);
     var operation = {
       updateOne: {
         filter: { _id: revision._id },

+ 8 - 19
bin/data-migrations/src/migrations/custom.js

@@ -6,23 +6,12 @@
 // processors for old format
 // ===========================================
 
-// processor for MIGRATION_FILE_NAME=custom, MIGRATION_TYPE=custom
-function customProcessor(body) {
-  // ADD YOUR PROCESS HERE!
-  // https://github.com/weseek/growi/discussions/7180
-  return body;
-}
+// processor for MIGRATION_MODULE=custom
 
-function getProcessorArray(migrationType) {
-  var oldFormatProcessors;
-  switch (migrationType) {
-    case 'custom':
-      oldFormatProcessors = [customProcessor];
-      break;
-    default:
-      oldFormatProcessors = [];
-  }
-  return oldFormatProcessors;
-}
-
-module.exports = getProcessorArray;
+module.exports = [
+  (body) => {
+    // ADD YOUR PROCESS HERE!
+    // https://github.com/weseek/growi/discussions/7180
+    return body;
+  },
+];

+ 0 - 66
bin/data-migrations/src/migrations/v60x.js

@@ -1,66 +0,0 @@
-
-/* eslint-disable no-undef, no-var, vars-on-top, no-restricted-globals, regex/invalid */
-// ignore lint error because this file is js as mongoshell
-
-// ===========================================
-// processors for old format
-// ===========================================
-
-function drawioProcessor(body) {
-  var oldDrawioRegExp = /:::\s?drawio\n(.+?)\n:::/g; // drawio old format
-  return body.replace(oldDrawioRegExp, '``` drawio\n$1\n```');
-}
-
-function plantumlProcessor(body) {
-  var oldPlantUmlRegExp = /@startuml\n([\s\S]*?)\n@enduml/g; // plantUML old format
-  return body.replace(oldPlantUmlRegExp, '``` plantuml\n$1\n```');
-}
-
-function tsvProcessor(body) {
-  var oldTsvTableRegExp = /::: tsv(-h)?\n([\s\S]*?)\n:::/g; // TSV old format
-  return body.replace(oldTsvTableRegExp, '``` tsv$1\n$2\n```');
-}
-
-function csvProcessor(body) {
-  var oldCsvTableRegExp = /::: csv(-h)?\n([\s\S]*?)\n:::/g; // CSV old format
-  return body.replace(oldCsvTableRegExp, '``` csv$1\n$2\n```');
-}
-
-function bracketlinkProcessor(body) {
-  // https://regex101.com/r/btZ4hc/1
-  var oldBracketLinkRegExp = /(?<!\[)\[{1}(\/.*?)\]{1}(?!\])/g; // Page Link old format
-  return body.replace(oldBracketLinkRegExp, '[[$1]]');
-}
-
-// ===========================================
-// define processors
-// ===========================================
-
-function getProcessorArray(migrationType) {
-  var oldFormatProcessors;
-  switch (migrationType) {
-    case 'drawio':
-      oldFormatProcessors = [drawioProcessor];
-      break;
-    case 'plantuml':
-      oldFormatProcessors = [plantumlProcessor];
-      break;
-    case 'tsv':
-      oldFormatProcessors = [tsvProcessor];
-      break;
-    case 'csv':
-      oldFormatProcessors = [csvProcessor];
-      break;
-    case 'bracketlink':
-      oldFormatProcessors = [bracketlinkProcessor];
-      break;
-    case 'all':
-      oldFormatProcessors = [drawioProcessor, plantumlProcessor, tsvProcessor, csvProcessor, bracketlinkProcessor];
-      break;
-    default:
-      oldFormatProcessors = [];
-  }
-  return oldFormatProcessors;
-}
-
-module.exports = getProcessorArray;

+ 8 - 0
bin/data-migrations/src/migrations/v60x/bracketlink.js

@@ -0,0 +1,8 @@
+module.exports = [
+  (body) => {
+    // https://regex101.com/r/btZ4hc/1
+    // eslint-disable-next-line regex/invalid
+    const oldBracketLinkRegExp = /(?<!\[)\[{1}(\/.*?)\]{1}(?!\])/g; // Page Link old format
+    return body.replace(oldBracketLinkRegExp, '[[$1]]');
+  },
+];

+ 6 - 0
bin/data-migrations/src/migrations/v60x/csv.js

@@ -0,0 +1,6 @@
+module.exports = [
+  (body) => {
+    const oldCsvTableRegExp = /::: csv(-h)?\n([\s\S]*?)\n:::/g; // CSV old format
+    return body.replace(oldCsvTableRegExp, '``` csv$1\n$2\n```');
+  },
+];

+ 6 - 0
bin/data-migrations/src/migrations/v60x/drawio.js

@@ -0,0 +1,6 @@
+module.exports = [
+  (body) => {
+    const oldDrawioRegExp = /:::\s?drawio\n(.+?)\n:::/g; // drawio old format
+    return body.replace(oldDrawioRegExp, '``` drawio\n$1\n```');
+  },
+];

+ 6 - 0
bin/data-migrations/src/migrations/v60x/index.js

@@ -0,0 +1,6 @@
+const bracketlink = require('./bracketlink');
+const csv = require('./csv');
+const plantUML = require('./plantuml');
+const tsv = require('./tsv');
+
+module.exports = [...bracketlink, ...csv, ...plantUML, ...tsv];

+ 6 - 0
bin/data-migrations/src/migrations/v60x/plantuml.js

@@ -0,0 +1,6 @@
+module.exports = [
+  (body) => {
+    const oldDrawioRegExp = /:::\s?drawio\n(.+?)\n:::/g; // drawio old format
+    return body.replace(oldDrawioRegExp, '``` drawio\n$1\n```');
+  },
+];

+ 6 - 0
bin/data-migrations/src/migrations/v60x/tsv.js

@@ -0,0 +1,6 @@
+module.exports = [
+  (body) => {
+    const oldTsvTableRegExp = /::: tsv(-h)?\n([\s\S]*?)\n:::/g; // TSV old format
+    return body.replace(oldTsvTableRegExp, '``` tsv$1\n$2\n```');
+  },
+];

+ 0 - 33
bin/data-migrations/src/migrations/v61x.js

@@ -1,33 +0,0 @@
-
-/* eslint-disable no-undef, no-var, vars-on-top, no-restricted-globals, regex/invalid */
-// ignore lint error because this file is js as mongoshell
-
-// ===========================================
-// processors for old format
-// ===========================================
-
-function mdcontPrefixProcessor(body) {
-  var oldMdcontPrefixRegExp = /#mdcont-/g;
-  return body.replace(oldMdcontPrefixRegExp, '#');
-}
-
-// ===========================================
-// define processors
-// ===========================================
-
-function getProcessorArray(migrationType) {
-  var oldFormatProcessors;
-  switch (migrationType) {
-    case 'mdcont':
-      oldFormatProcessors = [mdcontPrefixProcessor];
-      break;
-    case 'all':
-      oldFormatProcessors = [mdcontPrefixProcessor];
-      break;
-    default:
-      oldFormatProcessors = [];
-  }
-  return oldFormatProcessors;
-}
-
-module.exports = getProcessorArray;

+ 3 - 0
bin/data-migrations/src/migrations/v61x/index.js

@@ -0,0 +1,3 @@
+const mdcont = require('./mdcont');
+
+module.exports = [...mdcont];

+ 6 - 0
bin/data-migrations/src/migrations/v61x/mdcont.js

@@ -0,0 +1,6 @@
+module.exports = [
+  (body) => {
+    const oldMdcontPrefixRegExp = /#mdcont-/g;
+    return body.replace(oldMdcontPrefixRegExp, '#');
+  },
+];