From 1e5339121fe3bbbf5aa447941d0d7138654389e5 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 24 Dec 2017 00:09:53 +0000 Subject: [PATCH 1/3] refactoring database code Starting to refactor the database code for better use of promises + ES6 generators, to prepare for ES7 await/async. --- .../Postgres/PostgresStorageAdapter.js | 49 ++++++++----------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 4f44c1f946..a8bc2fc781 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -836,35 +836,26 @@ export class PostgresStorageAdapter { // Returns a Promise. deleteFields(className, schema, fieldNames) { debug('deleteFields', className, fieldNames); - return Promise.resolve() - .then(() => { - fieldNames = fieldNames.reduce((list, fieldName) => { - const field = schema.fields[fieldName] - if (field.type !== 'Relation') { - list.push(fieldName); - } - delete schema.fields[fieldName]; - return list; - }, []); - - const values = [className, ...fieldNames]; - const columns = fieldNames.map((name, idx) => { - return `$${idx + 2}:name`; - }).join(', DROP COLUMN'); - - const doBatch = (t) => { - const batch = [ - t.none('UPDATE "_SCHEMA" SET "schema"=$ WHERE "className"=$', {schema, className}) - ]; - if (values.length > 1) { - batch.push(t.none(`ALTER TABLE $1:name DROP COLUMN ${columns}`, values)); - } - return batch; - } - return this._client.tx((t) => { - return t.batch(doBatch(t)); - }); - }); + fieldNames = fieldNames.reduce((list, fieldName) => { + const field = schema.fields[fieldName] + if (field.type !== 'Relation') { + list.push(fieldName); + } + delete schema.fields[fieldName]; + return list; + }, []); + + const values = [className, ...fieldNames]; + const columns = fieldNames.map((name, idx) => { + return `$${idx + 2}:name`; + }).join(', DROP COLUMN'); + + return this._client.tx(function * (t) => { + yield t.none('UPDATE "_SCHEMA" SET "schema"=$ WHERE "className"=$', {schema, className}); + if (values.length > 1) { + yield t.none(`ALTER TABLE $1:name DROP COLUMN ${columns}`, values); + } + }); } // Return a promise for all schemas known to this adapter, in Parse format. In case the From 4f6d907f5688871e1ae08253f5bcca74fc3e0d83 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 24 Dec 2017 00:13:57 +0000 Subject: [PATCH 2/3] Update PostgresStorageAdapter.js --- src/Adapters/Storage/Postgres/PostgresStorageAdapter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index a8bc2fc781..9a783524d8 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -850,7 +850,7 @@ export class PostgresStorageAdapter { return `$${idx + 2}:name`; }).join(', DROP COLUMN'); - return this._client.tx(function * (t) => { + return this._client.tx(function * (t) { yield t.none('UPDATE "_SCHEMA" SET "schema"=$ WHERE "className"=$', {schema, className}); if (values.length > 1) { yield t.none(`ALTER TABLE $1:name DROP COLUMN ${columns}`, values); From 84438049b21f54f492736575aeb973841a066af9 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 24 Dec 2017 11:16:04 +0000 Subject: [PATCH 3/3] Update PostgresStorageAdapter.js naming the transaction. --- src/Adapters/Storage/Postgres/PostgresStorageAdapter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 9a783524d8..433e63ef92 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -850,7 +850,7 @@ export class PostgresStorageAdapter { return `$${idx + 2}:name`; }).join(', DROP COLUMN'); - return this._client.tx(function * (t) { + return this._client.tx('delete-fields', function * (t) { yield t.none('UPDATE "_SCHEMA" SET "schema"=$ WHERE "className"=$', {schema, className}); if (values.length > 1) { yield t.none(`ALTER TABLE $1:name DROP COLUMN ${columns}`, values);