This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate sfpowerscripts artifact to custom setting (#545)
* Implement SfpowerscriptsArtifact as a custom setting * Add log statement * Set isMigrated to true * Backwards-compatibility for custom object SfpowerscriptsArtifact__c * Throw error if query for SfpowerscriptsArtifact fails unexpectedly Prevent the updater or checker from updating sfpowerscripts artifacts if either of the queries for SfpowerscriptsArtifact__c or SfpowerscriptsArtifact2__c fail due to reason other than not existing. * Run SfpowerscriptsArtifact query only if failed previously * Move migration logic to method * Fix error handling Fix missing await on ArtifactMigrator.exec Fix duplicate migrations * Update sfpowerscripts-artifact package version ID * Revert package name * Fix ArtifactsToOrg unit tests Mock resolved value for ArtifactorMigrator.exec()
- Loading branch information
Showing
13 changed files
with
210 additions
and
203 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
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,181 @@ | ||
import child_process = require("child_process"); | ||
import * as fs from "fs-extra"; | ||
const retry = require("async-retry"); | ||
|
||
export default class ArtifactMigrator { | ||
|
||
/** | ||
* API name of latest SfpowerscriptsArtifact object installed in org | ||
*/ | ||
public static objectApiName: string = null; | ||
|
||
private static isMigrated: boolean = false; | ||
|
||
private static isSfpowerscriptsArtifact2Exist: boolean; | ||
private static isSfpowerscriptsArtifactExist: boolean; | ||
|
||
private static sfpowerscriptsArtifact2Records; | ||
private static sfpowerscriptsArtifactRecords; | ||
|
||
public static async exec(username: string): Promise<void> { | ||
if ( | ||
ArtifactMigrator.isSfpowerscriptsArtifact2Exist === undefined && | ||
ArtifactMigrator.isSfpowerscriptsArtifactExist === undefined | ||
) { | ||
ArtifactMigrator.querySfpowerscriptsArtifact2(username); | ||
ArtifactMigrator.querySfpowerscriptsArtifact(username); | ||
|
||
if (ArtifactMigrator.isSfpowerscriptsArtifact2Exist) { | ||
ArtifactMigrator.objectApiName = "SfpowerscriptsArtifact2__c"; | ||
} else { | ||
console.log("The custom object SfpowerscriptsArtifact__c will be deprecated in future release. Move to the new version of SfpowerscriptsArtifact to maintain compatibility."); | ||
ArtifactMigrator.objectApiName = "SfpowerscriptsArtifact__c"; | ||
} | ||
} | ||
|
||
if ( | ||
ArtifactMigrator.isSfpowerscriptsArtifact2Exist && | ||
ArtifactMigrator.isSfpowerscriptsArtifactExist | ||
) { | ||
if ( | ||
ArtifactMigrator.sfpowerscriptsArtifact2Records.length === 0 && | ||
ArtifactMigrator.sfpowerscriptsArtifactRecords.length > 0 && | ||
!ArtifactMigrator.isMigrated | ||
) { | ||
await ArtifactMigrator.migrate(username); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Migrate records from SfpowerscriptsArtifact__c to SfpowerscriptsArtifact2__c | ||
*/ | ||
private static async migrate(username) { | ||
console.log("Migrating records to SfpowerscriptsArtifact2__c..."); | ||
|
||
let recordsToImport = {records: []}; | ||
ArtifactMigrator.sfpowerscriptsArtifactRecords.forEach((record, idx) => { | ||
recordsToImport.records.push({ | ||
attributes: { | ||
type: "SfpowerscriptsArtifact2__c", | ||
referenceId: `SfpowerscriptsArtifact2_${idx}` | ||
}, | ||
Name: record.Name, | ||
Tag__c: record.Tag__c, | ||
Version__c: record.Version__c, | ||
CommitId__c: record.CommitId__c | ||
}); | ||
}); | ||
|
||
fs.writeFileSync( | ||
"SfpowerscriptsArtifact2SObjectTreeFile.json", | ||
JSON.stringify(recordsToImport) | ||
); | ||
|
||
try { | ||
await retry ( | ||
async (bail) => { | ||
let importResultJson = child_process.execSync( | ||
`sfdx force:data:tree:import -f SfpowerscriptsArtifact2SObjectTreeFile.json -u ${username} --json`, | ||
{ | ||
encoding: "utf8", | ||
stdio:"pipe" | ||
} | ||
); | ||
|
||
let importResult = JSON.parse(importResultJson); | ||
if (importResult.status === 1) { | ||
throw new Error("Failed to migrate records from SfpowerscriptsArtifact__c to SfpowerscriptsArtifact2__c"); | ||
} else { | ||
ArtifactMigrator.isMigrated = true; | ||
return; | ||
} | ||
}, | ||
{ retries: 3, minTimeout: 2000 } | ||
); | ||
} catch (error) { | ||
console.log(error.message); | ||
throw error; | ||
} finally { | ||
fs.unlinkSync("SfpowerscriptsArtifact2SObjectTreeFile.json"); | ||
} | ||
} | ||
|
||
/** | ||
* Sets properties for records and existence of SfpowerscriptsArtifact2 | ||
* @param username | ||
*/ | ||
private static querySfpowerscriptsArtifact2(username): void { | ||
try { | ||
let queryResultJson = child_process.execSync( | ||
`sfdx force:data:soql:query -q "SELECT Id, Name, CommitId__c, Version__c, Tag__c FROM SfpowerscriptsArtifact2__c" -r json -u ${username}`, | ||
{ | ||
encoding: "utf8", | ||
stdio:"pipe" | ||
} | ||
); | ||
|
||
let queryResult = JSON.parse(queryResultJson); | ||
if ( | ||
queryResult.status === 1 && | ||
queryResult.message.includes("sObject type 'SfpowerscriptsArtifact2__c' is not supported") | ||
) { | ||
ArtifactMigrator.isSfpowerscriptsArtifact2Exist = false; | ||
} else if (queryResult.status === 1) { | ||
console.log(queryResult.message); | ||
throw new Error(queryResult.message); | ||
} else { | ||
ArtifactMigrator.sfpowerscriptsArtifact2Records = queryResult.result.records; | ||
ArtifactMigrator.isSfpowerscriptsArtifact2Exist = true; | ||
} | ||
|
||
} catch (error) { | ||
let errorMsg = JSON.parse(error.stdout).message; | ||
if (errorMsg.includes("sObject type 'SfpowerscriptsArtifact2__c' is not supported")) { | ||
ArtifactMigrator.isSfpowerscriptsArtifact2Exist = false; | ||
} else { | ||
console.log(errorMsg); | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Set properties for records and existence of SfpowerscriptsArtifact | ||
* @param username | ||
*/ | ||
private static querySfpowerscriptsArtifact(username): void { | ||
try { | ||
let queryResultJson = child_process.execSync( | ||
`sfdx force:data:soql:query -q "SELECT Id, Name, CommitId__c, Version__c, Tag__c FROM SfpowerscriptsArtifact__c" -r json -u ${username}`, | ||
{ | ||
encoding: "utf8", | ||
stdio:"pipe" | ||
} | ||
); | ||
|
||
let queryResult = JSON.parse(queryResultJson); | ||
if ( | ||
queryResult.status === 1 && | ||
queryResult.message.includes("sObject type 'SfpowerscriptsArtifact__c' is not supported") | ||
) { | ||
ArtifactMigrator.isSfpowerscriptsArtifactExist = false; | ||
} else if (queryResult.status === 1) { | ||
console.log(queryResult.message); | ||
throw new Error(queryResult.message); | ||
} else { | ||
ArtifactMigrator.sfpowerscriptsArtifactRecords = queryResult.result.records; | ||
ArtifactMigrator.isSfpowerscriptsArtifactExist = true; | ||
} | ||
} catch (error) { | ||
let errorMsg = JSON.parse(error.stdout).message; | ||
if (errorMsg.includes("sObject type 'SfpowerscriptsArtifact__c' is not supported")) { | ||
ArtifactMigrator.isSfpowerscriptsArtifactExist = false; | ||
} else { | ||
console.log(errorMsg); | ||
throw error; | ||
}; | ||
} | ||
} | ||
|
||
} |
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
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
8 changes: 8 additions & 0 deletions
8
...ain/default/objects/SfpowerscriptsArtifact2__c/SfpowerscriptsArtifact2__c.object-meta.xml
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,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<customSettingsType>List</customSettingsType> | ||
<description>Sfpowerscripts artifacts installed in the org</description> | ||
<enableFeeds>false</enableFeeds> | ||
<label>Sfpowerscripts Artifact 2</label> | ||
<visibility>Public</visibility> | ||
</CustomObject> |
1 change: 1 addition & 0 deletions
1
...fact__c/fields/CommitId__c.field-meta.xml → ...act2__c/fields/CommitId__c.field-meta.xml
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
1 change: 1 addition & 0 deletions
1
...sArtifact__c/fields/Tag__c.field-meta.xml → ...Artifact2__c/fields/Tag__c.field-meta.xml
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
3 changes: 2 additions & 1 deletion
3
...ifact__c/fields/Version__c.field-meta.xml → ...fact2__c/fields/Version__c.field-meta.xml
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.