Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Able to know if the system is up-to-date #47

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 50 additions & 10 deletions lib/mongodb-migrations.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 33 additions & 7 deletions src/mongodb-migrations.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test/isUpToDateFromdir.coffee
Original file line number Diff line number Diff line change
@@ -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()