diff --git a/keyserver/src/database/database.js b/keyserver/src/database/database.js --- a/keyserver/src/database/database.js +++ b/keyserver/src/database/database.js @@ -13,6 +13,16 @@ const SQLStatement: SQLStatementType = SQL.SQLStatement; +let migrationConnection; +async function getMigrationConnection() { + if (migrationConnection) { + return migrationConnection; + } + const dbConfig = await getDBConfig(); + migrationConnection = await mysqlPromise.createConnection(dbConfig); + return migrationConnection; +} + let pool, databaseMonitor; async function loadPool(): Promise { if (pool) { @@ -86,6 +96,24 @@ const MYSQL_DEADLOCK_ERROR_CODE = 1213; +type ConnectionContext = { + +migrationsActive?: boolean, +}; +let connectionContext = { + migrationsActive: false, +}; + +function setConnectionContext(newContext: ConnectionContext) { + connectionContext = { + ...connectionContext, + ...newContext, + }; + if (!connectionContext.migrationsActive && migrationConnection) { + migrationConnection.end(); + migrationConnection = undefined; + } +} + type QueryOptions = { +triesLeft?: number, +multipleStatements?: boolean, @@ -98,6 +126,9 @@ const multipleStatements = options?.multipleStatements ?? false; let connection; + if (connectionContext.migrationsActive) { + connection = await getMigrationConnection(); + } if (multipleStatements) { connection = await getMultipleStatementsConnection(); } @@ -157,6 +188,7 @@ appendSQLArray, mergeAndConditions, mergeOrConditions, + setConnectionContext, dbQuery, rawSQL, }; 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,7 +6,7 @@ import { getMessageForException } from 'lib/utils/errors'; import sleep from 'lib/utils/sleep'; -import { dbQuery, SQL } from './database'; +import { dbQuery, SQL, setConnectionContext } from './database'; import { fetchDBVersion, updateDBVersion } from './db-version'; import { migrations } from './migration-config'; import { setupDB } from './setup-db'; @@ -26,6 +26,7 @@ return false; } + setConnectionContext({ migrationsActive: true }); for (const [idx, migration] of migrations.entries()) { if (idx <= dbVersion) { continue; @@ -45,6 +46,7 @@ return false; } } + setConnectionContext({ migrationsActive: false }); return true; }