-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor transaction ids to be UUIDs instead of random number
- Loading branch information
Showing
9 changed files
with
159 additions
and
20 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
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,83 @@ | ||
import { migrate, version } from './098'; | ||
|
||
const oldVersion = 97; | ||
describe('migration #98', () => { | ||
it('updates the version metadata', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: {}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
|
||
expect(newStorage.meta).toStrictEqual({ version }); | ||
}); | ||
|
||
it('handles missing TransactionController', async () => { | ||
const oldState = { | ||
OtherController: {}, | ||
}; | ||
|
||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: oldState, | ||
}); | ||
|
||
expect(transformedState.data).toEqual(oldState); | ||
}); | ||
|
||
it('handles empty transactions', async () => { | ||
const oldState = { | ||
TransactionController: { | ||
transactions: {}, | ||
}, | ||
}; | ||
|
||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: oldState, | ||
}); | ||
|
||
expect(transformedState.data).toEqual(oldState); | ||
}); | ||
|
||
it('handles missing state', async () => { | ||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: {}, | ||
}); | ||
|
||
expect(transformedState.data).toEqual({}); | ||
}); | ||
|
||
it('updates transaction ids', async () => { | ||
const oldState = { | ||
TransactionController: { | ||
transactions: { | ||
1: { | ||
id: 1, | ||
otherProp: 1, | ||
}, | ||
2: { | ||
id: 2, | ||
otherProp: 2, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: oldState, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
|
||
const migratedTransactions = | ||
newStorage.data.TransactionController.transactions; | ||
|
||
Object.keys(migratedTransactions).forEach((newTxId) => { | ||
expect(typeof newTxId).toBe('string'); | ||
expect(newTxId).toBe(migratedTransactions[newTxId].id); | ||
}); | ||
}); | ||
}); |
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,56 @@ | ||
import { cloneDeep, isEmpty } from 'lodash'; | ||
import { v4 as uuid } from 'uuid'; | ||
|
||
type VersionedData = { | ||
meta: { version: number }; | ||
data: Record<string, unknown>; | ||
}; | ||
|
||
export const version = 98; | ||
|
||
/** | ||
* The core TransactionController uses strings for transaction IDs, specifically UUIDs generated by the uuid package. | ||
* For the sake of standardisation and minimising code maintenance, the use of UUIDs is preferred. | ||
* This migration updates the transaction IDs to UUIDs. | ||
* | ||
* @param originalVersionedData | ||
*/ | ||
export async function migrate( | ||
originalVersionedData: VersionedData, | ||
): Promise<VersionedData> { | ||
const versionedData = cloneDeep(originalVersionedData); | ||
versionedData.meta.version = version; | ||
transformState(versionedData.data); | ||
return versionedData; | ||
} | ||
|
||
function transformState(state: Record<string, any>) { | ||
const transactionControllerState = state?.TransactionController || {}; | ||
const transactions = transactionControllerState?.transactions || {}; | ||
|
||
if (isEmpty(transactions)) { | ||
return; | ||
} | ||
|
||
const newTxs = Object.keys(transactions).reduce( | ||
(txs: { [key: string]: any }, oldTransactionId) => { | ||
// Clone the transaction | ||
const transaction = cloneDeep(transactions[oldTransactionId]); | ||
|
||
// Assign a new id to the transaction | ||
const newTransactionID = uuid(); | ||
transaction.id = newTransactionID; | ||
|
||
return { | ||
...txs, | ||
[newTransactionID]: transaction, | ||
}; | ||
}, | ||
{}, | ||
); | ||
|
||
state.TransactionController = { | ||
...transactionControllerState, | ||
transactions: newTxs, | ||
}; | ||
} |
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
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