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

Implements support for promise based migrations. #82

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 31 additions & 2 deletions lib/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ function migrate (set, direction, migrationName, fn) {
}

set.emit('migration', migration, direction)
migration[direction](function (err) {
if (err) return fn(err)
let migrationCompleted = false

let migrationPromise = migration[direction](function (err) {
if (migrationCompleted) {
Copy link
Collaborator

@wesleytodd wesleytodd Oct 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though I prefer multi-line if statements with curlys like this, the majority of the code base is done on a single line. Please follow the common formatting.

return fn(new Error('Migration was already run. Please provide callback OR promise, not both.'))
}
if (err) return fn(err)
migrationCompleted = true
// Set timestamp if running up, clear it if down
migration.timestamp = direction === 'up' ? Date.now() : null

Expand All @@ -42,6 +47,30 @@ function migrate (set, direction, migrationName, fn) {
next(migrations.shift())
})
})

if (migrationPromise && migrationPromise.then) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer this to be typeof Promise !== 'undefined' && migrationPromise instanceof Promise

migrationPromise
.then(function () {
if (migrationCompleted) {
return fn(new Error('Migration was already run. Please provide callback OR promise, not both.'))
}

migrationCompleted = true
// Set timestamp if running up, clear it if down
migration.timestamp = direction === 'up' ? Date.now() : null

// Decrement last run index
lastRunIndex--

set.lastRun = direction === 'up' ? migration.title : set.migrations[lastRunIndex] && set.migrations[lastRunIndex].title
set.save(function (err) {
if (err) return fn(err)

next(migrations.shift())
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this fine, or should i split the callback out into a separate function, which can be reused in both callback & promise cases?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I would do that for sure.

})
.catch(fn)
}
}

next(migrations.shift())
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/promises/1-callback-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

module.exports.up = function (next) {
setTimeout(function () {
return next()
}, 200)
}

module.exports.down = function (next) {
setTimeout(function () {
return next()
}, 200)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce test time, just do a timeout of 1 ms. It achieves the same effect without the wait.

}
17 changes: 17 additions & 0 deletions test/fixtures/promises/2-promise-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

module.exports.up = function () {
return new Promise(function (resolve, reject) {
setTimeout(function () {
return resolve()
}, 200)
})
}

module.exports.down = function () {
return new Promise(function (resolve, reject) {
setTimeout(function () {
return resolve()
}, 200)
})
}
19 changes: 19 additions & 0 deletions test/fixtures/promises/3-failure-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

module.exports.up = function (next) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
next()
return resolve()
}, 200)
})
}

module.exports.down = function (next) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
next()
return resolve()
}, 200)
})
}
56 changes: 56 additions & 0 deletions test/promises.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* global describe, it, beforeEach, afterEach */

var rimraf = require('rimraf')
var path = require('path')
var assert = require('assert')

var migrate = require('../')

var BASE = path.join(__dirname, 'fixtures', 'promises')
var STATE = path.join(__dirname, 'fixtures', '.migrate')

describe('Promise migrations', function () {
var set

beforeEach(function (done) {
migrate.load({
stateStore: STATE,
migrationsDirectory: BASE
}, function (err, s) {
set = s
done(err)
})
})

it('should handle callback migration', function (done) {
set.up('1-callback-test.js', function (err) {
assert.ifError(err)
done()
})
})

it('should handle promise migration', function (done) {
set.up('2-promise-test.js', function (err) {
assert.ifError(err)
done()
})
})

it('should error when using promise + callback', function (done) {
let errorThrown = false
set.up('3-failure-test.js', function (err) {
if (errorThrown) {
return
}

assert(err)
assert.equal(err.message, 'Migration was already run. Please provide callback OR promise, not both.')
errorThrown = true
done()
})
})

afterEach(function (done) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to the top like in the other tests. If this file grows it will get lost way down here.

rimraf(STATE, done)
})
})