From ce998dbca585d1171c4b4734eb49fb91b2824528 Mon Sep 17 00:00:00 2001 From: Ivan Kiral Date: Thu, 22 Jun 2023 17:01:52 +0200 Subject: [PATCH 1/6] Fix read from plugin returning empty object --- src/utils/statusManager.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/utils/statusManager.ts b/src/utils/statusManager.ts index 9736304..54cc3ee 100644 --- a/src/utils/statusManager.ts +++ b/src/utils/statusManager.ts @@ -6,7 +6,6 @@ import { type StatusPlugin } from './status/statusPlugin'; const migrationStatusFilename = 'status.json'; const pluginsFilename = 'plugins.js'; -// let status: IStatus = {}; const updateMigrationStatus = async (status: IStatus, projectId: string, migrationStatus: IMigrationStatus, saveStatusFromPlugin: StatusPlugin['saveStatus'] | null) => { status[projectId] = status[projectId] === undefined ? [] : status[projectId]; @@ -40,9 +39,9 @@ const saveStatusFile = async (migrationsStatus: IStatus, saveStatusFromPlugin: S if (saveStatusFromPlugin) { try { await saveStatusFromPlugin(statusJSON); + return; } catch (e) { console.error(`The error ${e} occured when using saveStatus function from plugin.`); - } finally { return; } } @@ -76,7 +75,6 @@ export const loadMigrationsExecutionStatus = async (readStatusFromPlugin: Status return await readStatusFromPlugin(); } catch (e) { console.error(`The error ${e} occured when using readStatus function from plugin.`); - } finally { return {}; } } From 8480fd180f68cdaee4724ea3145df2076069bd56 Mon Sep 17 00:00:00 2001 From: Ivan Kiral Date: Thu, 22 Jun 2023 17:02:09 +0200 Subject: [PATCH 2/6] Fix loging false when running the migration --- src/utils/migrationUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/migrationUtils.ts b/src/utils/migrationUtils.ts index 2195905..3fad95c 100644 --- a/src/utils/migrationUtils.ts +++ b/src/utils/migrationUtils.ts @@ -59,7 +59,7 @@ interface RunMigrationOptions { export const runMigration = async (migrationsStatus: IStatus, migration: IMigration, options: RunMigrationOptions): Promise => { const { client, projectId, operation, saveStatusFromPlugin } = options; - console.log(`Running the ${operation === 'rollback' && 'rollback of'} ${migration.name} migration.`); + console.log(`Running the ${operation === 'rollback' ? 'rollback of': ''} ${migration.name} migration.`); let isSuccess = true; @@ -115,7 +115,7 @@ export const runMigration = async (migrationsStatus: IStatus, migration: IMigrat return 1; } - console.log(chalk.green(`The \"${migration.name}\" migration on a project with ID \"${projectId}\" executed successfully.`)); + console.log(chalk.green(`The ${operation === 'rollback' ? 'rollback of ': ''}\"${migration.name}\" migration on a project with ID \"${projectId}\" executed successfully.`)); return 0; }; From 3dd536cef0df58645d2512b9e4e59161e8b35085 Mon Sep 17 00:00:00 2001 From: Ivan Kiral Date: Thu, 22 Jun 2023 17:33:25 +0200 Subject: [PATCH 3/6] Rewrite test --- src/tests/statusManager.test.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/tests/statusManager.test.ts b/src/tests/statusManager.test.ts index 771b2f9..cfbbb36 100644 --- a/src/tests/statusManager.test.ts +++ b/src/tests/statusManager.test.ts @@ -49,13 +49,15 @@ describe('Status manager', () => { }); it('loadMigrationsExecutionStatus to be called with plugins', async () => { - jest.spyOn(fileUtils, 'fileExists').mockReturnValue(true); + const expectedStatus = { "30816c62-8d41-4dc4-a1ab-40440070b7bf": [] }; - const readStatusMocked = jest.fn().mockResolvedValue({}); + const readStatusPlugin = async () => { + return expectedStatus; + }; - await statusManager.loadMigrationsExecutionStatus(readStatusMocked); + const returnedStatus = await statusManager.loadMigrationsExecutionStatus(readStatusPlugin); - expect(readStatusMocked).toHaveBeenCalled(); + expect(returnedStatus).toEqual(expectedStatus); }); it('MarkAsCompleted to be called with plugins', async () => { From e8cef66be92e96a25078651cc4a2264925dd9c76 Mon Sep 17 00:00:00 2001 From: Ivan Kiral Date: Thu, 22 Jun 2023 17:35:33 +0200 Subject: [PATCH 4/6] fix lint --- src/tests/statusManager.test.ts | 2 +- src/utils/migrationUtils.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tests/statusManager.test.ts b/src/tests/statusManager.test.ts index cfbbb36..3d85821 100644 --- a/src/tests/statusManager.test.ts +++ b/src/tests/statusManager.test.ts @@ -49,7 +49,7 @@ describe('Status manager', () => { }); it('loadMigrationsExecutionStatus to be called with plugins', async () => { - const expectedStatus = { "30816c62-8d41-4dc4-a1ab-40440070b7bf": [] }; + const expectedStatus = { '30816c62-8d41-4dc4-a1ab-40440070b7bf': [] }; const readStatusPlugin = async () => { return expectedStatus; diff --git a/src/utils/migrationUtils.ts b/src/utils/migrationUtils.ts index 3fad95c..b39dab2 100644 --- a/src/utils/migrationUtils.ts +++ b/src/utils/migrationUtils.ts @@ -59,7 +59,7 @@ interface RunMigrationOptions { export const runMigration = async (migrationsStatus: IStatus, migration: IMigration, options: RunMigrationOptions): Promise => { const { client, projectId, operation, saveStatusFromPlugin } = options; - console.log(`Running the ${operation === 'rollback' ? 'rollback of': ''} ${migration.name} migration.`); + console.log(`Running the ${operation === 'rollback' ? 'rollback of' : ''} ${migration.name} migration.`); let isSuccess = true; @@ -115,7 +115,7 @@ export const runMigration = async (migrationsStatus: IStatus, migration: IMigrat return 1; } - console.log(chalk.green(`The ${operation === 'rollback' ? 'rollback of ': ''}\"${migration.name}\" migration on a project with ID \"${projectId}\" executed successfully.`)); + console.log(chalk.green(`The ${operation === 'rollback' ? 'rollback of ' : ''}\"${migration.name}\" migration on a project with ID \"${projectId}\" executed successfully.`)); return 0; }; From 0b6ccb6c8d87f31133456cdecf17aad6b7c47e94 Mon Sep 17 00:00:00 2001 From: Ivan Kiral Date: Fri, 23 Jun 2023 10:47:26 +0200 Subject: [PATCH 5/6] change flow of reading/saving status to throw when not sucessfull --- src/cmds/migration/run.ts | 8 +++++++- src/tests/statusManager.test.ts | 10 ++++++++++ src/utils/statusManager.ts | 12 ++++-------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/cmds/migration/run.ts b/src/cmds/migration/run.ts index a883ebc..2bc1587 100644 --- a/src/cmds/migration/run.ts +++ b/src/cmds/migration/run.ts @@ -150,7 +150,13 @@ const runMigrationCommand: yargs.CommandModule = { saveStatusFromPlugin: plugin?.saveStatus ?? null, }; - const migrationsStatus = await loadMigrationsExecutionStatus(plugin?.readStatus ?? null); + let migrationsStatus: IStatus; + try { + migrationsStatus = await loadMigrationsExecutionStatus(plugin?.readStatus ?? null); + } catch (e) { + console.error(`An error ${chalk.red(e)} occured when trying to read status`); + process.exit(1); + } if (runAll || runRange) { let migrationsToRun = await loadMigrationFiles(); diff --git a/src/tests/statusManager.test.ts b/src/tests/statusManager.test.ts index 3d85821..5bb12be 100644 --- a/src/tests/statusManager.test.ts +++ b/src/tests/statusManager.test.ts @@ -60,6 +60,16 @@ describe('Status manager', () => { expect(returnedStatus).toEqual(expectedStatus); }); + it('loadMigrationsExecutionStatus to be called with plugin that throw', async () => { + const readStatusPlugin = async () => { + throw new Error('Error during plugin function'); + }; + + expect.assertions(1); + + return statusManager.loadMigrationsExecutionStatus(readStatusPlugin).catch((e) => expect((e as Error).message).toMatch('Error during plugin function')); + }); + it('MarkAsCompleted to be called with plugins', async () => { jest.spyOn(fileUtils, 'fileExists').mockReturnValue(true); diff --git a/src/utils/statusManager.ts b/src/utils/statusManager.ts index 54cc3ee..739f513 100644 --- a/src/utils/statusManager.ts +++ b/src/utils/statusManager.ts @@ -41,8 +41,9 @@ const saveStatusFile = async (migrationsStatus: IStatus, saveStatusFromPlugin: S await saveStatusFromPlugin(statusJSON); return; } catch (e) { - console.error(`The error ${e} occured when using saveStatus function from plugin.`); - return; + console.error(`The error ${e} occured when using saveStatus function from plugin. Fallbacking to write status into status.json`); + saveStatusToFile(statusJSON); + throw new Error((e as Error).message); } } @@ -71,12 +72,7 @@ const getStatusFilepath = (): string => { export const loadMigrationsExecutionStatus = async (readStatusFromPlugin: StatusPlugin['readStatus'] | null): Promise => { if (readStatusFromPlugin) { - try { - return await readStatusFromPlugin(); - } catch (e) { - console.error(`The error ${e} occured when using readStatus function from plugin.`); - return {}; - } + return await readStatusFromPlugin(); } return readFromStatus(); From e1aba3380986e9efe636acf814ab5a08fe3276e4 Mon Sep 17 00:00:00 2001 From: Ivan Kiral Date: Fri, 23 Jun 2023 11:26:00 +0200 Subject: [PATCH 6/6] update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cac26fe..2a17463 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kontent-ai/cli", - "version": "0.7.4", + "version": "0.7.5", "description": "Command line interface tool that can be used for generating and runningKontent.ai migration scripts", "main": "./lib/index.js", "types": "./lib/types/index.d.ts",