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: sourceRecordsFilter option added #936

Merged
merged 3 commits into from
Nov 28, 2024
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
1 change: 1 addition & 0 deletions messages/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
"writingToFile": "{%s} Creating the file %s ...",
"nothingUpdated": "Nothing was updated.",
"skippedUpdatesWarning": "{%s} %s target records remained untouched, since they do not differ from the corresponding source records.",
"skippedSourceRecordsFilterWarning": "One 'sourceRecordsFilter' could not be applied: %s.",
"skippedTargetRecordsFilterWarning": "One 'targetRecordsFilter' could not be applied: %s.",
"missingParentLookupsPrompt": "{%s} %s missing parent lookup records were found. See %s file for the details.",
"updatingSummary": "Data processing summary.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default interface ISfdmuRunCustomAddonScriptObject {
hardDelete?: boolean;
updateWithMockData?: boolean;
//mockCSVData?: boolean;
sourceRecordsFilter?: string;
targetRecordsFilter?: string;
excluded?: boolean;
useCSVValuesMapping?: boolean;
Expand Down
25 changes: 25 additions & 0 deletions src/modules/components/common_components/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1860,5 +1860,30 @@ export class Common {
return new Promise(resolve => setTimeout(resolve, time));
}

private static wrapWhereClauseInParenthesis(clause: WhereClause): { beginClause: WhereClause, endClause: WhereClause } {
const clone = JSON.parse(JSON.stringify(clause)) as WhereClause;
clone.left.openParen = (clone.left.openParen ?? 0) + 1
let current = clone;
while (current.right) {
current = current.right;
}
current.left.closeParen = (current.left.closeParen || 0) + 1
return { beginClause: clone, endClause: current };
}

static mergeWhereClauses(
where1?: WhereClause,
where2?: WhereClause,
operator: LogicalOperator = 'AND',
): WhereClause | undefined {
if (!where1 || !where2) return where1 || where2;

const { beginClause: wrappedWhere1, endClause: endClause1 } = Common.wrapWhereClauseInParenthesis(where1);
const { beginClause: wrappedWhere2 } = Common.wrapWhereClauseInParenthesis(where2);

endClause1.operator = operator;
endClause1.right = wrappedWhere2;

return wrappedWhere1;
}
}
1 change: 1 addition & 0 deletions src/modules/components/common_components/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export enum RESOURCES {
writingToFile = "writingToFile",
nothingUpdated = "nothingUpdated",
skippedUpdatesWarning = "skippedUpdatesWarning",
skippedSourceRecordsFilterWarning = "skippedSourceRecordsFilterWarning",
skippedTargetRecordsFilterWarning = "skippedTargetRecordsFilterWarning",
missingParentLookupsPrompt = "missingParentLookupsPrompt",
updatingSummary = "updatingSummary",
Expand Down
8 changes: 8 additions & 0 deletions src/modules/models/job_models/migrationJobTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,14 @@ export default class MigrationJobTask {
if (isTargetQuery) {
// Fix target query
___filterTargetQuery(tempQuery);
} else if (this.scriptObject.sourceRecordsFilter) {
// Add any extra filter conditions to the source query
try {
const additionalWhereClause = parseQuery(`SELECT Id FROM ${this.sObjectName} WHERE ${this.scriptObject.sourceRecordsFilter}`).where;
tempQuery.where = Common.mergeWhereClauses(tempQuery.where, additionalWhereClause);
} catch (ex) {
self.logger.warn(RESOURCES.skippedSourceRecordsFilterWarning, ex.message);
}
}

let query = composeQuery(tempQuery);
Expand Down
1 change: 1 addition & 0 deletions src/modules/models/script_models/scriptObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default class ScriptObject implements ISfdmuRunScriptObject {
hardDelete: boolean = false;
updateWithMockData: boolean = false;
//mockCSVData: boolean = false;
sourceRecordsFilter: string = "";
targetRecordsFilter: string = "";
excluded: boolean = false;
useCSVValuesMapping: boolean = false;
Expand Down