From 2b4c7570ea851ff77326f9c8872c597a70befe8c Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Wed, 23 Aug 2017 16:34:57 +0100 Subject: [PATCH] Update PostgresStorageAdapter.js (#4094) * proper use of the connections * proper integrity: creating tables within a single transaction. --- .../Storage/Postgres/PostgresStorageAdapter.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 5795f86d..e9fd7b1b 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -668,10 +668,12 @@ export class PostgresStorageAdapter { throw error; } }).then(() => { - // Create the relation tables - return Promise.all(relations.map((fieldName) => { - return conn.none('CREATE TABLE IF NOT EXISTS $ ("relatedId" varChar(120), "owningId" varChar(120), PRIMARY KEY("relatedId", "owningId") )', {joinTable: `_Join:${fieldName}:${className}`}); - })); + 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); + }); }); }