Skip to content

Commit f41c1d2

Browse files
committed
Manully pre-checkl if project exists before deleltion
1 parent 303a4fd commit f41c1d2

3 files changed

Lines changed: 161 additions & 146 deletions

File tree

.travis.yml

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/server/storage/mongo.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ var mongodb = require('mongodb'),
1616
CANON = requireJS('common/util/canon'),
1717
REGEXP = requireJS('common/regexp');
1818

19+
function projectExists(db, projectId) {
20+
var deferred = Q.defer();
21+
const collection = db.collection(projectId, { strict: true });
22+
// Check if collection is empty
23+
collection.findOne({})
24+
.then(function (doc) {
25+
deferred.resolve(doc !== null);
26+
})
27+
.catch(function (err) {
28+
deferred.reject(err);
29+
});
30+
31+
return deferred.promise;
32+
}
33+
1934
function Mongo(mainLogger, gmeConfig) {
2035
var self = this,
2136
connectionCnt = 0,
@@ -58,7 +73,7 @@ function Mongo(mainLogger, gmeConfig) {
5873
deferred.reject(new Error('loadObject - invalid hash :' + hash));
5974
} else {
6075
logger.debug('loadObject ' + hash);
61-
collection.findOne({_id: hash}).then(function (obj) {
76+
collection.findOne({ _id: hash }).then(function (obj) {
6277
if (obj) {
6378
deferred.resolve(obj);
6479
} else {
@@ -91,7 +106,7 @@ function Mongo(mainLogger, gmeConfig) {
91106
.catch(function (err) {
92107
// manually check duplicate keys
93108
if (err && err.code === 11000) {
94-
collection.findOne({_id: object._id})
109+
collection.findOne({ _id: object._id })
95110
.then(function (data) {
96111
if (CANON.stringify(object) === CANON.stringify(data)) {
97112
logger.debug('tried to insert existing hash - the two objects were equal',
@@ -141,7 +156,7 @@ function Mongo(mainLogger, gmeConfig) {
141156
this.getBranchHash = function (branch, callback) {
142157
branch = '*' + branch;
143158

144-
return Q(collection.findOne({_id: branch}))
159+
return Q(collection.findOne({ _id: branch }))
145160
.then(function (branchObj) {
146161
// FIXME: This behaviour of return empty string rather than an error is the same as before.
147162
// FIXME: Consider returning with an error in style with 'Branch does not exist'.
@@ -154,7 +169,7 @@ function Mongo(mainLogger, gmeConfig) {
154169
branch = '*' + branch;
155170

156171
if (oldhash === newhash) {
157-
collection.findOne({_id: branch})
172+
collection.findOne({ _id: branch })
158173
.then(function (obj) {
159174
if (oldhash !== ((obj && obj.hash) || '')) {
160175
deferred.reject(new Error('branch hash mismatch'));
@@ -172,7 +187,7 @@ function Mongo(mainLogger, gmeConfig) {
172187
})
173188
.then(function (result) {
174189
if (result.deletedCount !== 1) {
175-
return collection.findOne({_id: branch})
190+
return collection.findOne({ _id: branch })
176191
.then(function (obj) {
177192
if (obj) {
178193
throw new Error('branch hash mismatch');
@@ -259,7 +274,7 @@ function Mongo(mainLogger, gmeConfig) {
259274

260275
update.$set[name] = commitHash;
261276

262-
collection.updateOne(query, update, {upsert: true})
277+
collection.updateOne(query, update, { upsert: true })
263278
.then(function () {
264279
deferred.resolve();
265280
})
@@ -298,7 +313,7 @@ function Mongo(mainLogger, gmeConfig) {
298313
this.getTags = function (callback) {
299314
var deferred = Q.defer();
300315

301-
collection.findOne({_id: self.CONSTANTS.TAGS}, {})
316+
collection.findOne({ _id: self.CONSTANTS.TAGS }, {})
302317
.then(function (result) {
303318
if (result) {
304319
delete result._id;
@@ -368,7 +383,7 @@ function Mongo(mainLogger, gmeConfig) {
368383
.catch(function (err) {
369384
self.client = null;
370385
connectionCnt -= 1;
371-
logger.error('Failed to connect.', {metadata: err});
386+
logger.error('Failed to connect.', { metadata: err });
372387
connectDeferred.reject(err);
373388
});
374389
} else {
@@ -417,9 +432,14 @@ function Mongo(mainLogger, gmeConfig) {
417432
var deferred = Q.defer();
418433

419434
if (self.db) {
420-
Q(self.db.dropCollection(projectId))
435+
let didExist = false;
436+
projectExists(self.db, projectId)
437+
.then(function (_didExist) {
438+
didExist = _didExist;
439+
return Q(self.db.dropCollection(projectId));
440+
})
421441
.then(function () {
422-
deferred.resolve(true);
442+
deferred.resolve(didExist);
423443
})
424444
.catch(function (err) {
425445
if (err.ok === 0) { // http://docs.mongodb.org/manual/reference/method/db.collection.drop/
@@ -443,15 +463,12 @@ function Mongo(mainLogger, gmeConfig) {
443463

444464
if (self.db) {
445465
try {
446-
const collection = self.db.collection(projectId, {strict: true});
447-
// Check if collection is empty
448-
collection.findOne({})
449-
.then(function (doc) {
450-
const isEmpty = doc === null;
451-
if (isEmpty) {
452-
deferred.reject(new Error('Project does not exist ' + projectId));
466+
projectExists(self.db, projectId)
467+
.then(function (didExist) {
468+
if (didExist) {
469+
deferred.resolve(new MongoProject(projectId, self.db.collection(projectId)));
453470
} else {
454-
deferred.resolve(new MongoProject(projectId, collection));
471+
deferred.reject(new Error('Project does not exist ' + projectId));
455472
}
456473
})
457474
.catch(function (err) {
@@ -475,8 +492,8 @@ function Mongo(mainLogger, gmeConfig) {
475492
if (self.db) {
476493
const collection = self.db.collection(projectId);
477494
collection.insertMany([
478-
{_id: CONSTANTS.EMPTY_PROJECT_DATA},
479-
{_id: self.CONSTANTS.TAGS}])
495+
{ _id: CONSTANTS.EMPTY_PROJECT_DATA },
496+
{ _id: self.CONSTANTS.TAGS }])
480497
.then(function () {
481498
deferred.resolve(new MongoProject(projectId, collection));
482499
})

0 commit comments

Comments
 (0)