From ca36c2ff4f80b913342ef8c3aa85b62ab0ad0ebf Mon Sep 17 00:00:00 2001
From: nouraellm <nea@odoo.com>
Date: Sun, 18 Sep 2022 19:38:23 +0000
Subject: [PATCH] [FIX] sale_margin: speed up module installation

- Due to hefty data the RAM limit gets exhausted.
- The process gets killed due to computed field margin on sale.order.line

To solve the problem:
- We add column margin to DB schema.

Task id: #2950878

closes odoo/odoo#100453

Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
---
 addons/sale_margin/models/sale_order.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/addons/sale_margin/models/sale_order.py b/addons/sale_margin/models/sale_order.py
index 6d4aac674155..69aca28583db 100644
--- a/addons/sale_margin/models/sale_order.py
+++ b/addons/sale_margin/models/sale_order.py
@@ -2,11 +2,18 @@
 # 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 SaleOrderLine(models.Model):
     _inherit = "sale.order.line"
 
+    def _auto_init(self):
+        if not column_exists(self.env.cr, "sale_order_line", "margin"):
+            # By creating the column 'margin' manually we steer clear of hefty data computation.
+            create_column(self.env.cr, "sale_order_line", "margin", "NUMERIC")
+        return super()._auto_init()
+
     margin = fields.Float(compute='_product_margin', digits='Product Price', store=True)
     purchase_price = fields.Float(string='Cost', digits='Product Price')
 
-- 
GitLab