From 745648f42edc6b71f597c93b231740a6209d75ef Mon Sep 17 00:00:00 2001 From: Mateusz Rajski Date: Tue, 13 Feb 2024 15:56:38 +0100 Subject: [PATCH 1/3] Remove old migration --- .../migrations/PersonalDetailsByAccountID.js | 274 ----------- tests/unit/MigrationTest.js | 456 ------------------ 2 files changed, 730 deletions(-) delete mode 100644 src/libs/migrations/PersonalDetailsByAccountID.js diff --git a/src/libs/migrations/PersonalDetailsByAccountID.js b/src/libs/migrations/PersonalDetailsByAccountID.js deleted file mode 100644 index 24aece8f5a97..000000000000 --- a/src/libs/migrations/PersonalDetailsByAccountID.js +++ /dev/null @@ -1,274 +0,0 @@ -import lodashHas from 'lodash/has'; -import Onyx from 'react-native-onyx'; -import _ from 'underscore'; -import Log from '@libs/Log'; -import ONYXKEYS from '@src/ONYXKEYS'; - -const DEPRECATED_ONYX_KEYS = { - // Deprecated personal details object which was keyed by login instead of accountID. - PERSONAL_DETAILS: 'personalDetails', -}; - -/** - * @returns {Promise} - */ -function getReportActionsFromOnyx() { - return new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - return resolve(allReportActions); - }, - }); - }); -} - -/** - * @returns {Promise} - */ -function getReportsFromOnyx() { - return new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connectionID); - return resolve(allReports); - }, - }); - }); -} - -/** - * We use the old personalDetails object becuase it is more efficient for this migration since it is keyed by email address. - * Also, if the old personalDetails object is not available, that likely means the migration has already run successfully before on this account. - * - * @returns {Promise} - */ -function getDeprecatedPersonalDetailsFromOnyx() { - return new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: DEPRECATED_ONYX_KEYS.PERSONAL_DETAILS, - callback: (allPersonalDetails) => { - Onyx.disconnect(connectionID); - return resolve(allPersonalDetails); - }, - }); - }); -} - -/** - * @returns {Promise} - */ -function getDeprecatedPolicyMemberListFromOnyx() { - return new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.DEPRECATED_POLICY_MEMBER_LIST, - waitForCollectionCallback: true, - callback: (allPolicyMembers) => { - Onyx.disconnect(connectionID); - return resolve(allPolicyMembers); - }, - }); - }); -} - -/** - * Migrate Onyx data for the email to accountID migration. - * - * - personalDetails -> personalDetailsList - * - policyMemberList_ -> policyMembers_ - * - reportAction_ - * - originalMessage.oldLogin -> originalMessage.oldAccountID - * - originalMessage.newLogin -> originalMessage.newAccountID - * - accountEmail -> accountID - * - actorEmail -> actorAccountID - * - childManagerEmail -> childManagerAccountID - * - whisperedTo -> whisperedToAccountIDs - * - childOldestFourEmails -> childOldestFourAccountIDs - * - originalMessage.participants -> originalMessage.participantAccountIDs - * - report_ - * - lastActorEmail -> lastActorAccountID - * - participants -> participantAccountIDs - * - * @returns {Promise} - */ -export default function () { - return Promise.all([getReportActionsFromOnyx(), getReportsFromOnyx(), getDeprecatedPersonalDetailsFromOnyx(), getDeprecatedPolicyMemberListFromOnyx()]).then( - ([oldReportActions, oldReports, oldPersonalDetails, oldPolicyMemberList]) => { - const onyxData = {}; - - // The personalDetails object has been replaced by personalDetailsList - // So if we find an instance of personalDetails we will clear it out - if (oldPersonalDetails) { - Log.info('[Migrate Onyx] PersonalDetailsByAccountID migration: removing personalDetails'); - onyxData[DEPRECATED_ONYX_KEYS.PERSONAL_DETAILS] = null; - } - - // The policyMemberList_ collection has been replaced by policyMembers_ - // So if we find any instances of policyMemberList_ we will clear them out - _.each(oldPolicyMemberList, (_policyMembersForPolicy, policyKey) => { - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing policyMemberList ${policyKey}`); - onyxData[policyKey] = null; - }); - - // We migrate reportActions to remove old email-based data. - // If we do not find the equivalent accountID-based data in the reportAction, we will just clear the reportAction - // and let it be fetched from the API next time they open the report and scroll to that action. - // We do this because we know the reportAction from the API will include the needed accountID data. - _.each(oldReportActions, (reportActionsForReport, onyxKey) => { - if (_.isEmpty(reportActionsForReport)) { - Log.info(`[Migrate Onyx] Skipped migration PersonalDetailsByAccountID for ${onyxKey} because there were no reportActions`); - return; - } - const newReportActionsForReport = {}; - let reportActionsWereModified = false; - _.each(reportActionsForReport, (reportAction, reportActionID) => { - if (_.isEmpty(reportAction)) { - reportActionsWereModified = true; - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction ${reportActionID} because the reportAction was empty`); - return; - } - const newReportAction = reportAction; - - if (lodashHas(reportAction, ['originalMessage', 'oldLogin'])) { - reportActionsWereModified = true; - - if (lodashHas(reportAction, ['originalMessage', 'oldAccountID'])) { - delete newReportAction.originalMessage.oldLogin; - } else { - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction ${reportActionID} because originalMessage.oldAccountID not found`); - return; - } - } - - if (lodashHas(reportAction, ['originalMessage', 'newLogin'])) { - reportActionsWereModified = true; - - if (lodashHas(reportAction, ['originalMessage', 'newAccountID'])) { - delete newReportAction.originalMessage.newLogin; - } else { - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction ${reportActionID} because originalMessage.newAccountID not found`); - return; - } - } - - if (lodashHas(reportAction, ['accountEmail'])) { - reportActionsWereModified = true; - - if (lodashHas(reportAction, ['accountID'])) { - delete newReportAction.accountEmail; - } else { - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction ${reportActionID} because accountID not found`); - return; - } - } - - if (lodashHas(reportAction, ['actorEmail'])) { - reportActionsWereModified = true; - - if (lodashHas(reportAction, ['actorAccountID'])) { - delete newReportAction.actorEmail; - } else { - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction ${reportActionID} because actorAccountID not found`); - return; - } - } - - if (lodashHas(reportAction, ['childManagerEmail'])) { - reportActionsWereModified = true; - - if (lodashHas(reportAction, ['childManagerAccountID'])) { - delete newReportAction.childManagerEmail; - } else { - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction ${reportActionID} because childManagerAccountID not found`); - return; - } - } - - if (lodashHas(reportAction, ['whisperedTo'])) { - reportActionsWereModified = true; - - if (lodashHas(reportAction, ['whisperedToAccountIDs'])) { - delete newReportAction.whisperedTo; - } else { - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction ${reportActionID} because whisperedToAccountIDs not found`); - return; - } - } - - if (lodashHas(reportAction, ['childOldestFourEmails'])) { - reportActionsWereModified = true; - - if (lodashHas(reportAction, ['childOldestFourAccountIDs'])) { - delete newReportAction.childOldestFourEmails; - } else { - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction ${reportActionID} because childOldestFourAccountIDs not found`); - return; - } - } - - if (lodashHas(reportAction, ['originalMessage', 'participants'])) { - reportActionsWereModified = true; - - if (lodashHas(reportAction, ['originalMessage', 'participantAccountIDs'])) { - delete newReportAction.originalMessage.participants; - } else { - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction ${reportActionID} because originalMessage.participantAccountIDs not found`); - return; - } - } - - newReportActionsForReport[reportActionID] = newReportAction; - }); - - // Only include the reportActions from this report if at least one reportAction in this report - // was modified in any way. - if (reportActionsWereModified) { - onyxData[onyxKey] = newReportActionsForReport; - } - }); - - // For the reports migration, we don't need to look up emails from accountIDs. Instead, - // we will just look for old email data and automatically remove it if it exists. The reason for - // this is that we already stopped sending email based data in reports, and from everywhere else - // in the app by this point (since this is the last data we migrated). - _.each(oldReports, (report, onyxKey) => { - const newReport = report; - - // Delete report key if it's empty - if (_.isEmpty(newReport)) { - onyxData[onyxKey] = null; - return; - } - - let reportWasModified = false; - if (lodashHas(newReport, ['lastActorEmail'])) { - reportWasModified = true; - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing lastActorEmail from report ${newReport.reportID}`); - delete newReport.lastActorEmail; - } - - if (lodashHas(newReport, ['ownerEmail'])) { - reportWasModified = true; - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing ownerEmail from report ${newReport.reportID}`); - delete newReport.ownerEmail; - } - - if (lodashHas(newReport, ['managerEmail'])) { - reportWasModified = true; - Log.info(`[Migrate Onyx] PersonalDetailsByAccountID migration: removing managerEmail from report ${newReport.reportID}`); - delete newReport.managerEmail; - } - - if (reportWasModified) { - onyxData[onyxKey] = newReport; - } - }); - - return Onyx.multiSet(onyxData); - }, - ); -} diff --git a/tests/unit/MigrationTest.js b/tests/unit/MigrationTest.js index f5012162c59e..65ab921ac9e1 100644 --- a/tests/unit/MigrationTest.js +++ b/tests/unit/MigrationTest.js @@ -1,9 +1,7 @@ import Onyx from 'react-native-onyx'; -import _ from 'underscore'; import Log from '../../src/libs/Log'; import CheckForPreviousReportActionID from '../../src/libs/migrations/CheckForPreviousReportActionID'; import KeyReportActionsDraftByReportActionID from '../../src/libs/migrations/KeyReportActionsDraftByReportActionID'; -import PersonalDetailsByAccountID from '../../src/libs/migrations/PersonalDetailsByAccountID'; import ONYXKEYS from '../../src/ONYXKEYS'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; @@ -25,460 +23,6 @@ describe('Migrations', () => { return waitForBatchedUpdates(); }); - describe('PersonalDetailsByAccountID', () => { - const DEPRECATED_ONYX_KEYS = { - // Deprecated personal details object which was keyed by login instead of accountID. - PERSONAL_DETAILS: 'personalDetails', - }; - - it('Should skip any zombie reportAction collections that have no reportAction data in Onyx', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: null, - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}2`]: null, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - _.each(allReportActions, (reportActionsForReport) => expect(reportActionsForReport).toBeUndefined()); - }, - }); - })); - - it('Should remove any individual reportActions that have no data in Onyx', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: { - 1: {}, - 2: {}, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction 1 because the reportAction was empty'); - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction 2 because the reportAction was empty'); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - _.each(allReportActions, (reportActionsForReport) => expect(reportActionsForReport).toMatchObject({})); - }, - }); - })); - - it('Should remove any individual reportActions that have originalMessage.oldLogin but not originalMessage.oldAccountID', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: { - 1: { - originalMessage: { - oldLogin: 'test1@account.com', - oldAccountID: 100, - }, - }, - 2: { - originalMessage: { - oldLogin: 'test1@account.com', - }, - }, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction 2 because originalMessage.oldAccountID not found'); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toHaveProperty('1'); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).not.toHaveProperty('2'); - }, - }); - })); - - it('Should remove any individual reportActions that have originalMessage.newLogin but not originalMessage.newAccountID', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: { - 1: { - originalMessage: { - newLogin: 'test2@account.com', - newAccountID: 101, - }, - }, - 2: { - originalMessage: { - newLogin: 'test2@account.com', - }, - }, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction 2 because originalMessage.newAccountID not found'); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toHaveProperty('1'); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).not.toHaveProperty('2'); - }, - }); - })); - - it('Should remove any individual reportActions that have accountEmail but not accountID', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: { - 1: { - accountEmail: 'test2@account.com', - accountID: 101, - }, - 2: { - accountEmail: 'test2@account.com', - }, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction 2 because accountID not found'); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toHaveProperty('1'); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).not.toHaveProperty('2'); - }, - }); - })); - - it('Should remove any individual reportActions that have actorEmail but not actorAccountID', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: { - 1: { - actorEmail: 'test2@account.com', - actorAccountID: 101, - }, - 2: { - actorEmail: 'test2@account.com', - }, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction 2 because actorAccountID not found'); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toHaveProperty('1'); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).not.toHaveProperty('2'); - }, - }); - })); - - it('Should remove any individual reportActions that have childManagerEmail but not childManagerAccountID', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: { - 1: { - childManagerEmail: 'test2@account.com', - childManagerAccountID: 101, - }, - 2: { - childManagerEmail: 'test2@account.com', - }, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction 2 because childManagerAccountID not found'); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toHaveProperty('1'); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).not.toHaveProperty('2'); - }, - }); - })); - - it('Should remove any individual reportActions that have whisperedTo but not whisperedToAccountIDs', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: { - 1: { - whisperedTo: ['test1@account.com', 'test2@account.com'], - whisperedToAccountIDs: [100, 101], - }, - 2: { - whisperedTo: ['test1@account.com', 'test2@account.com'], - }, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction 2 because whisperedToAccountIDs not found'); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toHaveProperty('1'); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).not.toHaveProperty('2'); - }, - }); - })); - - it('Should remove any individual reportActions that have childOldestFourEmails but not childOldestFourAccountIDs', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: { - 1: { - childOldestFourEmails: 'test1@account.com, test2@account.com', - childOldestFourAccountIDs: '100,101', - }, - 2: { - childOldestFourEmails: 'test1@account.com, test2@account.com', - }, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction 2 because childOldestFourAccountIDs not found'); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toHaveProperty('1'); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).not.toHaveProperty('2'); - }, - }); - })); - - it('Should remove any individual reportActions that have originalMessage.participants but not originalMessage.participantAccountIDs', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: { - 1: { - originalMessage: { - participants: ['test1@account.com', 'test2@account.com'], - participantAccountIDs: [100, 101], - }, - }, - 2: { - originalMessage: { - participants: ['test1@account.com', 'test2@account.com'], - }, - }, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith( - '[Migrate Onyx] PersonalDetailsByAccountID migration: removing reportAction 2 because originalMessage.participantAccountIDs not found', - ); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).toHaveProperty('1'); - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]).not.toHaveProperty('2'); - }, - }); - })); - - it('Should succeed in removing all email data when equivalent accountID data exists', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`]: { - 1: { - originalMessage: { - oldLogin: 'test1@account.com', - oldAccountID: 100, - newLogin: 'test2@account.com', - newAccountID: 101, - participants: ['test1@account.com', 'test2@account.com'], - participantAccountIDs: [100, 101], - }, - actorEmail: 'test2@account.com', - actorAccountID: 101, - accountEmail: 'test2@account.com', - accountID: 101, - childManagerEmail: 'test2@account.com', - childManagerAccountID: 101, - whisperedTo: ['test1@account.com', 'test2@account.com'], - whisperedToAccountIDs: [100, 101], - childOldestFourEmails: 'test1@account.com, test2@account.com', - childOldestFourAccountIDs: '100,101', - }, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - const expectedReportAction = { - originalMessage: { - oldAccountID: 100, - newAccountID: 101, - participantAccountIDs: [100, 101], - }, - actorAccountID: 101, - accountID: 101, - childManagerAccountID: 101, - whisperedToAccountIDs: [100, 101], - childOldestFourAccountIDs: '100,101', - }; - expect(allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}1`][1]).toMatchObject(expectedReportAction); - }, - }); - })); - - it('Should succeed in removing any policyMemberList objects it finds in Onyx', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.DEPRECATED_POLICY_MEMBER_LIST}1`]: { - 'admin@company1.com': { - role: 'admin', - }, - 'employee@company1.com': { - role: 'user', - }, - }, - [`${ONYXKEYS.COLLECTION.DEPRECATED_POLICY_MEMBER_LIST}2`]: { - 'admin@company2.com': { - role: 'admin', - }, - 'employee@company2.com': { - role: 'user', - }, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith( - `[Migrate Onyx] PersonalDetailsByAccountID migration: removing policyMemberList ${ONYXKEYS.COLLECTION.DEPRECATED_POLICY_MEMBER_LIST}1`, - ); - expect(LogSpy).toHaveBeenCalledWith( - `[Migrate Onyx] PersonalDetailsByAccountID migration: removing policyMemberList ${ONYXKEYS.COLLECTION.DEPRECATED_POLICY_MEMBER_LIST}2`, - ); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.DEPRECATED_POLICY_MEMBER_LIST, - waitForCollectionCallback: true, - callback: (allPolicyMemberLists) => { - Onyx.disconnect(connectionID); - - expect(allPolicyMemberLists).toBeFalsy(); - }, - }); - })); - - it('Should succeed in removing the personalDetails object if found in Onyx', () => - Onyx.multiSet({ - [`${DEPRECATED_ONYX_KEYS.PERSONAL_DETAILS}`]: { - 'test1@account.com': { - accountID: 100, - login: 'test1@account.com', - }, - 'test2@account.com': { - accountID: 101, - login: 'test2@account.com', - }, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing personalDetails'); - const connectionID = Onyx.connect({ - key: DEPRECATED_ONYX_KEYS.PERSONAL_DETAILS, - callback: (allPersonalDetails) => { - Onyx.disconnect(connectionID); - expect(allPersonalDetails).toBeNull(); - }, - }); - })); - - it('Should remove any instances of lastActorEmail found in a report', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT}1`]: { - reportID: 1, - lastActorEmail: 'fake@test.com', - lastActorAccountID: 5, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing lastActorEmail from report 1'); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connectionID); - const expectedReport = { - reportID: 1, - lastActorAccountID: 5, - }; - expect(allReports[`${ONYXKEYS.COLLECTION.REPORT}1`]).toMatchObject(expectedReport); - }, - }); - })); - - it('Should remove any instances of ownerEmail found in a report', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT}1`]: { - reportID: 1, - ownerEmail: 'fake@test.com', - ownerAccountID: 5, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing ownerEmail from report 1'); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connectionID); - const expectedReport = { - reportID: 1, - ownerAccountID: 5, - }; - expect(allReports[`${ONYXKEYS.COLLECTION.REPORT}1`]).toMatchObject(expectedReport); - }, - }); - })); - - it('Should remove any instances of managerEmail found in a report', () => - Onyx.multiSet({ - [`${ONYXKEYS.COLLECTION.REPORT}1`]: { - reportID: 1, - managerEmail: 'fake@test.com', - managerID: 5, - }, - }) - .then(PersonalDetailsByAccountID) - .then(() => { - expect(LogSpy).toHaveBeenCalledWith('[Migrate Onyx] PersonalDetailsByAccountID migration: removing managerEmail from report 1'); - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connectionID); - const expectedReport = { - reportID: 1, - managerID: 5, - }; - expect(allReports[`${ONYXKEYS.COLLECTION.REPORT}1`]).toMatchObject(expectedReport); - }, - }); - })); - }); - describe('CheckForPreviousReportActionID', () => { it("Should work even if there's no reportAction data in Onyx", () => CheckForPreviousReportActionID().then(() => From 0590d43a30b5f6b0452128f9f73181b74c398993 Mon Sep 17 00:00:00 2001 From: Mateusz Rajski Date: Tue, 13 Feb 2024 15:58:26 +0100 Subject: [PATCH 2/3] Migrate migrateOnyx module --- src/libs/{migrateOnyx.js => migrateOnyx.ts} | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) rename src/libs/{migrateOnyx.js => migrateOnyx.ts} (77%) diff --git a/src/libs/migrateOnyx.js b/src/libs/migrateOnyx.ts similarity index 77% rename from src/libs/migrateOnyx.js rename to src/libs/migrateOnyx.ts index 9b8b4056e3e5..97e698790d81 100644 --- a/src/libs/migrateOnyx.js +++ b/src/libs/migrateOnyx.ts @@ -1,24 +1,21 @@ -import _ from 'underscore'; import Log from './Log'; import KeyReportActionsDraftByReportActionID from './migrations/KeyReportActionsDraftByReportActionID'; -import PersonalDetailsByAccountID from './migrations/PersonalDetailsByAccountID'; import RemoveEmptyReportActionsDrafts from './migrations/RemoveEmptyReportActionsDrafts'; import RenameReceiptFilename from './migrations/RenameReceiptFilename'; import TransactionBackupsToCollection from './migrations/TransactionBackupsToCollection'; -export default function () { +export default function (): Promise { const startTime = Date.now(); Log.info('[Migrate Onyx] start'); return new Promise((resolve) => { // Add all migrations to an array so they are executed in order - const migrationPromises = [PersonalDetailsByAccountID, RenameReceiptFilename, KeyReportActionsDraftByReportActionID, TransactionBackupsToCollection, RemoveEmptyReportActionsDrafts]; + const migrationPromises = [RenameReceiptFilename, KeyReportActionsDraftByReportActionID, TransactionBackupsToCollection, RemoveEmptyReportActionsDrafts]; // Reduce all promises down to a single promise. All promises run in a linear fashion, waiting for the // previous promise to finish before moving onto the next one. /* eslint-disable arrow-body-style */ - _.reduce( - migrationPromises, + migrationPromises.reduce( (previousPromise, migrationPromise) => { return previousPromise.then(() => { return migrationPromise(); From a7dd51d488b7efcaed1c4f23d5d3c3596d852a59 Mon Sep 17 00:00:00 2001 From: Mateusz Rajski Date: Wed, 14 Feb 2024 12:22:32 +0100 Subject: [PATCH 3/3] Format code --- src/libs/migrateOnyx.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/libs/migrateOnyx.ts b/src/libs/migrateOnyx.ts index 97e698790d81..1202275067a5 100644 --- a/src/libs/migrateOnyx.ts +++ b/src/libs/migrateOnyx.ts @@ -15,14 +15,12 @@ export default function (): Promise { // Reduce all promises down to a single promise. All promises run in a linear fashion, waiting for the // previous promise to finish before moving onto the next one. /* eslint-disable arrow-body-style */ - migrationPromises.reduce( - (previousPromise, migrationPromise) => { + migrationPromises + .reduce((previousPromise, migrationPromise) => { return previousPromise.then(() => { return migrationPromise(); }); - }, - Promise.resolve(), - ) + }, Promise.resolve()) // Once all migrations are done, resolve the main promise .then(() => {