Skip to content

Commit 4b52c1c

Browse files
#RI-4669 - add delete all workbench history endpoint (#2387)
1 parent 2b5b005 commit 4b52c1c

File tree

7 files changed

+119
-0
lines changed

7 files changed

+119
-0
lines changed

redisinsight/api/src/modules/workbench/repositories/command-execution.repository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export abstract class CommandExecutionRepository {
66
abstract getList(databaseId: string): Promise<ShortCommandExecution[]>;
77
abstract getOne(databaseId: string, id: string): Promise<CommandExecution>;
88
abstract delete(databaseId: string, id: string): Promise<void>;
9+
abstract deleteAll(databaseId: string): Promise<void>;
910
}

redisinsight/api/src/modules/workbench/repositories/local-command-execution.repository.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ describe('LocalCommandExecutionRepository', () => {
253253
);
254254
});
255255
});
256+
describe('deleteAll', () => {
257+
it('Should not return anything on delete', async () => {
258+
repository.delete.mockResolvedValueOnce(1);
259+
expect(await service.deleteAll(mockDatabase.id)).toEqual(
260+
undefined,
261+
);
262+
});
263+
});
256264
describe('cleanupDatabaseHistory', () => {
257265
it('Should should not return anything on cleanup', async () => {
258266
mockQueryBuilderGetManyRaw.mockReturnValueOnce([

redisinsight/api/src/modules/workbench/repositories/local-command-execution.repository.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ export class LocalCommandExecutionRepository extends CommandExecutionRepository
163163
this.logger.log('Command execution deleted');
164164
}
165165

166+
/**
167+
* Delete all items
168+
*
169+
* @param databaseId
170+
*/
171+
async deleteAll(databaseId: string): Promise<void> {
172+
this.logger.log('Delete all command executions');
173+
174+
await this.commandExecutionRepository.delete({ databaseId });
175+
176+
this.logger.log('Command executions deleted');
177+
}
178+
166179
/**
167180
* Clean history for particular database to fit 30 items limitation
168181
* @param databaseId

redisinsight/api/src/modules/workbench/workbench.controller.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,16 @@ export class WorkbenchController {
9898
): Promise<void> {
9999
return this.service.deleteCommandExecution(databaseId, id);
100100
}
101+
102+
@ApiEndpoint({
103+
description: 'Delete command executions',
104+
statusCode: 200,
105+
})
106+
@Delete('/command-executions')
107+
@ApiRedisParams()
108+
async deleteCommandExecutions(
109+
@Param('dbInstance') databaseId: string,
110+
): Promise<void> {
111+
return this.service.deleteCommandExecutions(databaseId);
112+
}
101113
}

redisinsight/api/src/modules/workbench/workbench.service.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ const mockCommandExecutionRepository = () => ({
134134
getList: jest.fn(),
135135
getOne: jest.fn(),
136136
delete: jest.fn(),
137+
deleteAll: jest.fn(),
137138
});
138139

139140
describe('WorkbenchService', () => {
@@ -389,6 +390,17 @@ describe('WorkbenchService', () => {
389390
mockCommandExecution.id,
390391
);
391392

393+
expect(result).toEqual(undefined);
394+
});
395+
});
396+
describe('deleteCommandExecutions', () => {
397+
it('should not return anything on delete', async () => {
398+
commandExecutionProvider.deleteAll.mockResolvedValueOnce('some response');
399+
400+
const result = await service.deleteCommandExecutions(
401+
mockWorkbenchClientMetadata.databaseId,
402+
);
403+
392404
expect(result).toEqual(undefined);
393405
});
394406
});

redisinsight/api/src/modules/workbench/workbench.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ export class WorkbenchService {
187187
this.analyticsService.sendCommandDeletedEvent(databaseId);
188188
}
189189

190+
/**
191+
* Delete command executions by databaseId
192+
*
193+
* @param databaseId
194+
*/
195+
async deleteCommandExecutions(databaseId: string): Promise<void> {
196+
await this.commandExecutionRepository.deleteAll(databaseId);
197+
}
198+
190199
/**
191200
* Check if workbench allows such command
192201
* @param commandLine
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
expect,
3+
describe,
4+
it,
5+
deps,
6+
validateApiCall,
7+
} from '../deps';
8+
const { server, request, constants, rte, localDb } = deps;
9+
10+
// endpoint to test
11+
const endpoint = (
12+
instanceId = constants.TEST_INSTANCE_ID,
13+
) =>
14+
request(server).delete(`/${constants.API.DATABASES}/${instanceId}/workbench/command-executions`);
15+
16+
const mainCheckFn = async (testCase) => {
17+
it(testCase.name, async () => {
18+
// additional checks before test run
19+
if (testCase.before) {
20+
await testCase.before();
21+
}
22+
23+
await validateApiCall({
24+
endpoint,
25+
...testCase,
26+
});
27+
28+
// additional checks after test pass
29+
if (testCase.after) {
30+
await testCase.after();
31+
}
32+
});
33+
};
34+
35+
describe('DELETE /databases/:instanceId/workbench/command-executions', () => {
36+
describe('Common', () => {
37+
[
38+
{
39+
name: 'Should return 404 not found when incorrect instance',
40+
endpoint: () => endpoint(
41+
constants.TEST_NOT_EXISTED_INSTANCE_ID,
42+
),
43+
statusCode: 404,
44+
responseBody: {
45+
statusCode: 404,
46+
message: 'Invalid database instance id.',
47+
error: 'Not Found'
48+
},
49+
},
50+
{
51+
name: 'Should return 0 array when no history items yet',
52+
before: async () => {
53+
await localDb.generateNCommandExecutions({
54+
databaseId: constants.TEST_INSTANCE_ID,
55+
id: constants.TEST_COMMAND_EXECUTION_ID_1,
56+
}, 2);
57+
},
58+
after: async () => {
59+
expect(await (await localDb.getRepository(localDb.repositories.COMMAND_EXECUTION)).count({})).to.eq(0)
60+
},
61+
},
62+
].map(mainCheckFn);
63+
});
64+
});

0 commit comments

Comments
 (0)