Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Set postgres search_path to the desired schema on every connection #9530

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/@n8n/nodes-langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"@langchain/pinecone": "0.0.6",
"@langchain/redis": "0.0.5",
"@langchain/textsplitters": "0.0.2",
"@n8n/typeorm": "0.3.20-9",
"@n8n/typeorm": "0.3.20-10",
"@n8n/vm2": "3.9.20",
"@pinecone-database/pinecone": "2.2.1",
"@qdrant/js-client-rest": "1.9.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"@n8n/localtunnel": "2.1.0",
"@n8n/n8n-nodes-langchain": "workspace:*",
"@n8n/permissions": "workspace:*",
"@n8n/typeorm": "0.3.20-9",
"@n8n/typeorm": "0.3.20-10",
"@n8n_io/license-sdk": "2.12.0",
"@oclif/core": "3.18.1",
"@pinecone-database/pinecone": "2.1.0",
Expand Down Expand Up @@ -165,7 +165,7 @@
"prom-client": "13.2.0",
"psl": "1.9.0",
"raw-body": "2.5.1",
"reflect-metadata": "0.2.1",
"reflect-metadata": "0.2.2",
"replacestream": "4.0.3",
"samlify": "2.8.9",
"semver": "7.5.4",
Expand Down
17 changes: 0 additions & 17 deletions packages/cli/src/Db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 0 additions & 5 deletions packages/cli/src/commands/db/revert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
64 changes: 32 additions & 32 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading