You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sb...@apache.org on 2017/10/19 08:30:33 UTC

[10/11] ignite git commit: IGNITE-6647 Web Console: Implemented support of schema migration scripts.

IGNITE-6647 Web Console: Implemented support of schema migration scripts.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/c65399c7
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/c65399c7
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/c65399c7

Branch: refs/heads/ignite-3478
Commit: c65399c70136ce08d26de3a63204d931d4f96e9e
Parents: bab8acb
Author: Alexey Kuznetsov <ak...@apache.org>
Authored: Thu Oct 19 09:43:20 2017 +0700
Committer: Alexey Kuznetsov <ak...@apache.org>
Committed: Thu Oct 19 09:43:20 2017 +0700

----------------------------------------------------------------------
 modules/web-console/DEVNOTES.txt                |  6 +++
 modules/web-console/backend/index.js            | 53 +++++++++++++++-----
 .../web-console/backend/migrations/README.txt   |  4 ++
 modules/web-console/backend/package.json        |  1 +
 4 files changed, 52 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/c65399c7/modules/web-console/DEVNOTES.txt
----------------------------------------------------------------------
diff --git a/modules/web-console/DEVNOTES.txt b/modules/web-console/DEVNOTES.txt
index 85ec958..aa8702e 100644
--- a/modules/web-console/DEVNOTES.txt
+++ b/modules/web-console/DEVNOTES.txt
@@ -27,3 +27,9 @@ How to run console in development mode:
   If needed run "npm install --no-optional" (if dependencies changed) and start webpack in development mode "npm run dev".
 
 4. In browser open: http://localhost:9000
+
+How to migrate model:
+
+1. Model will be upgraded on first start.
+2. To downgrade model execute in terminal following command: "./node_modules/.bin/migrate down <migration-name> -d <dbConnectionUri>".
+   Example: "./node_modules/.bin/migrate down add_index -d mongodb://localhost/console".

http://git-wip-us.apache.org/repos/asf/ignite/blob/c65399c7/modules/web-console/backend/index.js
----------------------------------------------------------------------
diff --git a/modules/web-console/backend/index.js b/modules/web-console/backend/index.js
index f6ba439..06a38f8 100644
--- a/modules/web-console/backend/index.js
+++ b/modules/web-console/backend/index.js
@@ -17,13 +17,15 @@
 
 'use strict';
 
+const _ = require('lodash');
 const fs = require('fs');
 const path = require('path');
 const http = require('http');
 const https = require('https');
+const MigrateMongoose = require('migrate-mongoose');
 
 const igniteModules = process.env.IGNITE_MODULES ?
-    path.join(path.normalize(process.env.IGNITE_MODULES), 'backend') : './ignite_modules';
+    path.join(path.normalize(process.env.IGNITE_MODULES), 'backend') : path.join(__dirname, 'ignite_modules');
 
 let injector;
 
@@ -35,7 +37,7 @@ try {
     injector = require(igniteModulesInjector);
 }
 catch (ignore) {
-    injector = require(path.join(__dirname, './injector'));
+    injector = require(path.join(__dirname, 'injector'));
 }
 
 /**
@@ -63,15 +65,6 @@ const _onError = (addr, error) => {
 };
 
 /**
- * Event listener for HTTP server "listening" event.
- */
-const _onListening = (addr) => {
-    const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
-
-    console.log('Start listening on ' + bind);
-};
-
-/**
  * @param settings
  * @param {ApiServer} apiSrv
  * @param {AgentsHandler} agentsHnd
@@ -98,7 +91,43 @@ const init = ([settings, apiSrv, agentsHnd, browsersHnd]) => {
         process.send('running');
 };
 
-Promise.all([injector('settings'), injector('api-server'), injector('agents-handler'), injector('browsers-handler')])
+/**
+ * Run mongo model migration.
+ * 
+ * @param dbConnectionUri Mongo connection url.
+ * @param group Migrations group.
+ * @param migrationsPath Migrations path.
+ */
+const migrate = (dbConnectionUri, group, migrationsPath) => {
+    const migrator = new MigrateMongoose({
+        migrationsPath,
+        dbConnectionUri,
+        autosync: true
+    });
+
+    console.log(`Running ${group} migrations...`);
+
+    return migrator.run('up')
+        .then(() => console.log(`All ${group} migrations finished successfully.`))
+        .catch((err) => {
+            const msg = _.get(err, 'message');
+
+            if (_.startsWith(msg, 'There are no migrations to run') || _.startsWith(msg, 'There are no pending migrations.')) {
+                console.log(`There are no ${group} migrations to run.`);
+
+                return;
+            }
+
+            throw err;
+        });
+};
+
+injector('settings')
+    .then(({mongoUrl}) => {
+        return migrate(mongoUrl, 'Ignite', path.join(__dirname, 'migrations'))
+            .then(() => migrate(mongoUrl, 'Ignite Modules', path.join(igniteModules, 'migrations')));
+    })
+    .then(() => Promise.all([injector('settings'), injector('api-server'), injector('agents-handler'), injector('browsers-handler')]))
     .then(init)
     .catch((err) => {
         console.error(err);

http://git-wip-us.apache.org/repos/asf/ignite/blob/c65399c7/modules/web-console/backend/migrations/README.txt
----------------------------------------------------------------------
diff --git a/modules/web-console/backend/migrations/README.txt b/modules/web-console/backend/migrations/README.txt
new file mode 100644
index 0000000..e907fad
--- /dev/null
+++ b/modules/web-console/backend/migrations/README.txt
@@ -0,0 +1,4 @@
+Ignite Web Console
+======================================
+
+This folder contains scripts for model migration.

http://git-wip-us.apache.org/repos/asf/ignite/blob/c65399c7/modules/web-console/backend/package.json
----------------------------------------------------------------------
diff --git a/modules/web-console/backend/package.json b/modules/web-console/backend/package.json
index 07af45f..29aa734 100644
--- a/modules/web-console/backend/package.json
+++ b/modules/web-console/backend/package.json
@@ -40,6 +40,7 @@
     "glob": "7.1.2",
     "jszip": "3.1.3",
     "lodash": "4.17.4",
+    "migrate-mongoose": "3.2.2",
     "mongoose": "4.11.4",
     "morgan": "1.8.2",
     "nconf": "0.8.4",