diff --git a/app/utils/pouch-views.js b/app/utils/pouch-views.js index 17b9b75933..d4e6e79a4f 100644 --- a/app/utils/pouch-views.js +++ b/app/utils/pouch-views.js @@ -1,7 +1,16 @@ +import Ember from 'ember'; /* global req */ /* global compareStrings */ /* global getCompareDate */ +function buildIndex(indexName, db) { + return db.query(indexName, { + limit: 0 + }).catch(function(err) { + console.log('index error:' + JSON.stringify(err, null, 2)); + }); +} + function createDesignDoc(item, rev) { var ddoc = { _id: '_design/' + item.name, @@ -21,6 +30,23 @@ function createDesignDoc(item, rev) { return ddoc; } +function checkForUpdate(view, db, runningTest, testDumpFile) { + return db.get('_design/' + view.name).then(function(doc) { + if (doc.version !== view.version) { + return updateDesignDoc(view, db, doc._rev, runningTest, testDumpFile); + } else { + if (runningTest) { + // Indexes need to be built when running tests + return buildIndex(view.name, db); + } else { + return Ember.RSVP.resolve(); + } + } + }, function() { + return updateDesignDoc(view, db, null, runningTest, testDumpFile); + }); +} + function generateSortFunction(sortFunction, includeCompareDate, filterFunction) { var generatedFunction = 'function(head, req) {' + 'function keysEqual(keyA, keyB) {' + @@ -99,14 +125,16 @@ function generateView(viewDocType, viewBody) { '}'; } -function updateDesignDoc(item, db, rev) { +function updateDesignDoc(item, db, rev, runningTest, testDumpFile) { var designDoc = createDesignDoc(item, rev); - db.put(designDoc).then(function() { - // design doc created! + if (runningTest) { + console.log(`WARNING: The view ${item.name} is out of date. Please update the pouch dump ${testDumpFile} to the latest version of ${item.name}`); + } + return db.put(designDoc).then(function() { // Update index - db.query(item.name, { stale: 'update_after' }); + return buildIndex(item.name, db); }, function(err) { - console.log('ERR updateDesignDoc:', err); + console.log('ERR updating design doc:', JSON.stringify(err, null, 2)); // ignored, design doc already exists }); } @@ -391,14 +419,10 @@ var designDocs = [{ version: 4 }]; -export default function(db) { +export default function(db, runningTest, testDumpFile) { + var viewUpdates = []; designDocs.forEach(function(item) { - db.get('_design/' + item.name).then(function(doc) { - if (doc.version !== item.version) { - updateDesignDoc(item, db, doc._rev); - } - }, function() { - updateDesignDoc(item, db); - }); + viewUpdates.push(checkForUpdate(item, db, runningTest, testDumpFile)); }); + return Ember.RSVP.all(viewUpdates); } diff --git a/tests/helpers/run-with-pouch-dump.js b/tests/helpers/run-with-pouch-dump.js index 8c2d7cb0a4..c697899753 100644 --- a/tests/helpers/run-with-pouch-dump.js +++ b/tests/helpers/run-with-pouch-dump.js @@ -1,4 +1,5 @@ /* jshint ignore:start */ +import createPouchViews from 'hospitalrun/utils/pouch-views'; import Ember from 'ember'; import PouchDB from 'pouchdb'; import DatabaseService from 'hospitalrun/services/database'; @@ -39,9 +40,7 @@ function runWithPouchDumpAsyncHelper(app, dumpName, functionToRun) { adapter: 'memory' }); const dump = require(`hospitalrun/tests/fixtures/${dumpName}`).default; - const promise = db.load(dump, { - proxy: 'main' - }); + const promise = db.load(dump); const InMemoryDatabaseService = DatabaseService.extend({ createDB() { @@ -68,13 +67,19 @@ function runWithPouchDumpAsyncHelper(app, dumpName, functionToRun) { return new Ember.RSVP.Promise(function(resolve) { promise.then(function() { - functionToRun(); - andThen(function() { - cleanupDatabases({ - config: configDB, - main: db - }).then(resolve); + createPouchViews(db, true, dumpName).then(function() { + functionToRun(); + andThen(function() { + cleanupDatabases({ + config: configDB, + main: db + }).then(resolve, function(err) { + console.log('error cleaning up dbs:', JSON.stringify(err, null, 2)); + }); + }); }); + }, function(err) { + console.log('error loading db', JSON.stringify(err, null, 2)); }); }); }