Skip to content
Snippets Groups Projects
Commit c354cf79 authored by ノウラ's avatar ノウラ
Browse files

[FIX] delivery: speed up delivery costs installation


Due to hefty data the RAM limit gets exhausted.
The process gets killed due to the computed field weight on
- stock.move
- stock.picking
When installing the delivery costs module.

To solve the problem:

We add the column weight to the DB schema.

Ticket ids:
- 3013955
- 2628251
- 3028081

closes odoo/odoo#107020

Signed-off-by: default avatarArnold Moyaux (arm) <arm@odoo.com>
parent 5ad43068
No related branches found
No related tags found
No related merge requests found
......@@ -7,5 +7,5 @@ from . import product_packaging
from . import product_template
from . import sale_order
from . import partner
from . import stock_picking
from . import stock_move
from . import stock_picking
......@@ -2,12 +2,27 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
from odoo.tools.sql import column_exists, create_column
class StockMove(models.Model):
_inherit = 'stock.move'
def _auto_init(self):
if not column_exists(self.env.cr, "stock_move", "weight"):
# In case of a big database with a lot of stock moves, the RAM gets exhausted
# To prevent a process from being killed We create the column 'weight' manually
# Then we do the computation in a query by multiplying product weight with qty
create_column(self.env.cr, "stock_move", "weight", "numeric")
self.env.cr.execute("""
UPDATE stock_move move
SET weight = move.product_qty * product.weight
FROM product_product product
WHERE move.product_id = product.id
AND move.state != 'cancel'
""")
return super()._auto_init()
weight = fields.Float(compute='_cal_move_weight', digits='Stock Weight', store=True, compute_sudo=True)
@api.depends('product_id', 'product_uom_qty', 'product_uom')
......
......@@ -6,7 +6,7 @@ from collections import defaultdict
from odoo import models, fields, api, _
from odoo.exceptions import UserError
from odoo.tools.sql import column_exists, create_column
class StockQuantPackage(models.Model):
......@@ -55,6 +55,25 @@ class StockQuantPackage(models.Model):
class StockPicking(models.Model):
_inherit = 'stock.picking'
def _auto_init(self):
if not column_exists(self.env.cr, "stock_picking", "weight"):
# In order to speed up module installation when dealing with hefty data
# We create the column weight manually, but the computation will be skipped
# Therefore we do the computation in a query by getting weight sum from stock moves
create_column(self.env.cr, "stock_picking", "weight", "numeric")
self.env.cr.execute("""
WITH computed_weight AS (
SELECT SUM(weight) AS weight_sum, picking_id
FROM stock_move
WHERE picking_id IS NOT NULL
GROUP BY picking_id
)
UPDATE stock_picking
SET weight = weight_sum
FROM computed_weight
WHERE stock_picking.id = computed_weight.picking_id;
""")
return super()._auto_init()
@api.depends('move_line_ids', 'move_line_ids.result_package_id')
def _compute_packages(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment