mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
🧪 Truncate mapping tables
This commit is contained in:
@@ -5,8 +5,13 @@ import { createConnection, getConnection, ConnectionOptions, Connection } from '
|
||||
import { Credentials, UserSettings } from 'n8n-core';
|
||||
|
||||
import config from '../../../config';
|
||||
import { BOOTSTRAP_MYSQL_CONNECTION_NAME, BOOTSTRAP_POSTGRES_CONNECTION_NAME } from './constants';
|
||||
import { Db, ICredentialsDb, IDatabaseCollections } from '../../../src';
|
||||
import {
|
||||
BOOTSTRAP_MYSQL_CONNECTION_NAME,
|
||||
BOOTSTRAP_POSTGRES_CONNECTION_NAME,
|
||||
MAPPING_TABLES,
|
||||
MAPPING_TABLES_TO_CLEAR,
|
||||
} from './constants';
|
||||
import { DatabaseType, Db, ICredentialsDb } from '../../../src';
|
||||
import { randomApiKey, randomEmail, randomName, randomString, randomValidPassword } from './random';
|
||||
import { CredentialsEntity } from '../../../src/databases/entities/CredentialsEntity';
|
||||
import { hashPassword } from '../../../src/UserManagement/UserManagementHelper';
|
||||
@@ -19,7 +24,7 @@ import { createCredentiasFromCredentialsEntity } from '../../../src/CredentialsH
|
||||
|
||||
import type { Role } from '../../../src/databases/entities/Role';
|
||||
import { User } from '../../../src/databases/entities/User';
|
||||
import type { CollectionName, CredentialPayload } from './types';
|
||||
import type { CollectionName, CredentialPayload, MappingName } from './types';
|
||||
import { WorkflowEntity } from '../../../src/databases/entities/WorkflowEntity';
|
||||
import { ExecutionEntity } from '../../../src/databases/entities/ExecutionEntity';
|
||||
import { TagEntity } from '../../../src/databases/entities/TagEntity';
|
||||
@@ -127,32 +132,84 @@ export async function terminate(testDbName: string) {
|
||||
}
|
||||
}
|
||||
|
||||
async function truncateMappingTables(
|
||||
dbType: DatabaseType,
|
||||
collections: Array<CollectionName>,
|
||||
testDb: Connection,
|
||||
) {
|
||||
const mappingTables = collections.reduce<string[]>((acc, collection) => {
|
||||
const found = MAPPING_TABLES_TO_CLEAR[collection];
|
||||
|
||||
if (found) acc.push(...found);
|
||||
|
||||
return acc;
|
||||
}, []);
|
||||
|
||||
if (dbType === 'sqlite') {
|
||||
const promises = mappingTables.map((tableName) =>
|
||||
testDb.query(`DELETE FROM ${tableName}; DELETE FROM sqlite_sequence WHERE name=${tableName};`),
|
||||
);
|
||||
|
||||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
if (dbType === 'postgresdb') {
|
||||
const promises = mappingTables.map((tableName) => {
|
||||
const schema = config.getEnv('database.postgresdb.schema');
|
||||
const fullTableName = `${schema}.${tableName}`;
|
||||
testDb.query(`TRUNCATE TABLE ${fullTableName} RESTART IDENTITY CASCADE;`);
|
||||
});
|
||||
|
||||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
// mysqldb, mariadb
|
||||
|
||||
const promises = mappingTables.map((tableName) =>
|
||||
testDb.query(`DELETE FROM ${tableName}; ALTER TABLE ${tableName} AUTO_INCREMENT = 1;`),
|
||||
);
|
||||
|
||||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate DB tables for collections.
|
||||
* Truncate specific DB tables in a test DB.
|
||||
*
|
||||
* @param collections Array of entity names whose tables to truncate.
|
||||
* @param testDbName Name of the test DB to truncate tables in.
|
||||
*/
|
||||
export async function truncate(collections: CollectionName[], testDbName: string) {
|
||||
export async function truncate(collections: Array<CollectionName>, testDbName: string) {
|
||||
const dbType = config.getEnv('database.type');
|
||||
|
||||
const testDb = getConnection(testDbName);
|
||||
|
||||
if (dbType === 'sqlite') {
|
||||
await testDb.query('PRAGMA foreign_keys=OFF');
|
||||
await Promise.all(collections.map((collection) => Db.collections[collection].clear()));
|
||||
|
||||
const truncationPromises = collections.map((collection) => {
|
||||
const tableName = toTableName(collection);
|
||||
return testDb.query(
|
||||
`DELETE FROM ${tableName}; DELETE FROM sqlite_sequence WHERE name=${tableName};`,
|
||||
);
|
||||
});
|
||||
|
||||
truncationPromises.push(truncateMappingTables(dbType, collections, testDb));
|
||||
|
||||
await Promise.all(truncationPromises);
|
||||
|
||||
return testDb.query('PRAGMA foreign_keys=ON');
|
||||
}
|
||||
|
||||
if (dbType === 'postgresdb') {
|
||||
return Promise.all(
|
||||
collections.map((collection) => {
|
||||
const schema = config.getEnv('database.postgresdb.schema');
|
||||
const fullTableName = `${schema}.${toTableName(collection)}`;
|
||||
const truncationPromises = collections.map((collection) => {
|
||||
const schema = config.getEnv('database.postgresdb.schema');
|
||||
const fullTableName = `${schema}.${toTableName(collection)}`;
|
||||
|
||||
testDb.query(`TRUNCATE TABLE ${fullTableName} RESTART IDENTITY CASCADE;`);
|
||||
}),
|
||||
);
|
||||
return testDb.query(`TRUNCATE TABLE ${fullTableName} RESTART IDENTITY CASCADE;`);
|
||||
});
|
||||
|
||||
truncationPromises.push(truncateMappingTables(dbType, collections, testDb));
|
||||
|
||||
return Promise.all(truncationPromises);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,11 +224,17 @@ export async function truncate(collections: CollectionName[], testDbName: string
|
||||
);
|
||||
|
||||
await truncateMySql(testDb, isShared);
|
||||
await truncateMappingTables(dbType, collections, testDb);
|
||||
await truncateMySql(testDb, isNotShared);
|
||||
}
|
||||
}
|
||||
|
||||
function toTableName(collectionName: CollectionName) {
|
||||
const isMapping = (collection: string): collection is MappingName =>
|
||||
Object.keys(MAPPING_TABLES).includes(collection);
|
||||
|
||||
function toTableName(sourceName: CollectionName | MappingName) {
|
||||
if (isMapping(sourceName)) return MAPPING_TABLES[sourceName];
|
||||
|
||||
return {
|
||||
Credentials: 'credentials_entity',
|
||||
Workflow: 'workflow_entity',
|
||||
@@ -183,10 +246,10 @@ function toTableName(collectionName: CollectionName) {
|
||||
SharedCredentials: 'shared_credentials',
|
||||
SharedWorkflow: 'shared_workflow',
|
||||
Settings: 'settings',
|
||||
}[collectionName];
|
||||
}[sourceName];
|
||||
}
|
||||
|
||||
function truncateMySql(connection: Connection, collections: Array<keyof IDatabaseCollections>) {
|
||||
function truncateMySql(connection: Connection, collections: CollectionName[]) {
|
||||
return Promise.all(
|
||||
collections.map(async (collection) => {
|
||||
const tableName = toTableName(collection);
|
||||
|
||||
Reference in New Issue
Block a user