From 415ee3a1fd51c30fd1c70f191f804c1139b96d98 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 24 Dec 2017 19:03:35 +0000 Subject: [PATCH] fixing method setIndexesWithSchemaFormat (#4454) Fixing invalid database code in method `setIndexesWithSchemaFormat`: * It must be a transaction, not a task, as it executes multiple database changes * It should contain the initial queries inside the transaction, providing the context, not outside it; * Replaced the code with the ES6 Generator notation * Removing the use of batch, as the value of the result promise is irrelevant, only success/failure that matters --- .../Postgres/PostgresStorageAdapter.js | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 0b7be499..1cc61c2a 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -613,6 +613,7 @@ export class PostgresStorageAdapter { setIndexesWithSchemaFormat(className, submittedIndexes, existingIndexes = {}, fields, conn) { conn = conn || this._client; + const self = this; if (submittedIndexes === undefined) { return Promise.resolve(); } @@ -645,22 +646,15 @@ export class PostgresStorageAdapter { }); } }); - let insertPromise = Promise.resolve(); - if (insertedIndexes.length > 0) { - insertPromise = this.createIndexes(className, insertedIndexes, conn); - } - let deletePromise = Promise.resolve(); - if (deletedIndexes.length > 0) { - deletePromise = this.dropIndexes(className, deletedIndexes, conn); - } - return conn.task(t => { - const values = [className, 'schema', 'indexes', JSON.stringify(existingIndexes)]; - return t.batch([ - deletePromise, - insertPromise, - this._ensureSchemaCollectionExists(t), - t.none('UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1', values) - ]); + return conn.tx('set-indexes-with-schema-format', function * (t) { + if (insertedIndexes.length > 0) { + yield self.createIndexes(className, insertedIndexes, t); + } + if (deletedIndexes.length > 0) { + yield self.dropIndexes(className, deletedIndexes, t); + } + yield self._ensureSchemaCollectionExists(t); + yield t.none('UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1', [className, 'schema', 'indexes', JSON.stringify(existingIndexes)]); }); }