refactor(core): Migrate DB setup to use DI (#15324)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2025-05-13 13:28:41 +02:00
committed by GitHub
parent c061acc01c
commit 8591c2e0d1
35 changed files with 782 additions and 378 deletions

View File

@@ -5,8 +5,8 @@ import type { DataSourceOptions } from '@n8n/typeorm';
import { DataSource as Connection } from '@n8n/typeorm';
import { randomString } from 'n8n-workflow';
import { getOptionOverrides } from '@/databases/config';
import * as Db from '@/db';
import { DbConnection } from '@/databases/db-connection';
import { DbConnectionOptions } from '@/databases/db-connection-options';
export const testDbPrefix = 'n8n_test_';
@@ -34,20 +34,23 @@ export async function init() {
globalConfig.database.mysqldb.database = testDbName;
}
await Db.init();
await Db.migrate();
const dbConnection = Container.get(DbConnection);
await dbConnection.init();
await dbConnection.migrate();
}
export function isReady() {
return Db.connectionState.connected && Db.connectionState.migrated;
const { connectionState } = Container.get(DbConnection);
return connectionState.connected && connectionState.migrated;
}
/**
* Drop test DB, closing bootstrap connection if existing.
*/
export async function terminate() {
await Db.close();
Db.connectionState.connected = false;
const dbConnection = Container.get(DbConnection);
await dbConnection.close();
dbConnection.connectionState.connected = false;
}
type EntityName = keyof typeof entities | 'InsightsRaw' | 'InsightsByPeriod' | 'InsightsMetadata';
@@ -71,7 +74,7 @@ export const getBootstrapDBOptions = (dbType: 'postgresdb' | 'mysqldb'): DataSou
const type = dbType === 'postgresdb' ? 'postgres' : 'mysql';
return {
type,
...getOptionOverrides(dbType),
...Container.get(DbConnectionOptions).getOverrides(dbType),
database: type,
entityPrefix: globalConfig.database.tablePrefix,
schema: dbType === 'postgresdb' ? globalConfig.database.postgresdb.schema : undefined,