Skip to content

Commit 9b4e283

Browse files
authored
refactoring deleteAllClasses
...to re-use the connection.
1 parent 5681b3f commit 9b4e283

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -794,25 +794,27 @@ export class PostgresStorageAdapter {
794794
// Delete all data known to this adapter. Used for testing.
795795
deleteAllClasses() {
796796
const now = new Date().getTime();
797+
const helpers = this._pgp.helpers;
797798
debug('deleteAllClasses');
798-
return this._client.any('SELECT * FROM "_SCHEMA"')
799-
.then(results => {
799+
800+
return this._client.task('delete-all-classes', function * (t) {
801+
try {
802+
const results = yield t.any('SELECT * FROM "_SCHEMA"');
800803
const joins = results.reduce((list, schema) => {
801804
return list.concat(joinTablesForSchema(schema.schema));
802805
}, []);
803806
const classes = ['_SCHEMA', '_PushStatus', '_JobStatus', '_JobSchedule', '_Hooks', '_GlobalConfig', '_Audience', ...results.map(result => result.className), ...joins];
804807
const queries = classes.map(className => ({query: 'DROP TABLE IF EXISTS $<className:name>', values: {className}}));
805-
return this._client.tx(t => t.none(this._pgp.helpers.concat(queries)));
806-
}, error => {
807-
if (error.code === PostgresRelationDoesNotExistError) {
808-
// No _SCHEMA collection. Don't delete anything.
809-
return;
810-
} else {
808+
yield t.tx(tx => tx.none(helpers.concat(queries)));
809+
} catch(error) {
810+
if (error.code !== PostgresRelationDoesNotExistError) {
811811
throw error;
812812
}
813-
}).then(() => {
813+
// No _SCHEMA collection. Don't delete anything.
814+
}
815+
}).then(() => {
814816
debug(`deleteAllClasses done in ${new Date().getTime() - now}`);
815-
});
817+
});
816818
}
817819

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

0 commit comments

Comments
 (0)