Comparing version 0.2.0 to 0.2.1
@@ -18,3 +18,7 @@ var RSVP = require("rsvp"); | ||
var promises = []; | ||
var collectionsToDrop = [].concat(this.collectionsToDrop); | ||
var collectionsToDrop = []; | ||
if (_.isArray(this.collectionsToDrop)){ | ||
collectionsToDrop = collectionsToDrop.concat(this.collectionsToDrop); | ||
} | ||
@@ -39,15 +43,13 @@ collectionsToDrop.forEach(function(collectionName){ | ||
var p = new RSVP.Promise(function(resolve, reject){ | ||
var collections = _.keys(mongoose.connection.collections); | ||
var collection = mongoose.connection.collections[collectionName]; | ||
var hasCollection = (!!collection); | ||
that._hasCollection(collectionName, function(err, hasCollection) { | ||
if (err) { return reject(err); } | ||
if (!hasCollection) { return resolve(); } | ||
if (hasCollection){ | ||
collection.drop(function(err) { | ||
if (err) { reject(err); } | ||
that._dropCollection(collectionName, function(err) { | ||
if (err) { return reject(err); } | ||
resolve(); | ||
}); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
@@ -58,2 +60,32 @@ | ||
CollectionDropper.prototype._hasCollection = function(collectionName, cb){ | ||
mongoose.connection.db.collectionNames(function(err, names){ | ||
if (err) { return cb(err); } | ||
var hasCollection = false; | ||
names.forEach(function(col){ | ||
if (col.name.split(".")[1] === collectionName){ | ||
hasCollection = true; | ||
} | ||
}); | ||
cb(null, hasCollection); | ||
}); | ||
}; | ||
CollectionDropper.prototype._dropCollection = function(collectionName, cb){ | ||
mongoose.connection.db.collection(collectionName, function(err, collection){ | ||
if (err) { return cb(err); } | ||
if (!collection){ | ||
return cb(); | ||
} | ||
collection.drop(function(err){ | ||
if (err) { return cb(err); } | ||
return cb(); | ||
}); | ||
}); | ||
}; | ||
// Exports | ||
@@ -60,0 +92,0 @@ // ------- |
@@ -96,3 +96,3 @@ var util = require("util"); | ||
that._complete(null, cb); | ||
that._complete(undefined, cb); | ||
}); | ||
@@ -111,3 +111,3 @@ | ||
this.emit("already-run"); | ||
this._complete(null, cb); | ||
this._complete(undefined, cb); | ||
}; | ||
@@ -118,4 +118,2 @@ | ||
if (err) { | ||
console.log("ERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"); | ||
console.log(err.stack); | ||
this.emit("error", err); | ||
@@ -122,0 +120,0 @@ } |
{ | ||
"name": "migroose", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "migration framework for mongoosejs and MongoDB", | ||
@@ -5,0 +5,0 @@ "main": "migroose/index.js", |
@@ -25,3 +25,3 @@ var _ = require("underscore"); | ||
describe("when specifying a collection to drop", function(){ | ||
xdescribe("when specifying a collection to drop", function(){ | ||
var async = new AsyncSpec(this); | ||
@@ -59,3 +59,25 @@ var things; | ||
describe("when trying to drop a collection that doesn't exist", function(){ | ||
var async = new AsyncSpec(this); | ||
var things; | ||
var err; | ||
async.beforeEach(function(done){ | ||
var migration = new Migroose.Migration(); | ||
migration.drop("i-dont-exist"); | ||
migration.migrate(function(e){ | ||
err = e; | ||
if (err) { console.log(err.stack); } | ||
done(); | ||
}); | ||
}); | ||
async.it("should not throw an error", function(done){ | ||
expect(err).toBe(undefined); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
31875
787