Skip to content

improve field deletion in collection #6823

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

Merged
merged 4 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### master
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.3.0...master)
- IMPROVE: Optimized deletion of class field from schema by using an index if available to do an index scan instead of a collection scan. [#6815](https://github.com/parse-community/parse-server/issues/6815). Thanks to [Manuel Trezza](https://github.com/mtrezza).

### 4.3.0
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.2.0...4.3.0)
Expand Down
37 changes: 37 additions & 0 deletions spec/MongoStorageAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,43 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
expect(postIndexPlan.executionStats.executionStages.stage).toBe('FETCH');
});

it('should delete field without index', async () => {
const database = Config.get(Parse.applicationId).database;
const obj = new Parse.Object('MyObject');
obj.set("test", 1);
await obj.save();
const schemaBeforeDeletion = await new Parse.Schema('MyObject').get();
await database.adapter.deleteFields(
"MyObject",
schemaBeforeDeletion,
["test"]
);
const schemaAfterDeletion = await new Parse.Schema('MyObject').get();
expect(schemaBeforeDeletion.fields.test).toBeDefined();
expect(schemaAfterDeletion.fields.test).toBeUndefined();
});

it('should delete field with index', async () => {
const database = Config.get(Parse.applicationId).database;
const obj = new Parse.Object('MyObject');
obj.set("test", 1);
await obj.save();
const schemaBeforeDeletion = await new Parse.Schema('MyObject').get();
await database.adapter.ensureIndex(
'MyObject',
schemaBeforeDeletion,
['test']
);
await database.adapter.deleteFields(
"MyObject",
schemaBeforeDeletion,
["test"]
);
const schemaAfterDeletion = await new Parse.Schema('MyObject').get();
expect(schemaBeforeDeletion.fields.test).toBeDefined();
expect(schemaAfterDeletion.fields.test).toBeUndefined();
});

if (
semver.satisfies(process.env.MONGODB_VERSION, '>=4.0.4') &&
process.env.MONGODB_TOPOLOGY === 'replicaset' &&
Expand Down
7 changes: 6 additions & 1 deletion src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,19 @@ export class MongoStorageAdapter implements StorageAdapter {
collectionUpdate['$unset'][name] = null;
});

const collectionFilter = { $or: [] };
mongoFormatNames.forEach(name => {
collectionFilter['$or'].push({ [name]: { $exists: true } });
});

const schemaUpdate = { $unset: {} };
fieldNames.forEach((name) => {
schemaUpdate['$unset'][name] = null;
schemaUpdate['$unset'][`_metadata.fields_options.${name}`] = null;
});

return this._adaptiveCollection(className)
.then((collection) => collection.updateMany({}, collectionUpdate))
.then((collection) => collection.updateMany(collectionFilter, collectionUpdate))
.then(() => this._schemaCollection())
.then((schemaCollection) =>
schemaCollection.updateSchema(className, schemaUpdate)
Expand Down