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

support promises Fixes #44 #48

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
34 changes: 13 additions & 21 deletions src/mongodb-migrations.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Migrator
systemLog ' ' + res.error
if res.status is 'ok' or (res.status is 'skip' and res.code in ['no_up', 'no_down'])
handleMigrationDone(migration.id)

fn = migration[direction]
id = migration.id

Expand All @@ -135,26 +135,18 @@ class Migrator
migrationDone status: 'skip', reason: skipReason, code: skipCode
return runOne()

isCallbackCalled = false
if @_timeout
timeoutId = setTimeout () ->
isCallbackCalled = true
err = new Error "migration timed-out"
migrationDone status: 'error', error: err
allDone(err)
, @_timeout

context = { db: @_db, log: userLog }
fn.call context, (err) ->
return if isCallbackCalled
clearTimeout timeoutId

if err
migrationDone status: 'error', error: err
allDone(err)
else
migrationDone status: 'ok'
runOne()
fn = fn.bind { db: @_db, log: userLog }
p = if fn.length == 1 then Promise.fromCallback fn else Promise.resolve fn()
if @_timeout:
p = p.timeout(@_timeout, "migration timed-out")
recover = (err) ->
migrationDone status: 'error', error: err
allDone(err)
successful ->
migrationDone status: 'ok'
runOne()

p.then recover, successful

runOne()

Expand Down