Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

fix(profile): add ability to reset cache when the command is executed against an org multiple times #1422

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion packages/sfpowerscripts-cli/messages/profile_merge.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"folderFlagDescription": "comma separated list of folders to scan for profiles. If ommited, the folders in the packageDirectories configuration will be used.",
"profileListFlagDescription": "comma separated list of profiles. If ommited, all the profiles found in the folder(s) will be merged",
"metadataFlagDescription": "comma separated list of metadata for which the permissions will be retrieved.",
"deleteFlagDescription": "set this flag to delete profile files that does not exist in the org."
"deleteFlagDescription": "set this flag to delete profile files that does not exist in the org.",
"resetCacheFlagDescription": "set this flag to reset the cache and retrieve the latest profile permissions from the org."
}
3 changes: 2 additions & 1 deletion packages/sfpowerscripts-cli/messages/profile_reconcile.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"nameFlagDescription": "list of profiles to be reconciled. If ommited, all the profiles components will be reconciled.",
"destFolderFlagDescription": " the destination folder for reconciled profiles, if omitted existing profiles will be reconciled and will be rewritten in the current location",
"sourceonlyFlagDescription": "set this flag to reconcile profiles only against component available in the project only. Configure ignored perissions in sfdx-project.json file in the array plugins->sfpowerkit->ignoredPermissions.",
"targetorgFlagDescription": " org against which profiles will be reconciled. this parameter can be ommited if sourceonly flag is set."
"targetorgFlagDescription": " org against which profiles will be reconciled. this parameter can be ommited if sourceonly flag is set.",
"resetCacheFlagDescription": "set this flag to reset the cache and retrieve the latest profile permissions from the org."
}
14 changes: 5 additions & 9 deletions packages/sfpowerscripts-cli/src/commands/profile/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export default class Merge extends SfpowerscriptsCommand {
description: messages.getMessage('deleteFlagDescription'),
required: false,
}),
resetcache: Flags.boolean({
description: messages.getMessage('resetCacheFlagDescription'),
required: false,
}),
targetorg: requiredUserNameFlag,
'apiversion': orgApiVersionFlagSfdxStyle,
loglevel,
Expand All @@ -60,15 +64,7 @@ export default class Merge extends SfpowerscriptsCommand {
let argProfileList = this.flags.profilelist;
let argMetadatas = this.flags.metadata;

// argMetadatas = (val: string) => {
// let parts = val.split(':');
// return {
// MetadataType: parts[0].trim(),
// ApiName: parts.length >= 2 ? parts[1].trim() : '*',
// };
// };

Sfpowerkit.initCache();
Sfpowerkit.initCache(this.flags.resetcache);

let metadatas = undefined;
let invalidArguments = [];
Expand Down
5 changes: 5 additions & 0 deletions packages/sfpowerscripts-cli/src/commands/profile/reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export default class Reconcile extends SfpowerscriptsCommand {
description: messages.getMessage('sourceonlyFlagDescription'),
required: false,
}),
resetcache: Flags.boolean({
description: messages.getMessage('resetCacheFlagDescription'),
required: false,
}),
targetorg: requiredUserNameFlag,
'apiversion': orgApiVersionFlagSfdxStyle,
loglevel,
Expand All @@ -67,6 +71,7 @@ export default class Reconcile extends SfpowerscriptsCommand {
public async execute(): Promise<Array<{state: any, fullName: any, type: any, path: any}>> {
let argFolder = this.flags.folder;
let argProfileList = this.flags.profilelist;
Sfpowerkit.initCache(this.flags.resetcache);

if (!this.flags.sourceonly) {
if (_.isNil(this.flags.targetorg)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class Retrieve extends SfpowerscriptsCommand {
folders.push(...argFolder);
}

Sfpowerkit.initCache();
Sfpowerkit.initCache(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azlam-abdulsalam
Why did you only optionally reseted the cache for the other two commands and forced it for the retrieve? Why not uniformly? The error can occur with every command. And is there anything against not flushing the cache when changing the org? Or to build the cache key with alias?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reconcile is used in sfpowerscripts using a dual pass approach, so if we reset the second one will be expensive. Hence made optional, it makes sense though while you retrieve to ensure you ignore any existing cache.

Ideally the fix would be to be have a proper data structure, that caches for every org, as you proposed, but I do think this is a cheaper option for now to get this bug resolved, and then proceed to fix it later

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me thats fine fore a temp fix. How do you decide if a cache is valid or not anymore?


SFPLogger.log(COLOR_WARNING(messages.getMessage('retriveDelayWarning')),LoggerLevel.INFO);
SFPLogger.log(COLOR_KEY_MESSAGE(`Retrieving profiles from ${this.flags.targetorg}`),LoggerLevel.INFO );
Expand Down
4 changes: 3 additions & 1 deletion packages/sfprofiles/src/utils/sfpowerkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export class Sfpowerkit {
fs.unlinkSync(cachePath);
}

public static initCache() {
public static initCache(resetCache?:boolean) {
try {
if(resetCache)
azlam-abdulsalam marked this conversation as resolved.
Show resolved Hide resolved
Sfpowerkit.resetCache();
//Set the cache path on init,
//TODO: Move this to a temporary directory with randomization
Sfpowerkit.cache = new SQLITEKeyValue(FileUtils.getLocalCachePath('sfpowerkit-cache.db'));
Expand Down