diff --git a/keyserver/src/database/migrations.js b/keyserver/src/database/migrations.js --- a/keyserver/src/database/migrations.js +++ b/keyserver/src/database/migrations.js @@ -6,6 +6,7 @@ import { getMessageForException } from 'lib/utils/errors'; import { dbQuery, SQL } from './database'; +import { setupDB } from './setup-db'; async function makeSureBaseRoutePathExists(filePath: string): Promise { let readFile, json; @@ -90,7 +91,7 @@ async function migrate(): Promise { let dbVersion = null; try { - dbVersion = await getDBVersion(); + dbVersion = await setUpDBAndReturnVersion(); console.log(`(node:${process.pid}) DB version: ${dbVersion}`); } catch (e) { const dbVersionExceptionMessage = String(getMessageForException(e)); @@ -120,7 +121,19 @@ return true; } -async function getDBVersion(): Promise { +async function setUpDBAndReturnVersion(): Promise { + try { + return await fetchDBVersion(); + } catch (e) { + if (e.errno !== 1146) { + throw e; + } + await setupDB(); + return await fetchDBVersion(); + } +} + +async function fetchDBVersion(): Promise { const versionQuery = SQL` SELECT data FROM metadata