diff --git a/keyserver/src/database/migration-config.js b/keyserver/src/database/migration-config.js --- a/keyserver/src/database/migration-config.js +++ b/keyserver/src/database/migration-config.js @@ -408,34 +408,23 @@ [ 35, async () => { - const [pickledContentAccount, pickledNotificationsAccount] = - await Promise.all([ - createPickledOlmAccount(), - createPickledOlmAccount(), - ]); - + await createOlmAccounts(); + }, + ], + [36, updateRolesAndPermissionsForAllThreads], + [ + 37, + async () => { await dbQuery( SQL` - INSERT INTO olm_accounts (is_content, version, - pickling_key, pickled_olm_account) - VALUES - ( - TRUE, - 0, - ${pickledContentAccount.picklingKey}, - ${pickledContentAccount.pickledAccount} - ), - ( - FALSE, - 0, - ${pickledNotificationsAccount.picklingKey}, - ${pickledNotificationsAccount.pickledAccount} - ); + DELETE FROM olm_accounts; + DELETE FROM olm_sessions; `, + { multipleStatements: true }, ); + await createOlmAccounts(); }, ], - [36, updateRolesAndPermissionsForAllThreads], ]); const newDatabaseVersion: number = Math.max(...migrations.keys()); @@ -532,4 +521,29 @@ await writeJSONToFile(newJSON, filePath); } -export { migrations, newDatabaseVersion }; +async function createOlmAccounts() { + const [pickledContentAccount, pickledNotificationsAccount] = + await Promise.all([createPickledOlmAccount(), createPickledOlmAccount()]); + + await dbQuery( + SQL` + INSERT INTO olm_accounts (is_content, version, + pickling_key, pickled_olm_account) + VALUES + ( + TRUE, + 0, + ${pickledContentAccount.picklingKey}, + ${pickledContentAccount.pickledAccount} + ), + ( + FALSE, + 0, + ${pickledNotificationsAccount.picklingKey}, + ${pickledNotificationsAccount.pickledAccount} + ); + `, + ); +} + +export { migrations, newDatabaseVersion, createOlmAccounts }; diff --git a/keyserver/src/database/setup-db.js b/keyserver/src/database/setup-db.js --- a/keyserver/src/database/setup-db.js +++ b/keyserver/src/database/setup-db.js @@ -11,9 +11,11 @@ import { createThread } from '../creators/thread-creator.js'; import { dbQuery, SQL } from '../database/database.js'; import { updateDBVersion } from '../database/db-version.js'; -import { newDatabaseVersion } from '../database/migration-config.js'; +import { + newDatabaseVersion, + createOlmAccounts, +} from '../database/migration-config.js'; import { createScriptViewer } from '../session/scripts.js'; -import { createPickledOlmAccount } from '../utils/olm-utils.js'; async function setupDB() { await createTables(); @@ -476,29 +478,4 @@ await updateDBVersion(newDatabaseVersion); } -async function createOlmAccounts() { - const [pickledContentAccount, pickledNotificationsAccount] = - await Promise.all([createPickledOlmAccount(), createPickledOlmAccount()]); - - await dbQuery( - SQL` - INSERT INTO olm_accounts (is_content, version, - pickling_key, pickled_olm_account) - VALUES - ( - TRUE, - 0, - ${pickledContentAccount.picklingKey}, - ${pickledContentAccount.pickledAccount} - ), - ( - FALSE, - 0, - ${pickledNotificationsAccount.picklingKey}, - ${pickledNotificationsAccount.pickledAccount} - ); - `, - ); -} - export { setupDB };