Skip to content

refactoring deleteAllClasses #4464

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 3 commits into from
Dec 27, 2017
Merged
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
20 changes: 11 additions & 9 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,23 +794,25 @@ export class PostgresStorageAdapter {
// Delete all data known to this adapter. Used for testing.
deleteAllClasses() {
const now = new Date().getTime();
const helpers = this._pgp.helpers;
debug('deleteAllClasses');
return this._client.any('SELECT * FROM "_SCHEMA"')
.then(results => {

return this._client.task('delete-all-classes', function * (t) {
try {
const results = yield t.any('SELECT * FROM "_SCHEMA"');
const joins = results.reduce((list, schema) => {
return list.concat(joinTablesForSchema(schema.schema));
}, []);
const classes = ['_SCHEMA', '_PushStatus', '_JobStatus', '_JobSchedule', '_Hooks', '_GlobalConfig', '_Audience', ...results.map(result => result.className), ...joins];
const queries = classes.map(className => ({query: 'DROP TABLE IF EXISTS $<className:name>', values: {className}}));
return this._client.tx(t => t.none(this._pgp.helpers.concat(queries)));
}, error => {
if (error.code === PostgresRelationDoesNotExistError) {
// No _SCHEMA collection. Don't delete anything.
return;
} else {
yield t.tx(tx => tx.none(helpers.concat(queries)));
} catch(error) {
if (error.code !== PostgresRelationDoesNotExistError) {
throw error;
}
}).then(() => {
// No _SCHEMA collection. Don't delete anything.
}
}).then(() => {
debug(`deleteAllClasses done in ${new Date().getTime() - now}`);
});
}
Expand Down