Skip to content

Commit

Permalink
Introduce dereferenceConnection for TypeORM to be used in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fflorent committed Aug 19, 2024
1 parent 4fab84f commit 2a2ccf1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/server/lib/dbUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
2 changes: 2 additions & 0 deletions test/gen-server/lib/homedb/UsersManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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_`);
Expand Down Expand Up @@ -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);
}
}

Expand Down
27 changes: 20 additions & 7 deletions test/gen-server/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<string, Connection>;
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
Expand Down

0 comments on commit 2a2ccf1

Please sign in to comment.