Skip to content

Commit e0e706c

Browse files
vitaly-tflovilmart
authored andcommitted
Refactoring method createTable (#4456)
* Refactoring method createTable Replacing the weird task + transaction chain, by replacing it with just one transaction that encapsulates the complete logic. * Update PostgresStorageAdapter.js correcting the sequence to match the original exactly. * Update PostgresStorageAdapter.js Nesting the transaction inside a task, so it can execute successfully no matter if the containing task succeeds or fails. * Update PostgresStorageAdapter.js adding the missing bracket.
1 parent a295c40 commit e0e706c

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
691691
// Just create a table, do not insert in schema
692692
createTable(className: string, schema: SchemaType, conn: any) {
693693
conn = conn || this._client;
694+
const self = this;
694695
debug('createTable', className, schema);
695696
const valuesArray = [];
696697
const patternsArray = [];
@@ -728,24 +729,23 @@ export class PostgresStorageAdapter implements StorageAdapter {
728729
});
729730
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join(',')})`;
730731
const values = [className, ...valuesArray];
731-
return conn.task(t => {
732-
return this._ensureSchemaCollectionExists(t)
733-
.then(() => t.none(qs, values))
734-
.catch(error => {
735-
if (error.code === PostgresDuplicateRelationError) {
736-
// Table already exists, must have been created by a different request. Ignore error.
737-
} else {
732+
733+
return conn.task('create-table', function * (t) {
734+
try {
735+
yield self._ensureSchemaCollectionExists(t);
736+
yield t.none(qs, values);
737+
} catch(error) {
738+
if (error.code !== PostgresDuplicateRelationError) {
738739
throw error;
739-
}})
740-
})
741-
.then(() => {
742-
return conn.tx('create-relation-tables', t => {
743-
const queries = relations.map((fieldName) => {
744-
return t.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
745-
});
746-
return t.batch(queries);
747-
});
740+
}
741+
// ELSE: Table already exists, must have been created by a different request. Ignore the error.
742+
}
743+
yield t.tx('create-table-tx', tx => {
744+
return tx.batch(relations.map(fieldName => {
745+
return tx.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
746+
}));
748747
});
748+
});
749749
}
750750

751751
addFieldIfNotExists(className: string, fieldName: string, type: any) {

0 commit comments

Comments
 (0)