-
Notifications
You must be signed in to change notification settings - Fork 21
refactor: credit ratio -> price & credit history filters #891
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
Open
amalcaraz
wants to merge
5
commits into
main
Choose a base branch
from
angel-credit_history_filters
base: main
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
5 commits
Select commit
Hold shift + click to select a range
ff63f9e
refactor: credit ratio -> price & credit history filters
amalcaraz 82d5448
fix: linter
amalcaraz f38ff72
Update src/aleph/db/accessors/balances.py
amalcaraz f544a68
fix: fixed price / ratio migration & include credit bonus_amount in r…
amalcaraz ac3cdfb
fix: migration bonus_amount
amalcaraz 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
deployment/migrations/versions/0040_e1f2a3b4c5d6_rename_credit_history_ratio_to_price.py
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,81 @@ | ||
| """rename credit_history ratio column to price | ||
|
|
||
| Revision ID: e1f2a3b4c5d6 | ||
| Revises: d0e1f2a3b4c5 | ||
| Create Date: 2025-12-01 00:00:00.000000 | ||
|
|
||
| """ | ||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = 'e1f2a3b4c5d6' | ||
| down_revision = 'd0e1f2a3b4c5' | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| # First, add the new price column | ||
| op.add_column('credit_history', sa.Column('price', sa.DECIMAL(), nullable=True)) | ||
|
|
||
| # Add the new bonus_amount column | ||
| op.add_column('credit_history', sa.Column('bonus_amount', sa.BigInteger(), nullable=True)) | ||
|
|
||
| # Transform data: price calculation depends on payment token | ||
| # Taking into account that bonus_ratio = 1.2 | ||
| # For ALEPH token: ratio = (1 / price) * bonus_ratio, therefore price = bonus_ratio / ratio | ||
| # For other tokens: price = 1/ratio | ||
| # Only update rows where payment_method is NOT 'credit_expense' or 'credit_transfer' | ||
| # and where ratio is not null and not zero | ||
| connection = op.get_bind() | ||
| connection.execute(sa.text(""" | ||
| UPDATE credit_history | ||
| SET price = CASE | ||
| WHEN token = 'ALEPH' THEN ROUND(1.2 / ratio, 18) | ||
| ELSE ROUND(1.0 / ratio, 18) | ||
| END | ||
| WHERE ratio IS NOT NULL | ||
| AND ratio != 0 | ||
| AND (payment_method IS NULL | ||
| OR (payment_method != 'credit_expense' AND payment_method != 'credit_transfer')) | ||
| """)) | ||
|
|
||
| # Update bonus_amount for ALEPH token records | ||
| connection.execute(sa.text(""" | ||
| UPDATE credit_history | ||
| SET bonus_amount = TRUNC(amount / 1.2) | ||
| WHERE token = 'ALEPH' | ||
| AND amount IS NOT NULL | ||
| """)) | ||
|
|
||
| # Drop the old ratio column | ||
| op.drop_column('credit_history', 'ratio') | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| # Add back the ratio column | ||
| op.add_column('credit_history', sa.Column('ratio', sa.DECIMAL(), nullable=True)) | ||
|
|
||
| # Transform data back: reverse price calculation depends on payment token | ||
| # For ALEPH token: price = 1.2 / ratio, therefore ratio = 1.2 / price | ||
| # For other tokens: ratio = 1/price | ||
| connection = op.get_bind() | ||
| connection.execute(sa.text(""" | ||
| UPDATE credit_history | ||
| SET ratio = CASE | ||
| WHEN token = 'ALEPH' THEN ROUND(1.2 / price, 18) | ||
| ELSE ROUND(1.0 / price, 18) | ||
| END | ||
| WHERE price IS NOT NULL | ||
| AND price != 0 | ||
| AND (payment_method IS NULL | ||
| OR (payment_method != 'credit_expense' AND payment_method != 'credit_transfer')) | ||
| """)) | ||
|
|
||
| # Drop the price column | ||
| op.drop_column('credit_history', 'price') | ||
|
|
||
| # Drop the bonus_amount column | ||
| op.drop_column('credit_history', 'bonus_amount') | ||
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.