-
-
Notifications
You must be signed in to change notification settings - Fork 784
[16.0][IMP] account_invoice_triple_discount: Improve migration performance #2279
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
Draft
lmignon
wants to merge
1
commit into
OCA:16.0
Choose a base branch
from
acsone:16.0-account_triple_discount-perf-lmi
base: 16.0
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.
Draft
Changes from all commits
Commits
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
119 changes: 73 additions & 46 deletions
119
account_invoice_triple_discount/migrations/16.0.2.0.0/pre-migrate.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 |
|---|---|---|
| @@ -1,64 +1,91 @@ | ||
| # Copyright 2024 Camptocamp SA | ||
| # Copyright 2026 ACSONE SA/NV (https://www.acsone.eu) | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) | ||
| from openupgradelib import openupgrade | ||
| import logging | ||
|
|
||
| from odoo import SUPERUSER_ID | ||
| from odoo.api import Environment | ||
| from odoo.tools import column_exists | ||
|
|
||
| USE_OPENUPGRADE = False | ||
| USE_ODOOUPGRADE = False | ||
|
|
||
| try: | ||
| from odoo.upgrade import util | ||
|
|
||
| USE_ODOOUPGRADE = True | ||
| except ImportError: | ||
| try: | ||
| from openupgradelib import openupgrade | ||
|
|
||
| USE_OPENUPGRADE = True | ||
| except ImportError as err: | ||
| raise ImportError( | ||
| "This migration script requires openupgradelib or odoo.upgrade.util.\n" | ||
| "Please install one of these libraries to proceed with the migration.\n" | ||
| "For better performances, it is recommended to use odoo.upgrade.util.\n" | ||
| "See https://github.com/odoo/upgrade-util/" | ||
| ) from err | ||
|
|
||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def migrate_discount_to_discount1(env): | ||
| openupgrade.add_fields( | ||
| env, | ||
| [ | ||
| ( | ||
| "discount1", | ||
| "account.move.line", | ||
| "account_move_line", | ||
| "float", | ||
| "numeric", | ||
| "account_invoice_triple_discount", | ||
| 0.0, | ||
| ) | ||
| ], | ||
| ) | ||
| openupgrade.logged_query( | ||
| env.cr, | ||
| """ | ||
| if not column_exists(env.cr, "account_move_line", "discount1"): | ||
| env.cr.execute( | ||
| """ | ||
| ALTER TABLE account_move_line | ||
| ADD COLUMN discount1 numeric; | ||
| """ | ||
| ) | ||
| _logger.info("Added column 'discount1' to 'account_move_line'") | ||
|
|
||
| query = """ | ||
| UPDATE account_move_line | ||
| SET discount1 = discount; | ||
| """, | ||
| ) | ||
| # if discounts are : 10% - 20% - 30% main discount is : 49.6 % | ||
| # if discounts are : 05% - 09% - 13% main discount is : 24.7885 % | ||
| SET discount1 = discount, | ||
| """ | ||
|
|
||
| if "discount_fixed" not in env.registry.models["account.move.line"]._fields: | ||
| openupgrade.logged_query( | ||
| env.cr, | ||
| """ | ||
| UPDATE account_move_line | ||
| SET discount = 100 * ( | ||
| query += """ | ||
| discount = 100 * ( | ||
| 1 - ( | ||
| (100 - COALESCE(discount1, 0.0)) / 100 | ||
| (100 - COALESCE(discount, 0.0)) / 100 | ||
| * (100 - COALESCE(discount2, 0.0)) / 100 | ||
| * (100 - COALESCE(discount3, 0.0)) / 100 | ||
| ) | ||
| ); | ||
| """, | ||
| ) | ||
| ) | ||
| """ | ||
|
|
||
| else: | ||
| # don't touch lines with fixed discount | ||
| openupgrade.logged_query( | ||
| env.cr, | ||
| """ | ||
| UPDATE account_move_line | ||
| SET discount = 100 * ( | ||
| 1 - ( | ||
| (100 - COALESCE(discount1, 0.0)) / 100 | ||
| * (100 - COALESCE(discount2, 0.0)) / 100 | ||
| * (100 - COALESCE(discount3, 0.0)) / 100 | ||
| # We don't use a WHERE clause since the explode_query_range will | ||
| # add a WHERE clause with an id range, so we need to express | ||
| # the condition in a way that is compatible with that. | ||
| query += """ | ||
| discount = | ||
| CASE | ||
| WHEN discount_fixed == 0 THEN | ||
| 100 * ( | ||
| 1 - ( | ||
| (100 - COALESCE(discount, 0.0)) / 100 | ||
| * (100 - COALESCE(discount2, 0.0)) / 100 | ||
| * (100 - COALESCE(discount3, 0.0)) / 100 | ||
| ) | ||
| ) | ||
| ) | ||
| WHERE discount_fixed == 0; | ||
| """, | ||
| ELSE discount | ||
| END | ||
| """ | ||
|
|
||
| if USE_OPENUPGRADE: | ||
| openupgrade.logged_query(env.cr, query) | ||
|
|
||
| else: | ||
| util.parallel_execute( | ||
| env.cr, util.explode_query_range(env.cr, query, table="account_move_line") | ||
| ) | ||
|
|
||
|
|
||
| @openupgrade.migrate() | ||
| def migrate(env, version): | ||
| def migrate(cr, version): | ||
| env = Environment(cr, SUPERUSER_ID, {}) | ||
|
Comment on lines
-62
to
+90
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change ? I think we could simplify the script. |
||
| migrate_discount_to_discount1(env) | ||
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.
wouldnt this add discount2 and discount3 to discount again?
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.
when the request is executed, discount1 is not yet initialized and discount on the right side of the operator contains the original value into the row...
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.
ah I see now