From cba94083c3e6edbf3effb2eb4f72c6c183ea749b Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 1 Aug 2025 17:07:27 -0500 Subject: [PATCH] Website: Add `primaryBuyingSituation` migration script. (#31494) Related to https://github.com/fleetdm/confidential/issues/11600 Changes: - Added a script to update user records to only use the new `primaryBuyingSituation` values added in https://github.com/fleetdm/fleet/pull/31301 --- ...ate-old-primary-buying-situation-values.js | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 website/scripts/migrate-old-primary-buying-situation-values.js diff --git a/website/scripts/migrate-old-primary-buying-situation-values.js b/website/scripts/migrate-old-primary-buying-situation-values.js new file mode 100644 index 0000000000..e922a0b074 --- /dev/null +++ b/website/scripts/migrate-old-primary-buying-situation-values.js @@ -0,0 +1,90 @@ +module.exports = { + + + friendlyName: 'Migrate old primary buying situation values', + + + description: 'Updates the primaryBuyingSituation value of user records that have older values set.', + + inputs: { + dry: { type: 'boolean', description: 'Whether to make this a dry run. (No database changes will be made.)' }, + }, + + fn: async function ({dry}) { + sails.log('Running custom shell script... (`sails run migrate-old-primary-buying-situation-values`)'); + + let numberOfUsersWhoWouldHaveBeenUpdated = 0; + sails.log('Migrating users with primaryBuyingSituation === mdm.'); + await User.stream({primaryBuyingSituation: 'mdm'}) + .eachRecord(async (thisUser)=>{ + // Default to 'it-major-mdm' for users with an mdm primaryBuyingSituation. + let newPrimaryBuyingSituationForThisUser = 'it-major-mdm'; + // If this user has filled out the get started questionnaire, check their answers to determine if they were interested in Linux MDM. + if(thisUser.getStartedQuestionnaireAnswers !== {}){ + if(thisUser.getStartedQuestionnaireAnswers['what-do-you-manage-mdm']) { + // If they previosuly selected linux as a response to the "What do you manage?" question, set their new primaryBuyingSituation to be 'it-gap-filler-mdm' + if(thisUser.getStartedQuestionnaireAnswers['what-do-you-manage-mdm'].mdmUseCase === 'linux') { + newPrimaryBuyingSituationForThisUser = 'it-gap-filler-mdm'; + } + } + } + if(dry) { + numberOfUsersWhoWouldHaveBeenUpdated++; + } else { + // Update this user's primary buying situation. + await User.updateOne({id: thisUser.id}).set({ + primaryBuyingSituation: newPrimaryBuyingSituationForThisUser, + }); + } + }); + + sails.log('Migrating users with primaryBuyingSituation === eo-it.'); + await User.stream({primaryBuyingSituation: 'eo-it'}) + .eachRecord(async (thisUser)=>{ + let newPrimaryBuyingSituationForThisUser = 'it-misc'; + if(dry) { + numberOfUsersWhoWouldHaveBeenUpdated++; + } else { + // Update this user's primary buying situation. + await User.updateOne({id: thisUser.id}).set({ + primaryBuyingSituation: newPrimaryBuyingSituationForThisUser, + }); + } + }); + + sails.log('Migrating users with primaryBuyingSituation === eo-security.'); + await User.stream({primaryBuyingSituation: 'eo-security'}) + .eachRecord(async (thisUser)=>{ + let newPrimaryBuyingSituationForThisUser = 'security-misc'; + if(dry) { + numberOfUsersWhoWouldHaveBeenUpdated++; + } else { + // Update this user's primary buying situation. + await User.updateOne({id: thisUser.id}).set({ + primaryBuyingSituation: newPrimaryBuyingSituationForThisUser, + }); + } + }); + + sails.log('Migrating users with primaryBuyingSituation === vm.'); + await User.stream({primaryBuyingSituation: 'vm'}) + .eachRecord(async (thisUser)=>{ + let newPrimaryBuyingSituationForThisUser = 'security-vm'; + if(dry) { + numberOfUsersWhoWouldHaveBeenUpdated++; + } else { + // Update this user's primary buying situation. + await User.updateOne({id: thisUser.id}).set({ + primaryBuyingSituation: newPrimaryBuyingSituationForThisUser, + }); + } + }); + + if(dry){ + sails.log(`Dry run: would have migrated the primaryBuyingSituation values for ${numberOfUsersWhoWouldHaveBeenUpdated} user records.`); + } + } + + +}; +