Skip to content

Commit d175800

Browse files
committed
Some cleanup for deleting one schema
1 parent 0d09476 commit d175800

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ export class MongoStorageAdapter {
7070
});
7171
}
7272

73-
dropCollection(className: string) {
73+
// Deletes a schema. Resolve if successful. If the schema doesn't
74+
// exist, resolve with undefined. If schema exists, but can't be deleted for some other reason,
75+
// reject with INTERNAL_SERVER_ERROR.
76+
deleteOneSchema(className: string) {
7477
return this.collection(this._collectionPrefix + className).then(collection => collection.drop())
7578
.catch(error => {
7679
// 'ns not found' means collection was already gone. Ignore deletion attempt.
@@ -81,15 +84,24 @@ export class MongoStorageAdapter {
8184
});
8285
}
8386

84-
// Used for testing only right now.
85-
allCollections() {
86-
return this.connect().then(() => {
87-
return this.database.collections();
88-
}).then(collections => {
87+
// Delete all data known to this adatper. Used for testing.
88+
deleteAllSchemas() {
89+
return this._allCollections().then(collections => {
90+
let promises = collections.map(collection => collection.drop());
91+
return Promise.all(promises);
92+
});
93+
}
94+
95+
_allCollections() {
96+
return this.connect()
97+
.then(() => this.database.collections())
98+
.then(collections => {
8999
return collections.filter(collection => {
90100
if (collection.namespace.match(/\.system\./)) {
91101
return false;
92102
}
103+
//TODO: If you have one app with a collection prefix that happens to be a prefix of another
104+
// apps prefix, this will go very very badly. We should fix that somehow.
93105
return (collection.collectionName.indexOf(this._collectionPrefix) == 0);
94106
});
95107
});

src/Controllers/DatabaseController.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,7 @@ DatabaseController.prototype.mongoFind = function(className, query, options = {}
363363
// Returns a promise.
364364
DatabaseController.prototype.deleteEverything = function() {
365365
this.schemaPromise = null;
366-
367-
return this.adapter.allCollections().then(collections => {
368-
let promises = collections.map(collection => collection.drop());
369-
return Promise.all(promises);
370-
});
366+
return this.adapter.deleteAllSchemas();
371367
};
372368

373369
// Finds the keys in a query. Returns a Set. REST format only
@@ -634,21 +630,19 @@ DatabaseController.prototype.find = function(className, query, {
634630

635631
DatabaseController.prototype.deleteSchema = function(className) {
636632
return this.collectionExists(className)
637-
.then(exist => {
638-
if (!exist) {
639-
return Promise.resolve();
633+
.then(exist => {
634+
if (!exist) {
635+
return Promise.resolve();
636+
}
637+
return this.adapter.adaptiveCollection(className)
638+
.then(collection => collection.count())
639+
.then(count => {
640+
if (count > 0) {
641+
throw new Parse.Error(255, `Class ${className} is not empty, contains ${count} objects, cannot drop schema.`);
640642
}
641-
return this.adapter.adaptiveCollection(className)
642-
.then(collection => {
643-
return collection.count()
644-
.then(count => {
645-
if (count > 0) {
646-
throw new Parse.Error(255, `Class ${className} is not empty, contains ${count} objects, cannot drop schema.`);
647-
}
648-
return collection.drop();
649-
})
650-
})
643+
return this.adapter.deleteOneSchema(className);
651644
})
645+
});
652646
}
653647

654648
DatabaseController.prototype.addPointerPermissions = function(schema, className, operation, query, aclGroup = []) {

src/Controllers/SchemaController.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function validateCLP(perms, fields) {
134134
}
135135
return;
136136
}
137-
137+
138138
Object.keys(perms[operation]).forEach((key) => {
139139
verifyPermissionKey(key);
140140
let perm = perms[operation][key];
@@ -543,7 +543,7 @@ class SchemaController {
543543
if (this.data[className][fieldName].type == 'Relation') {
544544
//For relations, drop the _Join table
545545
return database.adapter.deleteFields(className, [fieldName], [])
546-
.then(() => database.adapter.dropCollection(`_Join:${fieldName}:${className}`));
546+
.then(() => database.adapter.deleteOneSchema(`_Join:${fieldName}:${className}`));
547547
}
548548

549549
const fieldNames = [fieldName];
@@ -632,15 +632,15 @@ class SchemaController {
632632
found = true;
633633
}
634634
}
635-
635+
636636
if (found) {
637637
return Promise.resolve();
638638
}
639639

640640
// No matching CLP, let's check the Pointer permissions
641641
// And handle those later
642642
let permissionField = ['get', 'find'].indexOf(operation) > -1 ? 'readUserFields' : 'writeUserFields';
643-
643+
644644
// Reject create when write lockdown
645645
if (permissionField == 'writeUserFields' && operation == 'create') {
646646
throw new Parse.Error(Parse.Error.OPERATION_FORBIDDEN,

src/Routers/SchemasRouter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ var removeJoinTables = (database, mongoSchema) => {
7070
.filter(field => mongoSchema[field].startsWith('relation<'))
7171
.map(field => {
7272
let collectionName = `_Join:${field}:${mongoSchema._id}`;
73-
return database.adapter.dropCollection(collectionName);
73+
return database.adapter.deleteOneSchema(collectionName);
7474
})
7575
);
7676
};

0 commit comments

Comments
 (0)