refactor(core): Improve DB directory setup (#3502)
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
import { readFileSync, rmSync } from 'fs';
|
||||
import { UserSettings } from 'n8n-core';
|
||||
import { QueryRunner } from 'typeorm/query-runner/QueryRunner';
|
||||
import { getLogger } from '../../Logger';
|
||||
|
||||
const PERSONALIZATION_SURVEY_FILENAME = 'personalizationSurvey.json';
|
||||
@@ -53,3 +55,31 @@ export function logMigrationEnd(migrationName: string): void {
|
||||
logger.warn('Migrations finished.');
|
||||
}, 100);
|
||||
}
|
||||
|
||||
export function chunkQuery(query: string, limit: number, offset = 0): string {
|
||||
return `
|
||||
${query}
|
||||
LIMIT ${limit}
|
||||
OFFSET ${offset}
|
||||
`;
|
||||
}
|
||||
|
||||
export async function runChunked(
|
||||
queryRunner: QueryRunner,
|
||||
query: string,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
operation: (results: any[]) => Promise<void>,
|
||||
limit = 100,
|
||||
): Promise<void> {
|
||||
let offset = 0;
|
||||
let chunkedQuery: string;
|
||||
let chunkedQueryResults: unknown[];
|
||||
|
||||
do {
|
||||
chunkedQuery = chunkQuery(query, limit, offset);
|
||||
chunkedQueryResults = (await queryRunner.query(chunkedQuery)) as unknown[];
|
||||
// pass a copy to prevent errors from mutation
|
||||
await operation([...chunkedQueryResults]);
|
||||
offset += limit;
|
||||
} while (chunkedQueryResults.length === limit);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user