This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Basic approach to one off migration #2563
Open
snario
wants to merge
11
commits into
master
Choose a base branch
from
liam/migrateBasics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cc2d202
Basic approach to one off migration
snario e1dc299
Fix typo
snario 28328c1
Remove typo
snario c204cbc
Handle all PRs since migration
snario 268426a
Update script to use finer DB queries
snario 0e91e54
Remove ++ operator
snario 0f68c37
Export migrate script
snario 4a9113f
Bump node
snario 8a9aed0
Add missing version field
snario 3f5c7fc
Add missing version field
snario f4d39e4
Safer default value of store
snario File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,123 @@ | ||
import { Node } from "@counterfactual/types"; | ||
import { bigNumberify, BigNumberish } from "ethers/utils"; | ||
|
||
import { StateChannelJSON } from "../models/state-channel"; | ||
|
||
const DB_NAMESPACE_CHANNEL = "channel"; | ||
const DB_NAMESPACE_WITHDRAWALS = "multisigAddressToWithdrawalCommitment"; | ||
const DB_NAMESPACE_ALL_COMMITMENTS = "allCommitments"; | ||
|
||
const bigNumberIshToString = (x: BigNumberish) => bigNumberify(x).toHexString(); | ||
|
||
export const migrateToPatch1 = async ( | ||
storeService: Node.IStoreService, | ||
storeKeyPrefix: string | ||
) => { | ||
const stateChannelsMap: { | ||
[multisigAddress: string]: StateChannelJSON; | ||
} = await storeService.get(`${storeKeyPrefix}/${DB_NAMESPACE_CHANNEL}`); | ||
|
||
for (const multisigAddress in stateChannelsMap) { | ||
/** | ||
* Update proposal: | ||
* https://github.com/counterfactual/monorepo/pull/2542/files | ||
*/ | ||
|
||
const proposals = stateChannelsMap[multisigAddress].proposedAppInstances; | ||
|
||
for (let i = 0; i < proposals.length; i += 1) { | ||
const proposal = proposals[i][1]; | ||
stateChannelsMap[multisigAddress].proposedAppInstances[i][1] = { | ||
...proposal, | ||
initiatorDeposit: bigNumberIshToString(proposal.initiatorDeposit), | ||
responderDeposit: bigNumberIshToString(proposal.responderDeposit), | ||
timeout: bigNumberIshToString(proposal.timeout) | ||
}; | ||
} | ||
|
||
/** | ||
* Update interpreter params | ||
* https://github.com/counterfactual/monorepo/pull/2544/files#diff-95c2150ee50c44be12700a72fd6fbf73R32 | ||
*/ | ||
|
||
const apps = stateChannelsMap[multisigAddress].appInstances; | ||
|
||
for (let i = 0; i < apps.length; i += 1) { | ||
const app = apps[i][1]; | ||
|
||
if (app.twoPartyOutcomeInterpreterParams) { | ||
stateChannelsMap[multisigAddress].appInstances[ | ||
i | ||
][1].twoPartyOutcomeInterpreterParams = { | ||
...app.twoPartyOutcomeInterpreterParams!, | ||
amount: bigNumberIshToString( | ||
app.twoPartyOutcomeInterpreterParams!.amount | ||
) | ||
}; | ||
} | ||
|
||
if (app.singleAssetTwoPartyCoinTransferInterpreterParams) { | ||
stateChannelsMap[multisigAddress].appInstances[ | ||
i | ||
][1].singleAssetTwoPartyCoinTransferInterpreterParams = { | ||
...app.singleAssetTwoPartyCoinTransferInterpreterParams!, | ||
limit: bigNumberIshToString( | ||
app.singleAssetTwoPartyCoinTransferInterpreterParams!.limit | ||
) | ||
}; | ||
} | ||
|
||
if (app.multiAssetMultiPartyCoinTransferInterpreterParams) { | ||
stateChannelsMap[multisigAddress].appInstances[ | ||
i | ||
][1].multiAssetMultiPartyCoinTransferInterpreterParams = { | ||
...app.multiAssetMultiPartyCoinTransferInterpreterParams!, | ||
limit: app.multiAssetMultiPartyCoinTransferInterpreterParams.limit.map( | ||
bigNumberIshToString! | ||
) | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Delete createdAt | ||
* https://github.com/counterfactual/monorepo/pull/2541/files | ||
*/ | ||
delete stateChannelsMap[multisigAddress]["createdAt"]; | ||
|
||
/** | ||
* https://github.com/counterfactual/monorepo/pull/2538/files | ||
*/ | ||
|
||
const agreements = | ||
stateChannelsMap[multisigAddress] | ||
.singleAssetTwoPartyIntermediaryAgreements; | ||
|
||
for (let i = 0; i < agreements.length; i += 1) { | ||
const agreement = agreements[i][1]; | ||
stateChannelsMap[ | ||
multisigAddress | ||
].singleAssetTwoPartyIntermediaryAgreements[i][1] = { | ||
...agreements[i][1], | ||
capitalProvided: bigNumberIshToString(agreement.capitalProvided) | ||
}; | ||
} | ||
} | ||
|
||
const withdrawals = await storeService.get( | ||
`${storeKeyPrefix}/${DB_NAMESPACE_WITHDRAWALS}` | ||
); | ||
|
||
const commitments = await storeService.get( | ||
`${storeKeyPrefix}/${DB_NAMESPACE_ALL_COMMITMENTS}` | ||
); | ||
|
||
const sharedData = { | ||
commitments, | ||
withdrawals, | ||
stateChannelsMap, | ||
version: 1 | ||
}; | ||
|
||
await storeService.set([{ path: storeKeyPrefix, value: sharedData }]); | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From looking around it seems like there are various ways of versioning a db schema.
Though I'm kinda confused as to why the
version
number is being put inSharedData
? I'm also confused how this handles migrations overall if there are no typed schemas between the versions?Maybe we should have a separate blob that holds both the version number and the schema at the corresponding version number. The migration script would then just diff
n-1
andn
to apply the migration.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StoreData = { schemaVersion: number, data: SharedData }
?