Skip to content

Commit 7da4deb

Browse files
vitaly-tdrew-gross
authored andcommitted
Update PostgresStorageAdapter.js (#2087)
1. Deleting tables in a transaction, as opposed to just a task. 2. Added transaction where it was supposed to be. However, it is not enough, the logic is still broken there... First, do not use `.catch` before `.then`. It is dangerous without good understanding how it actually works. Check this out: ```js .catch(error => { if (error.code === PostgresRelationDoesNotExistError) { return this.createClass(className, {fields: {[fieldName]: type}}) // this gets into the following `.then` } else if (error.code === PostgresDuplicateColumnError) { // Column already exists, created by other request. Carry on to // See if it's the right type. // this will get the following `.then` with `undefined` as the value } else { throw error; } }) ```
1 parent c37a4ea commit 7da4deb

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -136,30 +136,36 @@ export class PostgresStorageAdapter {
136136
}
137137

138138
addFieldIfNotExists(className, fieldName, type) {
139-
// TODO: Must be re-done into a transaction!
140-
return this._client.query('ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>', { className, fieldName, postgresType: parseTypeToPostgresType(type) })
141-
.catch(error => {
142-
if (error.code === PostgresRelationDoesNotExistError) {
143-
return this.createClass(className, { fields: { [fieldName]: type } })
144-
} else if (error.code === PostgresDuplicateColumnError) {
145-
// Column already exists, created by other request. Carry on to
146-
// See if it's the right type.
147-
} else {
148-
throw error;
149-
}
150-
})
151-
.then(() => this._client.query('SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className>', { className }))
152-
.then(result => {
153-
if (fieldName in result[0].schema) {
154-
throw "Attempted to add a field that already exists";
155-
} else {
156-
result[0].schema.fields[fieldName] = type;
157-
return this._client.query(
158-
'UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>',
159-
{ schema: result[0].schema, className }
160-
);
161-
}
162-
})
139+
// TODO: Must be revised for invalid logic...
140+
return this._client.tx("addFieldIfNotExists", t=> {
141+
return t.query('ALTER TABLE $<className:name> ADD COLUMN $<fieldName:name> $<postgresType:raw>', {
142+
className,
143+
fieldName,
144+
postgresType: parseTypeToPostgresType(type)
145+
})
146+
.catch(error => {
147+
if (error.code === PostgresRelationDoesNotExistError) {
148+
return this.createClass(className, {fields: {[fieldName]: type}})
149+
} else if (error.code === PostgresDuplicateColumnError) {
150+
// Column already exists, created by other request. Carry on to
151+
// See if it's the right type.
152+
} else {
153+
throw error;
154+
}
155+
})
156+
.then(() => t.query('SELECT "schema" FROM "_SCHEMA" WHERE "className" = $<className>', {className}))
157+
.then(result => {
158+
if (fieldName in result[0].schema) {
159+
throw "Attempted to add a field that already exists";
160+
} else {
161+
result[0].schema.fields[fieldName] = type;
162+
return t.query(
163+
'UPDATE "_SCHEMA" SET "schema"=$<schema> WHERE "className"=$<className>',
164+
{schema: result[0].schema, className}
165+
);
166+
}
167+
})
168+
});
163169
}
164170

165171
// Drops a collection. Resolves with true if it was a Parse Schema (eg. _User, Custom, etc.)
@@ -173,7 +179,7 @@ export class PostgresStorageAdapter {
173179
return this._client.query('SELECT "className" FROM "_SCHEMA"')
174180
.then(results => {
175181
const classes = ['_SCHEMA', ...results.map(result => result.className)];
176-
return this._client.task(t=>t.batch(classes.map(className=>t.none('DROP TABLE $<className:name>', { className }))));
182+
return this._client.tx(t=>t.batch(classes.map(className=>t.none('DROP TABLE $<className:name>', { className }))));
177183
}, error => {
178184
if (error.code === PostgresRelationDoesNotExistError) {
179185
// No _SCHEMA collection. Don't delete anything.

0 commit comments

Comments
 (0)