diff --git a/keyserver/src/database/db-version.js b/keyserver/src/database/db-version.js new file mode 100644 --- /dev/null +++ b/keyserver/src/database/db-version.js @@ -0,0 +1,31 @@ +// @flow + +import type { QueryResults } from 'mysql'; + +import { dbQuery, SQL } from './database'; + +async function fetchDBVersion(): Promise { + const versionQuery = SQL` + SELECT data + FROM metadata + WHERE name = 'db_version'; + `; + const [[versionResult]] = await dbQuery(versionQuery); + if (!versionResult) { + return -1; + } + return versionResult.data; +} + +async function updateDBVersion(dbVersion: number): Promise { + const updateQuery = SQL` + INSERT INTO metadata (name, data) + VALUES ('db_version', ${dbVersion}) + ON DUPLICATE KEY + UPDATE + data = ${dbVersion}; + `; + return dbQuery(updateQuery); +} + +export { fetchDBVersion, updateDBVersion }; 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 @@ -5,6 +5,7 @@ import { getMessageForException } from 'lib/utils/errors'; import { dbQuery, SQL } from './database'; +import { fetchDBVersion, updateDBVersion } from './db-version'; import { migrations } from './migration-config'; import { setupDB } from './setup-db'; @@ -53,30 +54,6 @@ } } -async function fetchDBVersion(): Promise { - const versionQuery = SQL` - SELECT data - FROM metadata - WHERE name = 'db_version'; - `; - const [[versionResult]] = await dbQuery(versionQuery); - if (!versionResult) { - return -1; - } - return versionResult.data; -} - -async function updateDBVersion(dbVersion: number): Promise { - const updateQuery = SQL` - INSERT INTO metadata (name, data) - VALUES ('db_version', ${dbVersion}) - ON DUPLICATE KEY - UPDATE - data = ${dbVersion}; - `; - return dbQuery(updateQuery); -} - async function startTransaction(): Promise { const beginTxnQuery = SQL` START TRANSACTION;