From b1a441e4a71116c9d3c7687f30f092d921ea128d Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Thu, 1 Sep 2022 18:06:47 +0530 Subject: [PATCH] refactor/Now knex connections for all projects will be deleted before each test --- .../__tests__/unit/rest/init/cleanupMeta.ts | 4 ++ .../src/__tests__/unit/rest/init/index.ts | 43 +++++++++---------- packages/nocodb/src/lib/models/Project.ts | 2 + .../src/lib/utils/common/NcConnectionMgrv2.ts | 9 ++++ 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/packages/nocodb/src/__tests__/unit/rest/init/cleanupMeta.ts b/packages/nocodb/src/__tests__/unit/rest/init/cleanupMeta.ts index 44a195fe10..64cbca1ece 100644 --- a/packages/nocodb/src/__tests__/unit/rest/init/cleanupMeta.ts +++ b/packages/nocodb/src/__tests__/unit/rest/init/cleanupMeta.ts @@ -1,5 +1,7 @@ +import Base from '../../../../lib/models/Base'; import Model from '../../../../lib/models/Model'; import Project from '../../../../lib/models/Project'; +import NcConnectionMgrv2 from '../../../../lib/utils/common/NcConnectionMgrv2'; import { orderedMetaTables } from '../../../../lib/utils/globals'; const dropTablesAllNonExternalProjects = async (knexClient) => { @@ -42,6 +44,8 @@ const cleanupMetaTables = async (knexClient) => { export default async function (knexClient) { try { + await NcConnectionMgrv2.destroyAll(); + await dropTablesAllNonExternalProjects(knexClient); await cleanupMetaTables(knexClient); } catch (e) { diff --git a/packages/nocodb/src/__tests__/unit/rest/init/index.ts b/packages/nocodb/src/__tests__/unit/rest/init/index.ts index 972c41d057..9dba451f3a 100644 --- a/packages/nocodb/src/__tests__/unit/rest/init/index.ts +++ b/packages/nocodb/src/__tests__/unit/rest/init/index.ts @@ -4,8 +4,21 @@ import express from 'express'; import cleanupMeta from './cleanupMeta'; import cleanUpSakila from './cleanupSakila'; import { createUser } from '../tests/helpers/user'; +import knex from 'knex'; -const knex = require('knex'); +const knexClient = knex(dbConfig); + +const sakilaKnexClient = knex({ + client: 'mysql2', + connection: { + host: 'localhost', + port: 3306, + user: 'root', + password: 'password', + database: sakilaDbName, + }, + pool: { min: 0, max: 2 }, +}); const serverInit = async () => { const server = express(); @@ -14,7 +27,7 @@ const serverInit = async () => { return server; }; -const resetDatabase = async (knexClient) => { +const resetDatabase = async () => { try { if (!Noco.initialized) { try { @@ -27,39 +40,25 @@ const resetDatabase = async (knexClient) => { } }; -const cleanupAllTables = async (knexClient) => { - const sakilaKnexClient = knex({ - client: 'mysql2', - connection: { - host: 'localhost', - port: '3306', - user: 'root', - password: 'password', - database: sakilaDbName, - }, - }); - +const cleanupAllTables = async () => { try { await cleanUpSakila(sakilaKnexClient); - await sakilaKnexClient.destroy(); - await cleanupMeta(knexClient); } catch (e) { console.error('cleanupAllTables', e); - sakilaKnexClient.destroy(); } }; export default async function () { - const knexClient = knex(dbConfig); - await resetDatabase(knexClient); + await knexClient.raw(`USE ${dbName}`); + await sakilaKnexClient.raw(`USE ${sakilaDbName}`); - const server = await serverInit(); + await resetDatabase(); - await cleanupAllTables(knexClient); + const server = await serverInit(); - await knexClient.destroy(); + await cleanupAllTables(); const { token } = await createUser({ app: server }, { roles: 'editor' }); diff --git a/packages/nocodb/src/lib/models/Project.ts b/packages/nocodb/src/lib/models/Project.ts index b100504aa1..512515678e 100644 --- a/packages/nocodb/src/lib/models/Project.ts +++ b/packages/nocodb/src/lib/models/Project.ts @@ -173,6 +173,7 @@ export default class Project implements ProjectType { return null; } + // Todo: Remove the project entry from the connection pool in NcConnectionMgrv2 // @ts-ignore static async softDelete( projectId: string, @@ -273,6 +274,7 @@ export default class Project implements ProjectType { ); } + // Todo: Remove the project entry from the connection pool in NcConnectionMgrv2 static async delete(projectId, ncMeta = Noco.ncMeta): Promise { const bases = await Base.list({ projectId }); for (const base of bases) { diff --git a/packages/nocodb/src/lib/utils/common/NcConnectionMgrv2.ts b/packages/nocodb/src/lib/utils/common/NcConnectionMgrv2.ts index a073ed8662..602208354a 100644 --- a/packages/nocodb/src/lib/utils/common/NcConnectionMgrv2.ts +++ b/packages/nocodb/src/lib/utils/common/NcConnectionMgrv2.ts @@ -22,6 +22,15 @@ export default class NcConnectionMgrv2 { // this.metaKnex = ncMeta; // } + public static async destroyAll() { + for (const projectId in this.connectionRefs) { + for (const baseId in this.connectionRefs[projectId]) { + await this.connectionRefs[projectId][baseId].destroy(); + } + } + } + + // Todo: Should await on connection destroy public static delete(base: Base) { // todo: ignore meta projects if (this.connectionRefs?.[base.project_id]?.[base.id]) {