Skip to content

Commit 836b768

Browse files
vitaly-tflovilmart
authored andcommitted
refactoring deleteAllClasses (#4464)
* refactoring deleteAllClasses ...to re-use the connection. * Update PostgresStorageAdapter.js removing extra spaces.
1 parent 26fb4e6 commit 836b768

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -801,23 +801,25 @@ export class PostgresStorageAdapter implements StorageAdapter {
801801
// Delete all data known to this adapter. Used for testing.
802802
deleteAllClasses() {
803803
const now = new Date().getTime();
804+
const helpers = this._pgp.helpers;
804805
debug('deleteAllClasses');
805-
return this._client.any('SELECT * FROM "_SCHEMA"')
806-
.then(results => {
806+
807+
return this._client.task('delete-all-classes', function * (t) {
808+
try {
809+
const results = yield t.any('SELECT * FROM "_SCHEMA"');
807810
const joins = results.reduce((list, schema) => {
808811
return list.concat(joinTablesForSchema(schema.schema));
809812
}, []);
810813
const classes = ['_SCHEMA', '_PushStatus', '_JobStatus', '_JobSchedule', '_Hooks', '_GlobalConfig', '_Audience', ...results.map(result => result.className), ...joins];
811814
const queries = classes.map(className => ({query: 'DROP TABLE IF EXISTS $<className:name>', values: {className}}));
812-
return this._client.tx(t => t.none(this._pgp.helpers.concat(queries)));
813-
}, error => {
814-
if (error.code === PostgresRelationDoesNotExistError) {
815-
// No _SCHEMA collection. Don't delete anything.
816-
return;
817-
} else {
815+
yield t.tx(tx => tx.none(helpers.concat(queries)));
816+
} catch(error) {
817+
if (error.code !== PostgresRelationDoesNotExistError) {
818818
throw error;
819819
}
820-
}).then(() => {
820+
// No _SCHEMA collection. Don't delete anything.
821+
}
822+
}).then(() => {
821823
debug(`deleteAllClasses done in ${new Date().getTime() - now}`);
822824
});
823825
}

0 commit comments

Comments
 (0)