From 2259e1d44d551777e3f12d5d0d5c67f859ef96cd Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 2 Apr 2019 19:42:41 +0200 Subject: [PATCH] make sure to wait for DB connection to be up before starting ... --- electron.js | 2 ++ src/services/sql_init.js | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/electron.js b/electron.js index 798ebde9d..ecd3fb0de 100644 --- a/electron.js +++ b/electron.js @@ -30,6 +30,8 @@ function onClosed() { } async function createMainWindow() { + await sqlInit.dbConnection; + // if schema doesn't exist -> setup process // if schema exists, then we need to wait until the migration process is finished if (await sqlInit.schemaExists()) { diff --git a/src/services/sql_init.js b/src/services/sql_init.js index 60ad22163..3bed891a0 100644 --- a/src/services/sql_init.js +++ b/src/services/sql_init.js @@ -14,13 +14,19 @@ async function createConnection() { return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise}); } +const dbConnection = new Promise(async (resolve, reject) => { + // no need to create new connection now since DB stays the same all the time + const db = await createConnection(); + sql.setDbConnection(db); + + resolve(); +}); + let dbReadyResolve = null; const dbReady = new Promise(async (resolve, reject) => { dbReadyResolve = resolve; - // no need to create new connection now since DB stays the same all the time - const db = await createConnection(); - sql.setDbConnection(db); + await dbConnection; initDbConnection(); }); @@ -165,6 +171,7 @@ dbReady.then(async () => { module.exports = { dbReady, + dbConnection, schemaExists, isDbInitialized, initDbConnection,