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
This commit is contained in:
Vitaly Tomilov
2017-12-24 19:03:35 +00:00
committed by GitHub
parent a2b2f18927
commit 415ee3a1fd

View File

@@ -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();
return conn.tx('set-indexes-with-schema-format', function * (t) {
if (insertedIndexes.length > 0) {
insertPromise = this.createIndexes(className, insertedIndexes, conn);
yield self.createIndexes(className, insertedIndexes, t);
}
let deletePromise = Promise.resolve();
if (deletedIndexes.length > 0) {
deletePromise = this.dropIndexes(className, deletedIndexes, conn);
yield self.dropIndexes(className, deletedIndexes, t);
}
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)
]);
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)]);
});
}