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 a soqlqueryfile parameter to allow longer queries #327

Merged
merged 3 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
40 changes: 30 additions & 10 deletions command-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
{
"command": "force:data:bulk:delete",
"plugin": "@salesforce/plugin-data",
"flags": ["apiversion", "csvfile", "json", "loglevel", "sobjecttype", "targetusername", "wait"]
"flags": ["apiversion", "csvfile", "json", "loglevel", "sobjecttype", "targetusername", "wait"],
"alias": []
},
{
"command": "force:data:bulk:status",
"plugin": "@salesforce/plugin-data",
"flags": ["apiversion", "batchid", "jobid", "json", "loglevel", "targetusername"]
"flags": ["apiversion", "batchid", "jobid", "json", "loglevel", "targetusername"],
"alias": []
},
{
"command": "force:data:bulk:upsert",
Expand All @@ -22,12 +24,14 @@
"sobjecttype",
"targetusername",
"wait"
]
],
"alias": []
},
{
"command": "force:data:record:create",
"plugin": "@salesforce/plugin-data",
"flags": ["apiversion", "json", "loglevel", "perflog", "sobjecttype", "targetusername", "usetoolingapi", "values"]
"flags": ["apiversion", "json", "loglevel", "perflog", "sobjecttype", "targetusername", "usetoolingapi", "values"],
"alias": []
},
{
"command": "force:data:record:delete",
Expand All @@ -42,7 +46,8 @@
"targetusername",
"usetoolingapi",
"where"
]
],
"alias": []
},
{
"command": "force:data:record:get",
Expand All @@ -57,7 +62,8 @@
"targetusername",
"usetoolingapi",
"where"
]
],
"alias": []
},
{
"command": "force:data:record:update",
Expand All @@ -73,17 +79,30 @@
"usetoolingapi",
"values",
"where"
]
],
"alias": []
},
{
"command": "force:data:soql:query",
"plugin": "@salesforce/plugin-data",
"flags": ["apiversion", "json", "loglevel", "perflog", "query", "resultformat", "targetusername", "usetoolingapi"]
"flags": [
"apiversion",
"json",
"loglevel",
"perflog",
"query",
"resultformat",
"soqlqueryfile",
"targetusername",
"usetoolingapi"
],
"alias": []
},
{
"command": "force:data:tree:export",
"plugin": "@salesforce/plugin-data",
"flags": ["apiversion", "json", "loglevel", "outputdir", "plan", "prefix", "query", "targetusername"]
"flags": ["apiversion", "json", "loglevel", "outputdir", "plan", "prefix", "query", "targetusername"],
"alias": []
},
{
"command": "force:data:tree:import",
Expand All @@ -97,6 +116,7 @@
"plan",
"sobjecttreefiles",
"targetusername"
]
],
"alias": []
}
]
5 changes: 4 additions & 1 deletion messages/soql.query.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
"queryNoResults": "Your query returned no results.",
"queryRunningMessage": "Querying Data",
"queryMoreUpdateMessage": "Result size is %d, current count is %d",
"soqlqueryfile": "A SOQL query stored in a file",
"examples": [
"$ sfdx force:data:soql:query -q \"SELECT Id, Name, Account.Name FROM Contact\"",
"$ sfdx force:data:soql:query -q \"SELECT Id, Name FROM Account WHERE ShippingState IN ('CA', 'NY')\"",
"$ sfdx force:data:soql:query -q \"SELECT Id, Name FROM Account WHERE ShippingState IN ('CA', 'NY')\" --perflog --json",
"$ sfdx force:data:soql:query -q \"SELECT Name FROM ApexTrigger\" -t"
"$ sfdx force:data:soql:query -q \"SELECT Name FROM ApexTrigger\" -t",
"$ sfdx force:data:soql:query --soqlqueryfile query.txt",
"$ sfdx force:data:soql:query --soqlqueryfile query.txt -t"
]
}
13 changes: 11 additions & 2 deletions src/commands/force/data/soql/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import * as os from 'os';
import * as fs from 'fs';
import { flags, FlagsConfig } from '@salesforce/command';
import { CliUx } from '@oclif/core';
import { Connection, Logger, Messages, SfdxConfigAggregator } from '@salesforce/core';
Expand Down Expand Up @@ -182,8 +183,15 @@ export class DataSoqlQueryCommand extends DataCommand {
public static readonly flagsConfig: FlagsConfig = {
query: flags.string({
char: 'q',
required: true,
cristiand391 marked this conversation as resolved.
Show resolved Hide resolved
description: messages.getMessage('queryToExecute'),
exclusive: ['soqlqueryfile'],
exactlyOne: ['query', 'soqlqueryfile'],
}),
soqlqueryfile: flags.filepath({
char: 'f',
description: messages.getMessage('soqlqueryfile'),
exclusive: ['query'],
exactlyOne: ['query', 'soqlqueryfile'],
}),
usetoolingapi: flags.boolean({
char: 't',
Expand Down Expand Up @@ -220,9 +228,10 @@ export class DataSoqlQueryCommand extends DataCommand {
if (this.flags.resultformat !== 'json') this.ux.startSpinner(messages.getMessage('queryRunningMessage'));
const query = new SoqlQuery();
const conn = this.getConnection();
const queryString = (this.flags.query as string) ?? fs.readFileSync(this.flags.soqlqueryfile, 'utf8');
const queryResult: SoqlQueryResult = await query.runSoqlQuery(
conn as Connection,
this.flags.query,
queryString,
this.logger,
this.configAggregator
);
Expand Down
2 changes: 0 additions & 2 deletions test/commands/force/data/bulk/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ interface StatusResult {

describe('force:data:bulk:status', () => {
test
.skip()
.do(() => {
const Job = {
getAuthInfoFields: () => {
Expand Down Expand Up @@ -70,7 +69,6 @@ describe('force:data:bulk:status', () => {
});

test
.skip()
.do(() => {
const Job = {
getAuthInfoFields: () => {
Expand Down
28 changes: 26 additions & 2 deletions test/commands/force/data/soql/query/dataSoqlQuery.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as path from 'path';
import * as fs from 'fs';
import * as shell from 'shelljs';
import { isArray, AnyJson, ensureString } from '@salesforce/ts-types';
import { AnyJson, Dictionary, ensureString, getString, isArray } from '@salesforce/ts-types';
import { expect } from 'chai';
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
import { Dictionary, getString } from '@salesforce/ts-types';

export interface QueryResult {
totalSize: number;
Expand Down Expand Up @@ -116,6 +116,16 @@ describe('data:soql:query command', () => {
expect(stdError).to.include('unexpected token');
});

it('should produce correct error when invalid soql provided', () => {
const filepath = path.join(testSession.dir, 'soql.txt');
cristiand391 marked this conversation as resolved.
Show resolved Hide resolved
fs.writeFileSync(filepath, 'SELECT');

const result = execCmd(`force:data:soql:query --soqlqueryfile ${filepath}`, { ensureExitCode: 1 }).shellOutput
.stderr;
const stdError = result?.toLowerCase();
expect(stdError).to.include('unexpected token');
});

it('should error with no such column', () => {
// querying ApexClass including SymbolTable column w/o using tooling api will cause "no such column" error
const result = runQuery('SELECT Id, Name, SymbolTable from ApexClass', {
Expand Down Expand Up @@ -194,6 +204,20 @@ describe('data:soql:query command', () => {
expect(queryResult).to.match(/ID\s+?NAME\s+?PHONE\s+?WEBSITE\s+?NUMBEROFEMPLOYEES\s+?INDUSTRY/g);
expect(queryResult).to.match(/Total number of records retrieved: 1\./g);
});

it('should return account records, from --soqlqueryfile', () => {
const query =
"SELECT Id, Name, Phone, Website, NumberOfEmployees, Industry FROM Account WHERE Name LIKE 'SampleAccount%' limit 1";
const filepath = path.join(testSession.dir, 'soql.txt');
fs.writeFileSync(filepath, query);

const queryResult = execCmd(`force:data:soql:query --soqlqueryfile ${filepath}`, { ensureExitCode: 0 })
.shellOutput.stdout;

expect(queryResult).to.match(/ID\s+?NAME\s+?PHONE\s+?WEBSITE\s+?NUMBEROFEMPLOYEES\s+?INDUSTRY/g);
expect(queryResult).to.match(/Total number of records retrieved: 1\./g);
});

it('should return account records with nested contacts', () => {
const query =
"SELECT Id, Name, Phone, Website, NumberOfEmployees, Industry, (SELECT Lastname, Title, Email FROM Contacts) FROM Account WHERE Name LIKE 'SampleAccount%'";
Expand Down
19 changes: 18 additions & 1 deletion test/commands/force/data/soql/query/dataSoqlQueryCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface QueryResult {
result: { totalSize: number; records: [] };
}

describe.skip('Execute a SOQL statement', function (): void {
describe('Execute a SOQL statement', function (): void {
let sandbox: SinonSandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -148,6 +148,23 @@ describe.skip('Execute a SOQL statement', function (): void {
expect(ctx.stdout).to.include('records retrieved: 1');
});
});
describe('flag validation between --query and --soqlqueryfile', () => {
test
.withOrg({ username: '[email protected]' }, true)
.stderr()
.command([
QUERY_COMMAND,
'--targetusername',
'[email protected]',
'--query',
'SELECT Amount, Id, Name,StageName, CloseDate, (SELECT Id, ListPrice, PriceBookEntry.UnitPrice, PricebookEntry.Name, PricebookEntry.Id, PricebookEntry.product2.Family FROM OpportunityLineItems) FROM Opportunity',
'--soqlqueryfile',
'soql.txt',
])
.it('should throw an error when both query (inline/file query) flags are specified', (ctx) => {
expect(ctx.stderr).to.include('--soqlqueryfile= cannot also be provided when using --query=');
});
});
describe('reporters produce the correct aggregate query', () => {
beforeEach(() => {
soqlQuerySpy = sandbox
Expand Down