Skip to content

Commit ac6a382

Browse files
vitaly-tflovilmart
authored andcommitted
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 0e68691 commit ac6a382

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
@@ -667,8 +667,9 @@ export class PostgresStorageAdapter implements StorageAdapter {
667667
});
668668
}
669669

670-
createClass(className: string, schema: SchemaType) {
671-
return this._client.tx('create-class', t => {
670+
createClass(className: string, schema: SchemaType, conn: ?any) {
671+
conn = conn || this._client;
672+
return conn.tx('create-class', t => {
672673
const q1 = this.createTable(className, schema, t);
673674
const q2 = t.none('INSERT INTO "_SCHEMA" ("className", "schema", "isParseClass") VALUES ($<className>, $<schema>, true)', { className, schema });
674675
const q3 = this.setIndexesWithSchemaFormat(className, schema.indexes, {}, schema.fields, t);
@@ -753,40 +754,36 @@ export class PostgresStorageAdapter implements StorageAdapter {
753754
addFieldIfNotExists(className: string, fieldName: string, type: any) {
754755
// TODO: Must be revised for invalid logic...
755756
debug('addFieldIfNotExists', {className, fieldName, type});
756-
return this._client.tx('add-field-if-not-exists', t => {
757-
let promise = Promise.resolve();
757+
const self = this;
758+
return this._client.tx('add-field-if-not-exists', function * (t) {
758759
if (type.type !== 'Relation') {
759-
promise = t.none('ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>', {
760-
className,
761-
fieldName,
762-
postgresType: parseTypeToPostgresType(type)
763-
})
764-
.catch(error => {
760+
try {
761+
yield t.none('ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>', {
762+
className,
763+
fieldName,
764+
postgresType: parseTypeToPostgresType(type)
765+
});
766+
} catch(error) {
765767
if (error.code === PostgresRelationDoesNotExistError) {
766-
return this.createClass(className, {fields: {[fieldName]: type}})
767-
} else if (error.code === PostgresDuplicateColumnError) {
768-
// Column already exists, created by other request. Carry on to
769-
// See if it's the right type.
770-
} else {
768+
return yield self.createClass(className, {fields: {[fieldName]: type}}, t);
769+
}
770+
if (error.code !== PostgresDuplicateColumnError) {
771771
throw error;
772772
}
773-
})
773+
// Column already exists, created by other request. Carry on to see if it's the right type.
774+
};
774775
} else {
775-
promise = t.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`})
776+
yield t.none('CREATE TABLE IF NOT EXISTS $<joinTable:name> ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`});
777+
}
778+
779+
const result = yield t.any('SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className> and ("schema"::json->\'fields\'->$<fieldName>) is not null', {className, fieldName});
780+
781+
if (result[0]) {
782+
throw 'Attempted to add a field that already exists';
783+
} else {
784+
const path = `{fields,${fieldName}}`;
785+
yield t.none('UPDATE "_SCHEMA" SET "schema"=jsonb_set("schema", $<path>, $<type>) WHERE "className"=$<className>', {path, type, className});
776786
}
777-
return promise.then(() => {
778-
return t.any('SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className> and ("schema"::json->\'fields\'->$<fieldName>) is not null', {className, fieldName});
779-
}).then(result => {
780-
if (result[0]) {
781-
throw "Attempted to add a field that already exists";
782-
} else {
783-
const path = `{fields,${fieldName}}`;
784-
return t.none(
785-
'UPDATE "_SCHEMA" SET "schema"=jsonb_set("schema", $<path>, $<type>) WHERE "className"=$<className>',
786-
{ path, type, className }
787-
);
788-
}
789-
});
790787
});
791788
}
792789

0 commit comments

Comments
 (0)