From 31c83391cfe3866b118d31f586b72bd4123afaad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Thu, 21 Dec 2023 14:30:28 +0100 Subject: [PATCH] fix(core): Handle empty executions table in pruning in migrations --- ...690000000002-MigrateIntegerKeysToString.ts | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/cli/src/databases/migrations/sqlite/1690000000002-MigrateIntegerKeysToString.ts b/packages/cli/src/databases/migrations/sqlite/1690000000002-MigrateIntegerKeysToString.ts index 48dfc3ad46f57..83b55787b2f51 100644 --- a/packages/cli/src/databases/migrations/sqlite/1690000000002-MigrateIntegerKeysToString.ts +++ b/packages/cli/src/databases/migrations/sqlite/1690000000002-MigrateIntegerKeysToString.ts @@ -208,20 +208,24 @@ const pruneExecutionsData = async ({ queryRunner, tablePrefix, logger }: Migrati } console.time('pruningData'); - const counting = (await queryRunner.query( - `select count(id) as rows from "${tablePrefix}execution_entity";`, - )) as Array<{ rows: number }>; - - const averageExecutionSize = dbFileSize / counting[0].rows; - 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 idToKeep = await queryRunner - .query(query) - .then((rows: Array<{ id: number }>) => rows[0].id); - - const removalQuery = `DELETE FROM "${tablePrefix}execution_entity" WHERE id < ${idToKeep} and status IN ('success')`; - await queryRunner.query(removalQuery); + const [{ rowCount }] = (await queryRunner.query( + `select count(id) as rowCount from "${tablePrefix}execution_entity";`, + )) as Array<{ rowCount: number }>; + + if (rowCount > 0) { + 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 idToKeep = await queryRunner + .query(query) + .then((rows: Array<{ id: number }>) => rows[0].id); + + const removalQuery = `DELETE FROM "${tablePrefix}execution_entity" WHERE id < ${idToKeep} and status IN ('success')`; + await queryRunner.query(removalQuery); + } console.timeEnd('pruningData'); } else { logger.debug('Pruning was requested, but was not enabled');