refactor(core): Move first batch of repositories to @n8n/db (#15130)

This commit is contained in:
Iván Ovejero
2025-05-06 11:22:43 +02:00
committed by GitHub
parent 939ff97ec4
commit ade546fb48
151 changed files with 338 additions and 299 deletions

View File

@@ -1,9 +1,8 @@
import { GlobalConfig } from '@n8n/config';
import type { entities } from '@n8n/db';
import { Container } from '@n8n/di';
import type { DataSourceOptions, Repository } from '@n8n/typeorm';
import type { DataSourceOptions } from '@n8n/typeorm';
import { DataSource as Connection } from '@n8n/typeorm';
import { kebabCase } from 'lodash';
import type { Class } from 'n8n-core';
import { randomString } from 'n8n-workflow';
import { getOptionOverrides } from '@/databases/config';
@@ -11,18 +10,13 @@ import * as Db from '@/db';
export const testDbPrefix = 'n8n_test_';
type Extensions = 'insights';
let loadedExtensions: Extensions[] = [];
/**
* Initialize one test DB per suite run, with bootstrap connection if needed.
*/
export async function init(extensionNames: Extensions[] = []) {
export async function init() {
const globalConfig = Container.get(GlobalConfig);
const dbType = globalConfig.database.type;
const testDbName = `${testDbPrefix}${randomString(6, 10).toLowerCase()}_${Date.now()}`;
loadedExtensions = extensionNames;
if (dbType === 'postgresdb') {
const bootstrapPostgres = await new Connection(
@@ -56,71 +50,16 @@ export async function terminate() {
Db.connectionState.connected = false;
}
// Can't use `Object.keys(entities)` here because some entities have a `Entity` suffix, while the repositories don't
const repositories = [
'AnnotationTag',
'AuthIdentity',
'AuthProviderSyncHistory',
'Credentials',
'EventDestinations',
'Execution',
'ExecutionAnnotation',
'ExecutionData',
'ExecutionMetadata',
'InstalledNodes',
'InstalledPackages',
'Project',
'ProjectRelation',
'Role',
'ProcessedData',
'Project',
'ProjectRelation',
'Settings',
'SharedCredentials',
'SharedWorkflow',
'Tag',
'TestDefinition',
'TestMetric',
'TestRun',
'User',
'Variables',
'Webhook',
'Workflow',
'WorkflowHistory',
'WorkflowStatistics',
'WorkflowTagMapping',
'ApiKey',
'Folder',
'InsightsRaw',
'InsightsMetadata',
'InsightsByPeriod',
] as const;
type EntityName = keyof typeof entities | 'InsightsRaw' | 'InsightsByPeriod' | 'InsightsMetadata';
/**
* Truncate specific DB tables in a test DB.
*/
export async function truncate(names: Array<(typeof repositories)[number]>) {
for (const name of names) {
let RepositoryClass: Class<Repository<object>>;
export async function truncate(entities: EntityName[]) {
const connection = Container.get(Connection);
const fileName = `${kebabCase(name)}.repository`;
const paths = [
`@/databases/repositories/${fileName}.ee`,
`@/databases/repositories/${fileName}`,
];
for (const extension of loadedExtensions) {
paths.push(
`@/modules/${extension}/database/repositories/${fileName}`,
`@/modules/${extension}/database/repositories/${fileName}.ee`,
);
}
RepositoryClass = (await Promise.any(paths.map(async (path) => await import(path))))[
`${name}Repository`
];
await Container.get(RepositoryClass).delete({});
for (const name of entities) {
await connection.getRepository(name).delete({});
}
}