Skip to content

Commit 23e1819

Browse files
committed
Reverts to class by class deletion, introduced fast mode that just delete data for mongo
- Speeds up are incredible Executed 1695 of 1713 specs INCOMPLETE (18 PENDING) in 4 mins 19 secs.
1 parent df2bd70 commit 23e1819

File tree

8 files changed

+29
-16
lines changed

8 files changed

+29
-16
lines changed

spec/MongoStorageAdapter.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const databaseURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDataba
99
describe_only_db('mongo')('MongoStorageAdapter', () => {
1010
beforeEach(done => {
1111
new MongoStorageAdapter({ uri: databaseURI })
12-
.dropDatabase()
12+
.deleteAllClasses()
1313
.then(done, fail);
1414
});
1515

spec/PostgresStorageAdapter.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const dropTable = (client, className) => {
1212
describe_only_db('postgres')('PostgresStorageAdapter', () => {
1313
const adapter = new PostgresStorageAdapter({ uri: databaseURI })
1414
beforeEach(() => {
15-
return adapter.dropDatabase();
15+
return adapter.deleteAllClasses();
1616
});
1717

1818
it('schemaUpgrade, upgrade the database schema when schema changes', done => {

spec/helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ beforeEach(done => {
172172
throw error;
173173
}
174174
}
175-
TestUtils.destroyAllDataPermanently()
175+
TestUtils.destroyAllDataPermanently(true)
176176
.catch(error => {
177177
// For tests that connect to their own mongo, there won't be any data to delete.
178178
if (error.message === 'ns not found' || error.message.startsWith('connect ECONNREFUSED')) {
@@ -196,7 +196,7 @@ afterEach(function(done) {
196196
fail('There were open connections to the server left after the test finished');
197197
}
198198
on_db('postgres', () => {
199-
TestUtils.destroyAllDataPermanently().then(done, done);
199+
TestUtils.destroyAllDataPermanently(true).then(done, done);
200200
}, done);
201201
};
202202
Parse.Cloud._removeAllHooks();

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ const MongoClient = mongodb.MongoClient;
3131
const ReadPreference = mongodb.ReadPreference;
3232

3333
const MongoSchemaCollectionName = '_SCHEMA';
34+
35+
const storageAdapterAllCollections = mongoAdapter => {
36+
return mongoAdapter.connect()
37+
.then(() => mongoAdapter.database.collections())
38+
.then(collections => {
39+
return collections.filter(collection => {
40+
if (collection.namespace.match(/\.system\./)) {
41+
return false;
42+
}
43+
// TODO: If you have one app with a collection prefix that happens to be a prefix of another
44+
// apps prefix, this will go very very badly. We should fix that somehow.
45+
return (collection.collectionName.indexOf(mongoAdapter._collectionPrefix) == 0);
46+
});
47+
});
48+
}
49+
3450
const convertParseSchemaToMongoSchema = ({...schema}) => {
3551
delete schema.fields._rperm;
3652
delete schema.fields._wperm;
@@ -297,9 +313,10 @@ export class MongoStorageAdapter implements StorageAdapter {
297313
.catch(err => this.handleError(err));
298314
}
299315

300-
dropDatabase() {
301-
if (!this.database) { return Promise.resolve(); }
302-
return this.database.dropDatabase();
316+
deleteAllClasses(fast: boolean) {
317+
return storageAdapterAllCollections(this)
318+
.then(collections => Promise.all(collections.map(collection => fast ? collection.remove({}) : collection.drop())))
319+
.catch(err => this.handleError(err));
303320
}
304321

305322
// Remove the column and all the data. For Relations, the _Join collection is handled

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -959,10 +959,6 @@ export class PostgresStorageAdapter implements StorageAdapter {
959959
});
960960
}
961961

962-
dropDatabase() {
963-
return this.deleteAllClasses();
964-
}
965-
966962
// Remove the column and all the data. For Relations, the _Join collection is handled
967963
// specially, this function does not delete _Join columns. It should, however, indicate
968964
// that the relation fields does not exist anymore. In mongo, this means removing it from

src/Adapters/Storage/StorageAdapter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface StorageAdapter {
3131
createClass(className: string, schema: SchemaType): Promise<void>;
3232
addFieldIfNotExists(className: string, fieldName: string, type: any): Promise<void>;
3333
deleteClass(className: string): Promise<void>;
34-
dropDatabase(): Promise<void>;
34+
deleteAllClasses(fast: boolean): Promise<void>;
3535
deleteFields(className: string, schema: SchemaType, fieldNames: Array<string>): Promise<void>;
3636
getAllClasses(): Promise<StorageClass[]>;
3737
getClass(className: string): Promise<StorageClass>;

src/Controllers/DatabaseController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,10 @@ class DatabaseController {
655655

656656
// Won't delete collections in the system namespace
657657
// Returns a promise.
658-
deleteEverything() {
658+
deleteEverything(fast: boolean = false) {
659659
this.schemaPromise = null;
660660
return Promise.all([
661-
this.adapter.dropDatabase(),
661+
this.adapter.deleteAllClasses(fast),
662662
this.schemaCache.clear()
663663
]);
664664
}

src/TestUtils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import AppCache from './cache';
22

33
//Used by tests
4-
export function destroyAllDataPermanently() {
4+
export function destroyAllDataPermanently(fast) {
55
if (!process.env.TESTING) {
66
throw 'Only supported in test environment';
77
}
88
return Promise.all(Object.keys(AppCache.cache).map(appId => {
99
const app = AppCache.get(appId);
1010
if (app.databaseController) {
11-
return app.databaseController.deleteEverything();
11+
return app.databaseController.deleteEverything(fast);
1212
} else {
1313
return Promise.resolve();
1414
}

0 commit comments

Comments
 (0)