Przeglądaj źródła

Merge pull request #9590 from weseek/imprv/160722-make-data-migration-type-safe

imprv: Make data migration type safe
Yuki Takei 1 rok temu
rodzic
commit
c9456fbe50

+ 5 - 3
bin/data-migrations/src/index.js

@@ -5,13 +5,14 @@
 /**
 /**
  * @typedef {import('./types').MigrationModule} MigrationModule
  * @typedef {import('./types').MigrationModule} MigrationModule
  * @typedef {import('./types').ReplaceLatestRevisions} ReplaceLatestRevisions
  * @typedef {import('./types').ReplaceLatestRevisions} ReplaceLatestRevisions
+ * @typedef {import('./types').Operatioins } Operations
  */
  */
 
 
 var pagesCollection = db.getCollection('pages');
 var pagesCollection = db.getCollection('pages');
 var revisionsCollection = db.getCollection('revisions');
 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 batchSize = Number(process.env.BATCH_SIZE ?? 100); // default 100 revisions in 1 bulkwrite
+var batchSizeInterval = Number(process.env.BATCH_INTERVAL ?? 3000); // default 3 sec
 
 
 var migrationModule = process.env.MIGRATION_MODULE;
 var migrationModule = process.env.MIGRATION_MODULE;
 
 
@@ -31,8 +32,9 @@ function replaceLatestRevisions(body, migrationModules) {
   return replacedBody;
   return replacedBody;
 }
 }
 
 
+/** @type {Operations} */
 var operations = [];
 var operations = [];
-pagesCollection.find({}).forEach((doc) => {
+pagesCollection.find({}).forEach((/** @type {any} */ doc) => {
   if (doc.revision) {
   if (doc.revision) {
     try {
     try {
       var revision = revisionsCollection.findOne({ _id: doc.revision });
       var revision = revisionsCollection.findOne({ _id: doc.revision });

+ 1 - 1
bin/data-migrations/src/migrations/custom.js

@@ -1,5 +1,5 @@
 /**
 /**
- * @typedef {import('../../types').MigrationModule} MigrationModule
+ * @typedef {import('../types').MigrationModule} MigrationModule
  */
  */
 
 
 module.exports = [
 module.exports = [

+ 16 - 0
bin/data-migrations/src/types.d.ts

@@ -1,2 +1,18 @@
 export type MigrationModule = (body: string) => string;
 export type MigrationModule = (body: string) => string;
 export type ReplaceLatestRevisions = (body: string, migrationModules: MigrationModule[]) => string;
 export type ReplaceLatestRevisions = (body: string, migrationModules: MigrationModule[]) => string;
+
+export type Operatioins = Array<
+  {
+    updateOne: {
+      filter: { _id: string }
+      update: { $set: { body: string }}
+    }
+  }
+>
+
+export declare global {
+  const db;
+  const sleep;
+
+  function print(arg: string): void;
+}

+ 8 - 0
bin/data-migrations/tsconfig.json

@@ -0,0 +1,8 @@
+{
+  "$schema": "http://json.schemastore.org/tsconfig",
+  "extends": "../../tsconfig.base.json",
+  "compilerOptions": {
+    "checkJs": true,
+    "strict": true,
+  }
+}