Skip to content

Commit 5681b3f

Browse files
authored
Refactoring method addFieldIfNotExists (#4461)
* Refactoring method `addFieldIfNotExists` * Using ES6 Generators syntax * Passing in the context into method `createClass`, to reuse the connection * Extending method `createClass` to reuse connections * Update PostgresStorageAdapter.js forgot: extending method `createClass` to reuse the connection. * Update PostgresStorageAdapter.js fixing the re-throw logic.
1 parent 820ae2b commit 5681b3f

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,9 @@ export class PostgresStorageAdapter {
660660
});
661661
}
662662

663-
createClass(className, schema) {
664-
return this._client.tx('create-class', t => {
663+
createClass(className, schema, conn) {
664+
conn = conn || this._client;
665+
return conn.tx('create-class', t => {
665666
const q1 = this.createTable(className, schema, t);
666667
const q2 = t.none('INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)', { className, schema });
667668
const q3 = this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields, t);
@@ -746,40 +747,36 @@ export class PostgresStorageAdapter {
746747
addFieldIfNotExists(className, fieldName, type) {
747748
// TODO: Must be revised for invalid logic...
748749
debug('addFieldIfNotExists', {className, fieldName, type});
749-
return this._client.tx('add-field-if-not-exists', t => {
750-
let promise = Promise.resolve();
750+
const self = this;
751+
return this._client.tx('add-field-if-not-exists', function * (t) {
751752
if (type.type !== 'Relation') {
752-
promise = t.none('ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>', {
753-
className,
754-
fieldName,
755-
postgresType: parseTypeToPostgresType(type)
756-
})
757-
.catch(error => {
753+
try {
754+
yield t.none('ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>', {
755+
className,
756+
fieldName,
757+
postgresType: parseTypeToPostgresType(type)
758+
});
759+
} catch(error) {
758760
if (error.code === PostgresRelationDoesNotExistError) {
759-
return this.createClass(className, {fields: {[fieldName]: type}})
760-
} else if (error.code === PostgresDuplicateColumnError) {
761-
// Column already exists, created by other request. Carry on to
762-
// See if it's the right type.
763-
} else {
761+
return yield self.createClass(className, {fields: {[fieldName]: type}}, t);
762+
}
763+
if (error.code !== PostgresDuplicateColumnError) {
764764
throw error;
765765
}
766-
})
766+
// Column already exists, created by other request. Carry on to see if it's the right type.
767+
};
767768
} else {
768-
promise = t.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`})
769+
yield t.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
770+
}
771+
772+
const result = yield t.any('SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className> and ("schema"::json->\'fields\'->$<fieldName>) is not null', {className, fieldName});
773+
774+
if (result[0]) {
775+
throw 'Attempted to add a field that already exists';
776+
} else {
777+
const path = `{fields,${fieldName}}`;
778+
yield t.none('UPDATE "_SCHEMA" SET "schema"=jsonb_set("schema", $<path>, $<type>) WHERE "className"=$<className>', {path, type, className});
769779
}
770-
return promise.then(() => {
771-
return t.any('SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className> and ("schema"::json->\'fields\'->$<fieldName>) is not null', {className, fieldName});
772-
}).then(result => {
773-
if (result[0]) {
774-
throw "Attempted to add a field that already exists";
775-
} else {
776-
const path = `{fields,${fieldName}}`;
777-
return t.none(
778-
'UPDATE "_SCHEMA" SET "schema"=jsonb_set("schema", $<path>, $<type>) WHERE "className"=$<className>',
779-
{ path, type, className }
780-
);
781-
}
782-
});
783780
});
784781
}
785782

0 commit comments

Comments
 (0)