Skip to content

Commit dbf6d60

Browse files
vitaly-tflovilmart
authored andcommitted
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
1 parent 171870e commit dbf6d60

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,9 @@ export class PostgresStorageAdapter implements StorageAdapter {
618618
});
619619
}
620620

621-
setIndexesWithSchemaFormat(className: string, submittedIndexes: any, existingIndexes: any = {}, fields: any, client: ?any): Promise<void> {
622-
const conn = client || this._client;
621+
setIndexesWithSchemaFormat(className: string, submittedIndexes: any, existingIndexes: any = {}, fields: any, conn: ?any): Promise<void> {
622+
conn = conn || this._client;
623+
const self = this;
623624
if (submittedIndexes === undefined) {
624625
return Promise.resolve();
625626
}
@@ -652,22 +653,15 @@ export class PostgresStorageAdapter implements StorageAdapter {
652653
});
653654
}
654655
});
655-
let insertPromise = Promise.resolve();
656-
if (insertedIndexes.length > 0) {
657-
insertPromise = this.createIndexes(className, insertedIndexes, conn);
658-
}
659-
let deletePromise = Promise.resolve();
660-
if (deletedIndexes.length > 0) {
661-
deletePromise = this.dropIndexes(className, deletedIndexes, conn);
662-
}
663-
return conn.task(t => {
664-
const values = [className, 'schema', 'indexes', JSON.stringify(existingIndexes)];
665-
return t.batch([
666-
deletePromise,
667-
insertPromise,
668-
this._ensureSchemaCollectionExists(t),
669-
t.none('UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1', values)
670-
]);
656+
return conn.tx('set-indexes-with-schema-format', function * (t) {
657+
if (insertedIndexes.length > 0) {
658+
yield self.createIndexes(className, insertedIndexes, t);
659+
}
660+
if (deletedIndexes.length > 0) {
661+
yield self.dropIndexes(className, deletedIndexes, t);
662+
}
663+
yield self._ensureSchemaCollectionExists(t);
664+
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)]);
671665
});
672666
}
673667

0 commit comments

Comments
 (0)