From 8bf618be9b3cdcf0cdd14e1cc2be31f8409605a5 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Thu, 28 Dec 2017 12:57:20 +0000 Subject: [PATCH] Rewriting method createClass So it would make sense in how queries are handled, how their results are handled and how errors are processed. It was originally a bit of a logical mess, also using results / error handling that wasn't clear, and not always reliable. --- .../Postgres/PostgresStorageAdapter.js | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index f7ab358194..4ae8e0623b 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -662,24 +662,33 @@ export class PostgresStorageAdapter { createClass(className, schema, conn) { conn = conn || this._client; - return conn.tx('create-class', t => { - const q1 = this.createTable(className, schema, t); - const q2 = t.none('INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($, $, true)', { className, schema }); - const q3 = this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields, t); - return t.batch([q1, q2, q3]); - }) - .then(() => { - return toParseSchema(schema); - }) - .catch(err => { - if (err.data[0].result.code === PostgresTransactionAbortedError) { - err = err.data[1].result; + const self = this; + + return conn.tx('create-class', function * (t) { + let error; + try { + yield self.createTable(className, schema, t); + } catch(e) { + if (e.code === PostgresTransactionAbortedError) { + error = e; } - if (err.code === PostgresUniqueIndexViolationError && err.detail.includes(className)) { + } + try { + yield t.none('INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($, $, true)', { className, schema }); + } catch(e) { + error = e; + } + if (error) { + if (error.code === PostgresUniqueIndexViolationError && error.detail.includes(className)) { throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, `Class ${className} already exists.`) } - throw err; - }) + throw error; + } + yield self.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields, t); + }) + .then(() => { + return toParseSchema(schema); + }); } // Just create a table, do not insert in schema