-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add backfill position job by workspace (#5725)
- Removing existing listener that was backfilling created records without position - Switch to a job that backfill all objects within workspace - Adapting `FIND_BY_POSITION` so it can fetch objects without position. Currently we needed to input a number
- Loading branch information
Showing
9 changed files
with
307 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...gine/api/graphql/workspace-query-runner/commands/0-20-record-position-backfill.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Inject } from '@nestjs/common'; | ||
|
||
import { Command, CommandRunner, Option } from 'nest-commander'; | ||
|
||
import { | ||
RecordPositionBackfillJob, | ||
RecordPositionBackfillJobData, | ||
} from 'src/engine/api/graphql/workspace-query-runner/jobs/record-position-backfill.job'; | ||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; | ||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; | ||
|
||
export type RecordPositionBackfillCommandOptions = { | ||
workspaceId: string; | ||
dryRun?: boolean; | ||
}; | ||
|
||
@Command({ | ||
name: 'migrate-0.20:backfill-record-position', | ||
description: 'Backfill record position', | ||
}) | ||
export class RecordPositionBackfillCommand extends CommandRunner { | ||
constructor( | ||
@Inject(MessageQueue.recordPositionBackfillQueue) | ||
private readonly messageQueueService: MessageQueueService, | ||
) { | ||
super(); | ||
} | ||
|
||
@Option({ | ||
flags: '-w, --workspace-id [workspace_id]', | ||
description: 'workspace id', | ||
required: true, | ||
}) | ||
parseWorkspaceId(value: string): string { | ||
return value; | ||
} | ||
|
||
@Option({ | ||
flags: '-d, --dry-run [dry run]', | ||
description: 'Dry run: Log backfill actions.', | ||
required: false, | ||
}) | ||
dryRun(value: string): boolean { | ||
return Boolean(value); | ||
} | ||
|
||
async run( | ||
_passedParam: string[], | ||
options: RecordPositionBackfillCommandOptions, | ||
): Promise<void> { | ||
this.messageQueueService.add<RecordPositionBackfillJobData>( | ||
RecordPositionBackfillJob.name, | ||
{ workspaceId: options.workspaceId, dryRun: options.dryRun ?? false }, | ||
{ retryLimit: 3 }, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 0 additions & 59 deletions
59
...erver/src/engine/api/graphql/workspace-query-runner/listeners/record-position.listener.ts
This file was deleted.
Oops, something went wrong.
139 changes: 139 additions & 0 deletions
139
...raphql/workspace-query-runner/services/__tests__/record-position-backfill-service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { TestingModule, Test } from '@nestjs/testing'; | ||
|
||
import { RecordPositionQueryFactory } from 'src/engine/api/graphql/workspace-query-builder/factories/record-position-query.factory'; | ||
import { RecordPositionFactory } from 'src/engine/api/graphql/workspace-query-runner/factories/record-position.factory'; | ||
import { RecordPositionBackfillService } from 'src/engine/api/graphql/workspace-query-runner/services/record-position-backfill-service'; | ||
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service'; | ||
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; | ||
|
||
describe('RecordPositionBackfillService', () => { | ||
let recordPositionQueryFactory; | ||
let recordPositionFactory; | ||
let objectMetadataService; | ||
let workspaceDataSourceService; | ||
|
||
let service: RecordPositionBackfillService; | ||
|
||
beforeEach(async () => { | ||
recordPositionQueryFactory = { | ||
create: jest.fn().mockReturnValue(['query', []]), | ||
}; | ||
|
||
recordPositionFactory = { | ||
create: jest.fn().mockResolvedValue([ | ||
{ | ||
position: 1, | ||
}, | ||
]), | ||
}; | ||
|
||
objectMetadataService = { | ||
findManyWithinWorkspace: jest.fn().mockReturnValue([]), | ||
}; | ||
|
||
workspaceDataSourceService = { | ||
getSchemaName: jest.fn().mockReturnValue('schemaName'), | ||
executeRawQuery: jest.fn().mockResolvedValue([]), | ||
}; | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
RecordPositionBackfillService, | ||
{ | ||
provide: RecordPositionQueryFactory, | ||
useValue: recordPositionQueryFactory, | ||
}, | ||
{ | ||
provide: RecordPositionFactory, | ||
useValue: recordPositionFactory, | ||
}, | ||
{ | ||
provide: WorkspaceDataSourceService, | ||
useValue: workspaceDataSourceService, | ||
}, | ||
{ | ||
provide: ObjectMetadataService, | ||
useValue: objectMetadataService, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<RecordPositionBackfillService>( | ||
RecordPositionBackfillService, | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
it('when no object metadata found, should do nothing', async () => { | ||
await service.backfill('workspaceId', false); | ||
expect(workspaceDataSourceService.executeRawQuery).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('when objectMetadata without position, should do nothing', async () => { | ||
objectMetadataService.findManyWithinWorkspace.mockReturnValue([ | ||
{ | ||
id: '1', | ||
nameSingular: 'name', | ||
fields: [], | ||
}, | ||
]); | ||
await service.backfill('workspaceId', false); | ||
expect(workspaceDataSourceService.executeRawQuery).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('when objectMetadata but all record with position, should create and run query once', async () => { | ||
objectMetadataService.findManyWithinWorkspace.mockReturnValue([ | ||
{ | ||
id: '1', | ||
nameSingular: 'company', | ||
fields: [], | ||
}, | ||
]); | ||
await service.backfill('workspaceId', false); | ||
expect(workspaceDataSourceService.executeRawQuery).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('when record without position, should create and run query twice', async () => { | ||
objectMetadataService.findManyWithinWorkspace.mockReturnValue([ | ||
{ | ||
id: '1', | ||
nameSingular: 'company', | ||
fields: [], | ||
}, | ||
]); | ||
workspaceDataSourceService.executeRawQuery.mockResolvedValueOnce([ | ||
{ | ||
id: '1', | ||
}, | ||
]); | ||
await service.backfill('workspaceId', false); | ||
expect(workspaceDataSourceService.executeRawQuery).toHaveBeenCalledTimes(2); | ||
expect(recordPositionFactory.create).toHaveBeenCalledTimes(1); | ||
expect(recordPositionQueryFactory.create).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('when dryRun is true, should not update position', async () => { | ||
objectMetadataService.findManyWithinWorkspace.mockReturnValue([ | ||
{ | ||
id: '1', | ||
nameSingular: 'company', | ||
fields: [], | ||
}, | ||
]); | ||
workspaceDataSourceService.executeRawQuery.mockResolvedValueOnce([ | ||
{ | ||
id: '1', | ||
}, | ||
]); | ||
await service.backfill('workspaceId', true); | ||
expect(workspaceDataSourceService.executeRawQuery).toHaveBeenCalledTimes(1); | ||
expect(recordPositionFactory.create).toHaveBeenCalledTimes(1); | ||
expect(recordPositionQueryFactory.create).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.