diff --git a/app/server/lib/dbUtils.ts b/app/server/lib/dbUtils.ts index 65a23b5e2a1..754d774ecc1 100644 --- a/app/server/lib/dbUtils.ts +++ b/app/server/lib/dbUtils.ts @@ -45,7 +45,7 @@ export async function updateDb(connection?: Connection) { await synchronizeProducts(connection, true); } -function getConnectionName() { +export function getConnectionName() { return process.env.TYPEORM_NAME || 'default'; } diff --git a/test/gen-server/lib/homedb/UsersManager.ts b/test/gen-server/lib/homedb/UsersManager.ts index c2c9625f58e..736f1710555 100644 --- a/test/gen-server/lib/homedb/UsersManager.ts +++ b/test/gen-server/lib/homedb/UsersManager.ts @@ -26,6 +26,7 @@ import winston from 'winston'; import fs from 'fs/promises'; import { tmpdir } from 'os'; import path from 'path'; +import { dereferenceConnection } from 'test/gen-server/seed'; const username = process.env.USER || "nobody"; const tmpDirPrefix = path.join(tmpdir(), `grist_test_${username}_userendpoint_`); @@ -276,6 +277,7 @@ describe('UsersManager', function () { // TODO: Check whether using DataSource would help and avoid this hack. await db.connection.destroy(); await db.createNewConnection(); + dereferenceConnection(dbName); } } diff --git a/test/gen-server/seed.ts b/test/gen-server/seed.ts index d935211ef91..4f9791ac3a9 100644 --- a/test/gen-server/seed.ts +++ b/test/gen-server/seed.ts @@ -42,7 +42,9 @@ import {User} from "app/gen-server/entity/User"; import {Workspace} from "app/gen-server/entity/Workspace"; import {EXAMPLE_WORKSPACE_NAME} from 'app/gen-server/lib/homedb/HomeDBManager'; import {Permissions} from 'app/gen-server/lib/Permissions'; -import {getOrCreateConnection, runMigrations, undoLastMigration, updateDb} from 'app/server/lib/dbUtils'; +import { + getConnectionName, getOrCreateConnection, runMigrations, undoLastMigration, updateDb +} from 'app/server/lib/dbUtils'; import {FlexServer} from 'app/server/lib/FlexServer'; import * as fse from 'fs-extra'; @@ -526,17 +528,28 @@ class Seed { // When running mocha on several test files at once, we need to reset our database connection // if it exists. This is a little ugly since it is stored globally. -export async function removeConnection() { - if (getConnectionManager().connections.length > 0) { - if (getConnectionManager().connections.length > 1) { +export async function removeConnection(name?: string) { + const connections = getConnectionManager().connections; + if (connections.length > 0) { + if (connections.length > 1) { throw new Error("unexpected number of connections"); } - await getConnectionManager().connections[0].close(); - // There is still no official way to delete connections that I've found. - (getConnectionManager() as any).connectionMap = new Map(); + await connections[0].destroy(); + dereferenceConnection(getConnectionName()); } } +export function dereferenceConnection(name: string) { + // There seem to be no official way to delete connections. + // Also we should probably get rid of the use of connectionManager, which is deprecated + const connectionMgr = getConnectionManager(); + const connectionMap = (connectionMgr as any).connectionMap as Map; + if (!connectionMap.has(name)) { + throw new Error('connection with this name not found: ' + name); + } + connectionMap.delete(name); +} + export async function createInitialDb(connection?: Connection, migrateAndSeedData: boolean = true) { // In jenkins tests, we may want to reset the database to a clean // state. If so, TEST_CLEAN_DATABASE will have been set. How to