diff --git a/spec/schemas.spec.js b/spec/schemas.spec.js index 24589a3865..6bb65023c1 100644 --- a/spec/schemas.spec.js +++ b/spec/schemas.spec.js @@ -744,4 +744,75 @@ describe('schemas', () => { done(); }); }); + + it('deletes schema when actual collection does not exist', done => { + request.post({ + url: 'http://localhost:8378/1/schemas/NewClassForDelete', + headers: masterKeyHeaders, + json: true, + body: { + className: 'NewClassForDelete' + } + }, (error, response, body) => { + expect(error).toEqual(null); + expect(response.body.className).toEqual('NewClassForDelete'); + request.del({ + url: 'http://localhost:8378/1/schemas/NewClassForDelete', + headers: masterKeyHeaders, + json: true, + }, (error, response, body) => { + expect(response.statusCode).toEqual(200); + expect(response.body).toEqual({}); + config.database.loadSchema().then(schema => { + schema.hasClass('NewClassForDelete').then(exist => { + expect(exist).toEqual(false); + done(); + }); + }) + }); + }); + }); + + it('deletes schema when actual collection exists', done => { + request.post({ + url: 'http://localhost:8378/1/schemas/NewClassForDelete', + headers: masterKeyHeaders, + json: true, + body: { + className: 'NewClassForDelete' + } + }, (error, response, body) => { + expect(error).toEqual(null); + expect(response.body.className).toEqual('NewClassForDelete'); + request.post({ + url: 'http://localhost:8378/1/classes/NewClassForDelete', + headers: restKeyHeaders, + json: true + }, (error, response, body) => { + expect(error).toEqual(null); + expect(typeof response.body.objectId).toEqual('string'); + request.del({ + url: 'http://localhost:8378/1/classes/NewClassForDelete/' + response.body.objectId, + headers: restKeyHeaders, + json: true, + }, (error, response, body) => { + expect(error).toEqual(null); + request.del({ + url: 'http://localhost:8378/1/schemas/NewClassForDelete', + headers: masterKeyHeaders, + json: true, + }, (error, response, body) => { + expect(response.statusCode).toEqual(200); + expect(response.body).toEqual({}); + config.database.loadSchema().then(schema => { + schema.hasClass('NewClassForDelete').then(exist => { + expect(exist).toEqual(false); + done(); + }); + }); + }); + }); + }); + }); + }); }); diff --git a/src/Routers/SchemasRouter.js b/src/Routers/SchemasRouter.js index 70b3157e88..1dfff74b56 100644 --- a/src/Routers/SchemasRouter.js +++ b/src/Routers/SchemasRouter.js @@ -151,14 +151,20 @@ function deleteSchema(req) { throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, Schema.invalidClassNameMessage(req.params.className)); } - return req.config.database.adaptiveCollection(req.params.className) - .then(collection => { - return collection.count() - .then(count => { - if (count > 0) { - throw new Parse.Error(255, `Class ${req.params.className} is not empty, contains ${count} objects, cannot drop schema.`); - } - return collection.drop(); + return req.config.database.collectionExists(req.params.className) + .then(exist => { + if (!exist) { + return Promise.resolve(); + } + return req.config.database.adaptiveCollection(req.params.className) + .then(collection => { + return collection.count() + .then(count => { + if (count > 0) { + throw new Parse.Error(255, `Class ${req.params.className} is not empty, contains ${count} objects, cannot drop schema.`); + } + return collection.drop(); + }) }) }) .then(() => {