Skip to content

Commit 415ee3a

Browse files
authored
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 a2b2f18 commit 415ee3a

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ export class PostgresStorageAdapter {
613613

614614
setIndexesWithSchemaFormat(className, submittedIndexes, existingIndexes = {}, fields, conn) {
615615
conn = conn || this._client;
616+
const self = this;
616617
if (submittedIndexes === undefined) {
617618
return Promise.resolve();
618619
}
@@ -645,22 +646,15 @@ export class PostgresStorageAdapter {
645646
});
646647
}
647648
});
648-
let insertPromise = Promise.resolve();
649-
if (insertedIndexes.length > 0) {
650-
insertPromise = this.createIndexes(className, insertedIndexes, conn);
651-
}
652-
let deletePromise = Promise.resolve();
653-
if (deletedIndexes.length > 0) {
654-
deletePromise = this.dropIndexes(className, deletedIndexes, conn);
655-
}
656-
return conn.task(t => {
657-
const values = [className, 'schema', 'indexes', JSON.stringify(existingIndexes)];
658-
return t.batch([
659-
deletePromise,
660-
insertPromise,
661-
this._ensureSchemaCollectionExists(t),
662-
t.none('UPDATE "_SCHEMA" SET $2:name = json_object_set_key($2:name, $3::text, $4::jsonb) WHERE "className"=$1', values)
663-
]);
649+
return conn.tx('set-indexes-with-schema-format', function * (t) {
650+
if (insertedIndexes.length > 0) {
651+
yield self.createIndexes(className, insertedIndexes, t);
652+
}
653+
if (deletedIndexes.length > 0) {
654+
yield self.dropIndexes(className, deletedIndexes, t);
655+
}
656+
yield self._ensureSchemaCollectionExists(t);
657+
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)]);
664658
});
665659
}
666660

0 commit comments

Comments
 (0)