Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add verbose flag to bulk delete and upsert #615

Merged
merged 6 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions messages/bulk.operation.command.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ Number of minutes to wait for the command to complete before displaying the resu
# flags.async.summary

Run the command asynchronously.

# flags.verbose

Print verbose output of failed records if result is available.
36 changes: 33 additions & 3 deletions src/bulkOperationCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import * as os from 'os';
import { Flags } from '@salesforce/sf-plugins-core';
import { Duration } from '@salesforce/kit';
import { Connection, Messages } from '@salesforce/core';
import { BulkOperation, IngestJobV2, IngestOperation, JobInfoV2 } from 'jsforce/api/bulk';
import { ux } from '@oclif/core';
import { BulkOperation, IngestJobV2, IngestJobV2FailedResults, IngestOperation, JobInfoV2 } from 'jsforce/api/bulk';
import { Schema } from 'jsforce';
import { orgFlags } from './flags';
import { BulkDataRequestCache } from './bulkDataRequestCache';
Expand Down Expand Up @@ -60,6 +61,10 @@ export abstract class BulkOperationCommand extends BulkBaseCommand {
default: false,
exclusive: ['wait'],
}),
verbose: Flags.boolean({
summary: messages.getMessage('flags.verbose'),
default: false,
R0Wi marked this conversation as resolved.
Show resolved Hide resolved
}),
};

/**
Expand Down Expand Up @@ -93,11 +98,26 @@ export abstract class BulkOperationCommand extends BulkBaseCommand {
return job.check();
}

private printBulkErrors(failedResults: IngestJobV2FailedResults<Schema>): void {
const columns = {
id: { header: 'Id' },
error: { header: 'Error' },
};
const options = { title: `Bulk Failures [${failedResults.length}]` };
ux.log();
R0Wi marked this conversation as resolved.
Show resolved Hide resolved
ux.table(
failedResults.map((f) => ({ id: f.sf__Id, error: f.sf__Error })),
columns,
options
);
}

public async runBulkOperation(
sobject: string,
csvFileName: string,
connection: Connection,
wait: number,
verbose: boolean,
operation: BulkOperation,
options?: { extIdField: string }
): Promise<BulkResultV2> {
Expand Down Expand Up @@ -133,10 +153,20 @@ export abstract class BulkOperationCommand extends BulkBaseCommand {
}
this.displayBulkV2Result(jobInfo);
const result = { jobInfo } as BulkResultV2;
if (!isBulkV2RequestDone(jobInfo) || !this.jsonEnabled()) {
if (!isBulkV2RequestDone(jobInfo)) {
return result;
}
let records = null;
if (verbose) {
R0Wi marked this conversation as resolved.
Show resolved Hide resolved
records = await this.job.getAllResults();
if (records?.failedResults?.length > 0) {
this.printBulkErrors(records.failedResults);
}
}
if (!this.jsonEnabled()) {
return result;
}
result.records = transformResults(await this.job.getAllResults());
result.records = transformResults(records ?? await this.job.getAllResults());
return result;
} catch (err) {
this.spinner.stop();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/data/delete/bulk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class Delete extends BulkOperationCommand {

await validateSobjectType(flags.sobject, conn);

return this.runBulkOperation(flags.sobject, flags.file, conn, flags.async ? 0 : flags.wait?.minutes, 'delete');
return this.runBulkOperation(flags.sobject, flags.file, conn, flags.async ? 0 : flags.wait?.minutes, flags.verbose, 'delete');
}

// eslint-disable-next-line class-methods-use-this
Expand Down
2 changes: 1 addition & 1 deletion src/commands/data/upsert/bulk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class Upsert extends BulkOperationCommand {

await validateSobjectType(flags.sobject, conn);

return this.runBulkOperation(flags.sobject, flags.file, conn, flags.async ? 0 : flags.wait?.minutes, 'upsert', {
return this.runBulkOperation(flags.sobject, flags.file, conn, flags.async ? 0 : flags.wait?.minutes, flags.verbose, 'upsert', {
extIdField: flags['external-id'],
});
}
Expand Down