From 2146a57ad25381f8bbaf7ecedef33a9e2a39e52e Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 24 Dec 2017 20:55:49 +0000 Subject: [PATCH 1/4] Refactoring method createTable Replacing the weird task + transaction chain, by replacing it with just one transaction that encapsulates the complete logic. --- .../Postgres/PostgresStorageAdapter.js | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 1cc61c2aae..4e0934c153 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -684,6 +684,7 @@ export class PostgresStorageAdapter { // Just create a table, do not insert in schema createTable(className, schema, conn) { conn = conn || this._client; + const self = this; debug('createTable', className, schema); const valuesArray = []; const patternsArray = []; @@ -721,24 +722,23 @@ export class PostgresStorageAdapter { }); const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join(',')})`; const values = [className, ...valuesArray]; - return conn.task(t => { - return this._ensureSchemaCollectionExists(t) - .then(() => t.none(qs, values)) - .catch(error => { - if (error.code === PostgresDuplicateRelationError) { - // Table already exists, must have been created by a different request. Ignore error. - } else { + + return conn.tx('create-table', function * (t) { + try { + yield self._ensureSchemaCollectionExists(t); + } catch(error) { + if (error.code !== PostgresDuplicateRelationError) { throw error; - }}) - }) - .then(() => { - return conn.tx('create-relation-tables', t => { - const queries = relations.map((fieldName) => { - return t.none('CREATE TABLE IF NOT EXISTS $ ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`}); - }); - return t.batch(queries); - }); + } + // ELSE: Table already exists, must have been created by a different request. Ignore the error. + } + yield t.none(qs, values); + + const queries = relations.map(fieldName => { + return t.none('CREATE TABLE IF NOT EXISTS $ ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`}); }); + yield t.batch(queries); + }); } addFieldIfNotExists(className, fieldName, type) { From e07da41ce601cb20e26027fc8259f0aed4ffdfd7 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 24 Dec 2017 21:12:09 +0000 Subject: [PATCH 2/4] Update PostgresStorageAdapter.js correcting the sequence to match the original exactly. --- src/Adapters/Storage/Postgres/PostgresStorageAdapter.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 4e0934c153..f544870e25 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -726,14 +726,13 @@ export class PostgresStorageAdapter { return conn.tx('create-table', function * (t) { try { yield self._ensureSchemaCollectionExists(t); + yield t.none(qs, values); } catch(error) { if (error.code !== PostgresDuplicateRelationError) { throw error; } // ELSE: Table already exists, must have been created by a different request. Ignore the error. - } - yield t.none(qs, values); - + } const queries = relations.map(fieldName => { return t.none('CREATE TABLE IF NOT EXISTS $ ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`}); }); From 78c63c499de122b4a017aac07996b09df5b87ce8 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 24 Dec 2017 21:32:48 +0000 Subject: [PATCH 3/4] Update PostgresStorageAdapter.js Nesting the transaction inside a task, so it can execute successfully no matter if the containing task succeeds or fails. --- .../Storage/Postgres/PostgresStorageAdapter.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index f544870e25..e9cc60746b 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -723,7 +723,7 @@ export class PostgresStorageAdapter { const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join(',')})`; const values = [className, ...valuesArray]; - return conn.tx('create-table', function * (t) { + return conn.task('create-table', function * (t) { try { yield self._ensureSchemaCollectionExists(t); yield t.none(qs, values); @@ -732,11 +732,12 @@ export class PostgresStorageAdapter { throw error; } // ELSE: Table already exists, must have been created by a different request. Ignore the error. - } - const queries = relations.map(fieldName => { - return t.none('CREATE TABLE IF NOT EXISTS $ ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`}); + } + yield t.tx('create-table-tx', tx => { + return tx.batch(relations.map(fieldName => { + return tx.none('CREATE TABLE IF NOT EXISTS $ ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`}); + }); }); - yield t.batch(queries); }); } From d2fdc64e69becdf77695fcef509672dd06cfd844 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sun, 24 Dec 2017 21:39:54 +0000 Subject: [PATCH 4/4] Update PostgresStorageAdapter.js adding the missing bracket. --- src/Adapters/Storage/Postgres/PostgresStorageAdapter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index e9cc60746b..0bb6d54689 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -736,9 +736,9 @@ export class PostgresStorageAdapter { yield t.tx('create-table-tx', tx => { return tx.batch(relations.map(fieldName => { return tx.none('CREATE TABLE IF NOT EXISTS $ ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`}); - }); + })); }); - }); + }); } addFieldIfNotExists(className, fieldName, type) {