Repository navigation
feat(database): recalculate all balances migration #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
einhornimmond
wants to merge
23
commits into
einhornimmond:master
from
gradido:database_recalculate_balances_all_with_new_decay_algo2
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
bcf2322
recalculate all decay balances db migration
64625ca
move ledger anchor into abstract class, control ledger anchor ids wit…
e739624
remove leftofer
7618400
make where possible more secure against sql injection attacks
0daa153
remove usage of legacy decay calculation, remove decimal.js all toget…
914d054
fix lint
ab02185
reduce attack surface
4f47a81
fix lint
0f57731
finetuning
53e9c30
add debug logs
773587b
test other build flags
c39bece
update decay algo to work also on arm
c0ae82c
update decay algo
d74db95
small fix, code formatting
167a3e0
improve test
a76e5aa
add decimal.js for test, compare with legacy
17c29a1
lint
5f5fb43
Merge branch 'fix_decay_calculation_deterministic' into database_reca…
9f51898
zero negative balance, throw if calculateDecay is called with negativ…
bc5429d
remove compiler flags which aren't work anyway on arm
ab022da
update gradido node and gradido blockchain js
c95c771
lint, remove debugging, handle negative balance correct
a1e2f4b
update inspector polling
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
83 changes: 83 additions & 0 deletions
83
database/migration/migrations/0102-recalculate_balances.ts
This file contains hidden or 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 { DecayCalculationType, GradidoUnit } from 'shared' | ||
| import { calculateDecay } from 'shared-native' | ||
|
|
||
| export async function upgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) { | ||
| // Find all users & loop over them | ||
| const runningRequests: Promise<Array<any>>[] = [] | ||
|
einhornimmond marked this conversation as resolved.
Resolved
|
||
| const users = await queryFn('SELECT id FROM users') | ||
| let countDiffs = 0 | ||
| for (let u = 0; u < users.length; u++) { | ||
| process.stdout.write(`Recalculating balances for user ${u}/${users.length}\r`) | ||
| // find all transactions for a user | ||
| const transactions = await queryFn( | ||
| ` | ||
| SELECT id, amount_gdd4, balance_gdd4, balance_legacy, decay_gdd4, balance_date | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| FROM transactions | ||
| WHERE user_id = ? | ||
| ORDER BY balance_date ASC, id ASC | ||
| ; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| `, | ||
| [users[u].id], | ||
| ) | ||
|
|
||
| let previous = null | ||
| let balance = 0n | ||
| const transactionsToUpdate: string[] = [] | ||
|
|
||
| for (let t = 0; t < transactions.length; t++) { | ||
| const transaction = transactions[t] | ||
| const amount = BigInt(transaction.amount_gdd4) | ||
| let decay = 0n | ||
| if (balance > 0n && previous) { | ||
| const decayedBalance = calculateDecay( | ||
| balance, | ||
| GradidoUnit.effectiveDecayDuration(previous.balance_date, transaction.balance_date) | ||
| .seconds, | ||
| ) | ||
| decay = decayedBalance - balance | ||
| balance = decayedBalance | ||
| } else if (balance <= 0n) { | ||
| if (balance < 0) { | ||
| // biome-ignore lint/suspicious/noConsole: no logger in migration | ||
| console.warn( | ||
| `set negative balance: ${balance.toString()} for transaction: ${transaction.id} to zero.`, | ||
| ) | ||
| } | ||
| balance = 0n | ||
| } | ||
| balance += amount | ||
| if (BigInt(transaction.balance_gdd4) !== balance) { | ||
| countDiffs++ | ||
| transactionsToUpdate.push( | ||
| `UPDATE transactions | ||
| SET balance_gdd4 = '${balance.toString()}', | ||
| decay_gdd4 = '${decay.toString()}' | ||
| WHERE id = ${transaction.id} | ||
| ; | ||
| `, | ||
| ) | ||
| previous = transaction | ||
| } | ||
| } | ||
| if (transactionsToUpdate.length) { | ||
| runningRequests.push(queryFn(transactionsToUpdate.join('\n'))) | ||
| } | ||
| } | ||
| await Promise.all(runningRequests) | ||
| await queryFn(`UPDATE transactions set decay_calculation_type = ?`, [ | ||
| DecayCalculationType.NATIVE_C_FIXED_FACTOR_INTEGER, | ||
| ]) | ||
| process.stdout.write(`\n`) | ||
| // biome-ignore lint/suspicious/noConsole: no logger present | ||
| console.log(`${countDiffs} updated transactions`) | ||
| } | ||
|
einhornimmond marked this conversation as resolved.
Resolved
|
||
|
|
||
| export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) { | ||
| await queryFn(` | ||
| UPDATE transactions | ||
| SET balance_gdd4 = CAST(ROUND(balance_legacy * 10000) AS SIGNED), | ||
| decay_gdd4 = CAST(ROUND(decay_legacy * 10000) AS SIGNED); `) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| await queryFn( | ||
| `UPDATE transactions set decay_calculation_type = ${DecayCalculationType.DECIMAL_JS_FIXED_FACTOR}`, | ||
| ) | ||
| } | ||
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.