Skip to content

Commit

Permalink
Update PostgresStorageAdapter.js (#6989)
Browse files Browse the repository at this point in the history
* Update PostgresStorageAdapter.js

Improve `createClass` transaction:

* `await` makes it a more consistent sequence of queries
* `batch` is not needed there
* No need for an extra `.then` section

* Update PostgresStorageAdapter.js

Remove batch-dependent error code check, as it should happen automatically without batch result.

* Update PostgresStorageAdapter.js

Removing unused variable.
  • Loading branch information
vitaly-t authored Nov 3, 2020
1 parent c983202 commit bbae55d
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const PostgresDuplicateColumnError = '42701';
const PostgresMissingColumnError = '42703';
const PostgresDuplicateObjectError = '42710';
const PostgresUniqueIndexViolationError = '23505';
const PostgresTransactionAbortedError = '25P02';
const logger = require('../../../logger');

const debug = function (...args: any) {
Expand Down Expand Up @@ -986,29 +985,21 @@ export class PostgresStorageAdapter implements StorageAdapter {
conn = conn || this._client;
return conn
.tx('create-class', async t => {
const q1 = this.createTable(className, schema, t);
const q2 = t.none(
await this.createTable(className, schema, t);
await t.none(
'INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)',
{ className, schema }
);
const q3 = this.setIndexesWithSchemaFormat(
await this.setIndexesWithSchemaFormat(
className,
schema.indexes,
{},
schema.fields,
t
);
// TODO: The test should not verify the returned value, and then
// the method can be simplified, to avoid returning useless stuff.
return t.batch([q1, q2, q3]);
})
.then(() => {
return toParseSchema(schema);
})
.catch(err => {
if (err.data[0].result.code === PostgresTransactionAbortedError) {
err = err.data[1].result;
}
if (
err.code === PostgresUniqueIndexViolationError &&
err.detail.includes(className)
Expand Down

0 comments on commit bbae55d

Please sign in to comment.