fix(core): Try setting postgres search_path on the database (#9530)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-05-30 12:09:49 +02:00
committed by GitHub
parent cadb59fecb
commit e55bf0393a
5 changed files with 35 additions and 57 deletions

View File

@@ -4,7 +4,6 @@ import type { EntityManager } from '@n8n/typeorm';
import { DataSource as Connection } from '@n8n/typeorm';
import { ErrorReporterProxy as ErrorReporter } from 'n8n-workflow';
import config from '@/config';
import { inTest } from '@/constants';
import { wrapMigration } from '@db/utils/migrationHelpers';
import type { Migration } from '@db/types';
@@ -48,30 +47,14 @@ export async function transaction<T>(fn: (entityManager: EntityManager) => Promi
return await connection.transaction(fn);
}
export async function setSchema(conn: Connection) {
const schema = config.getEnv('database.postgresdb.schema');
const searchPath = ['public'];
if (schema !== 'public') {
await conn.query(`CREATE SCHEMA IF NOT EXISTS ${schema}`);
searchPath.unshift(schema);
}
await conn.query(`SET search_path TO ${searchPath.join(',')};`);
}
export async function init(): Promise<void> {
if (connectionState.connected) return;
const dbType = config.getEnv('database.type');
const connectionOptions = getConnectionOptions();
connection = new Connection(connectionOptions);
Container.set(Connection, connection);
await connection.initialize();
if (dbType === 'postgresdb') {
await setSchema(connection);
}
connectionState.connected = true;
}

View File

@@ -3,11 +3,9 @@ import type { DataSourceOptions as ConnectionOptions } from '@n8n/typeorm';
import { MigrationExecutor, DataSource as Connection } from '@n8n/typeorm';
import { Container } from 'typedi';
import { Logger } from '@/Logger';
import { setSchema } from '@/Db';
import { getConnectionOptions } from '@db/config';
import type { Migration } from '@db/types';
import { wrapMigration } from '@db/utils/migrationHelpers';
import config from '@/config';
// This function is extracted to make it easier to unit test it.
// Mocking turned into a mess due to this command using typeorm and the db
@@ -88,9 +86,6 @@ export class DbRevertMigrationCommand extends Command {
const connection = new Connection(connectionOptions);
await connection.initialize();
const dbType = config.getEnv('database.type');
if (dbType === 'postgresdb') await setSchema(connection);
const migrationExecutor = new MigrationExecutor(connection);
(connectionOptions.migrations as Migration[]).forEach(wrapMigration);