From fd392d05f0955cc78c745b0e26a050422fe8daca Mon Sep 17 00:00:00 2001 From: Thomas Hilaire Date: Mon, 12 Jun 2017 12:24:09 +0200 Subject: [PATCH] Able to know if the system is up-to-date --- lib/mongodb-migrations.js | 60 +++++++++++++++++++++++++++++------ src/mongodb-migrations.coffee | 40 +++++++++++++++++++---- test/isUpToDateFromdir.coffee | 31 ++++++++++++++++++ 3 files changed, 114 insertions(+), 17 deletions(-) create mode 100644 test/isUpToDateFromdir.coffee diff --git a/lib/mongodb-migrations.js b/lib/mongodb-migrations.js index 0410834..a5cd46e 100644 --- a/lib/mongodb-migrations.js +++ b/lib/mongodb-migrations.js @@ -58,6 +58,24 @@ return this._db.collection(this._collName); }; + Migrator.prototype._collectRanMigrations = function(cb) { + this._ranMigrations = {}; + return this._coll().find().toArray((function(_this) { + return function(err, docs) { + var doc, j, len; + if (err) { + return cb(err); + } else { + for (j = 0, len = docs.length; j < len; j++) { + doc = docs[j]; + _this._ranMigrations[doc.id] = true; + } + return cb(null); + } + }; + })(this)); + }; + Migrator.prototype._runWhenReady = function(direction, cb, progress) { var onError, onSuccess; if (this._isDisposed) { @@ -65,17 +83,12 @@ } onSuccess = (function(_this) { return function() { - _this._ranMigrations = {}; - return _this._coll().find().toArray(function(err, docs) { - var doc, j, len; + return _this._collectRanMigrations(function(err) { if (err) { return cb(err); + } else { + return _this._run(direction, cb, progress); } - for (j = 0, len = docs.length; j < len; j++) { - doc = docs[j]; - _this._ranMigrations[doc.id] = true; - } - return _this._run(direction, cb, progress); }); }; })(this); @@ -262,8 +275,9 @@ }).sort(function(f1, f2) { return f1.number - f2.number; }).map(function(f) { - var fileName; - fileName = path.join(dir, f.name); + var fileName, migrationsDir; + migrationsDir = path.resolve(process.cwd(), dir); + fileName = path.join(migrationsDir, f.name); if (fileName.match(/\.coffee$/)) { require('coffee-script/register'); } @@ -289,6 +303,32 @@ })(this)); }; + Migrator.prototype.isUpToDateFromDir = function(dir, done) { + return this._loadMigrationFiles(dir, (function(_this) { + return function(err, migrationFiles) { + var makeDiffWhenDbReady; + if (err) { + return done(err); + } else { + makeDiffWhenDbReady = function() { + return _this._collectRanMigrations(function(err) { + var differenceCount, migrationFilesIds, ranMigrationsIds; + if (err) { + return done(err); + } else { + migrationFilesIds = _.map(migrationFiles, 'module.id'); + ranMigrationsIds = _.keys(_this._ranMigrations); + differenceCount = _.difference(migrationFilesIds, ranMigrationsIds); + return done(null, differenceCount.length === 0); + } + }); + }; + return _this._dbReady.then(makeDiffWhenDbReady, done); + } + }; + })(this)); + }; + Migrator.prototype.create = function(dir, id, done, coffeeScript) { if (coffeeScript == null) { coffeeScript = false; diff --git a/src/mongodb-migrations.coffee b/src/mongodb-migrations.coffee index ce3eb1c..5f07867 100644 --- a/src/mongodb-migrations.coffee +++ b/src/mongodb-migrations.coffee @@ -44,17 +44,25 @@ class Migrator _coll: -> @_db.collection(@_collName) + _collectRanMigrations: (cb) -> + @_ranMigrations = {} + @_coll().find().toArray (err, docs) => + if err + cb err + else + for doc in docs + @_ranMigrations[doc.id] = true + cb null + _runWhenReady: (direction, cb, progress) -> if @_isDisposed return cb new Error 'This migrator is disposed and cannot be used anymore' onSuccess = => - @_ranMigrations = {} - @_coll().find().toArray (err, docs) => + @_collectRanMigrations (err) => if err - return cb err - for doc in docs - @_ranMigrations[doc.id] = true - @_run direction, cb, progress + cb err + else + @_run direction, cb, progress onError = (err) -> cb err @_dbReady.then onSuccess, onError @@ -188,7 +196,8 @@ class Migrator .filter (f) -> !!f.name .sort (f1, f2) -> f1.number - f2.number .map (f) -> - fileName = path.join dir, f.name + migrationsDir = path.resolve process.cwd(), dir + fileName = path.join migrationsDir, f.name if fileName.match /\.coffee$/ require('coffee-script/register') return { number: f.number, module: require(fileName) } @@ -201,6 +210,23 @@ class Migrator @bulkAdd _.map(files, 'module') @migrate done, progress + isUpToDateFromDir: (dir, done) -> + @_loadMigrationFiles dir, (err, migrationFiles) => + if err + done err + else + makeDiffWhenDbReady = => + @_collectRanMigrations (err) => + if err + done err + else + migrationFilesIds = _.map(migrationFiles, 'module.id') + ranMigrationsIds = _.keys(@_ranMigrations) + differenceCount = _.difference(migrationFilesIds, ranMigrationsIds) + done null, differenceCount.length == 0 + @_dbReady.then makeDiffWhenDbReady, done + + create: (dir, id, done, coffeeScript=false) -> @_loadMigrationFiles dir, (err, files) -> if err diff --git a/test/isUpToDateFromdir.coffee b/test/isUpToDateFromdir.coffee new file mode 100644 index 0000000..507701e --- /dev/null +++ b/test/isUpToDateFromdir.coffee @@ -0,0 +1,31 @@ +path = require 'path' +mm = require '../src/mongodb-migrations' +testsCommon = require './common' + +describe 'Is up to date from Directory', -> + migrator = null + db = null + coll = null + + beforeEach (done) -> + testsCommon.beforeEach (res) -> + {migrator, db} = res + coll = db.collection 'test' + coll.remove {}, -> + done() + + it 'be false if migrations were not ran', (done) -> + dir = path.join __dirname, 'migrations' + migrator.isUpToDateFromDir dir, (err, res) -> + return done(err) if err + res.should.be.equal false + done() + + it 'be true if migrations were ran', (done) -> + dir = path.join __dirname, 'migrations' + migrator.runFromDir dir, (err) -> + return done(err) if err + migrator.isUpToDateFromDir dir, (err, res) -> + return done(err) if err + res.should.be.equal true + done()