Browse Source

refactor/Now knex connections for all projects will be deleted before each test

pull/3358/head
Muhammed Mustafa 2 years ago
parent
commit
b1a441e4a7
  1. 4
      packages/nocodb/src/__tests__/unit/rest/init/cleanupMeta.ts
  2. 43
      packages/nocodb/src/__tests__/unit/rest/init/index.ts
  3. 2
      packages/nocodb/src/lib/models/Project.ts
  4. 9
      packages/nocodb/src/lib/utils/common/NcConnectionMgrv2.ts

4
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) {

43
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' });

2
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<any> {
const bases = await Base.list({ projectId });
for (const base of bases) {

9
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]) {

Loading…
Cancel
Save