mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-12 21:37:32 -08:00
fix(core): Handle empty executions table in pruning in migrations (#8121)
In case someone manually prunes their executions table before upgrading to 1.x, `MigrateIntegerKeysToString` should gracefully handle that, instead of crashing the application. ## Review / Merge checklist - [x] PR title and summary are descriptive
This commit is contained in:
parent
bec0faed9e
commit
ffaa30ddc4
|
@ -208,20 +208,24 @@ const pruneExecutionsData = async ({ queryRunner, tablePrefix, logger }: Migrati
|
||||||
}
|
}
|
||||||
|
|
||||||
console.time('pruningData');
|
console.time('pruningData');
|
||||||
const counting = (await queryRunner.query(
|
const [{ rowCount }] = (await queryRunner.query(
|
||||||
`select count(id) as rows from "${tablePrefix}execution_entity";`,
|
`select count(id) as rowCount from "${tablePrefix}execution_entity";`,
|
||||||
)) as Array<{ rows: number }>;
|
)) as Array<{ rowCount: number }>;
|
||||||
|
|
||||||
const averageExecutionSize = dbFileSize / counting[0].rows;
|
if (rowCount > 0) {
|
||||||
const numberOfExecutionsToKeep = Math.floor(DESIRED_DATABASE_FILE_SIZE / averageExecutionSize);
|
const averageExecutionSize = dbFileSize / rowCount;
|
||||||
|
const numberOfExecutionsToKeep = Math.floor(
|
||||||
|
DESIRED_DATABASE_FILE_SIZE / averageExecutionSize,
|
||||||
|
);
|
||||||
|
|
||||||
const query = `SELECT id FROM "${tablePrefix}execution_entity" ORDER BY id DESC limit ${numberOfExecutionsToKeep}, 1`;
|
const query = `SELECT id FROM "${tablePrefix}execution_entity" ORDER BY id DESC limit ${numberOfExecutionsToKeep}, 1`;
|
||||||
const idToKeep = await queryRunner
|
const idToKeep = await queryRunner
|
||||||
.query(query)
|
.query(query)
|
||||||
.then((rows: Array<{ id: number }>) => rows[0].id);
|
.then((rows: Array<{ id: number }>) => rows[0].id);
|
||||||
|
|
||||||
const removalQuery = `DELETE FROM "${tablePrefix}execution_entity" WHERE id < ${idToKeep} and status IN ('success')`;
|
const removalQuery = `DELETE FROM "${tablePrefix}execution_entity" WHERE id < ${idToKeep} and status IN ('success')`;
|
||||||
await queryRunner.query(removalQuery);
|
await queryRunner.query(removalQuery);
|
||||||
|
}
|
||||||
console.timeEnd('pruningData');
|
console.timeEnd('pruningData');
|
||||||
} else {
|
} else {
|
||||||
logger.debug('Pruning was requested, but was not enabled');
|
logger.debug('Pruning was requested, but was not enabled');
|
||||||
|
|
Loading…
Reference in a new issue