Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion addons/product/migrations/13.0.1.2/pre-migration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2020 ForgeFLow <http://www.forgeflow.com>
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade
from openupgradelib import openupgrade, openupgrade_merge_records
from odoo.tools import sql

_xmlid_renames = [
Expand Down Expand Up @@ -161,6 +161,33 @@ def insert_missing_product_template_attribute_value(env):
)


def merge_variants_with_same_attributes(env):
openupgrade.logged_query(
env.cr, """
WITH templates AS (
SELECT pt.id, pp.combination_indices, COUNT(*)
FROM product_product pp
JOIN product_template pt ON pt.id = pp.product_tmpl_id
WHERE pp.combination_indices IS NOT NULL
GROUP BY pt.id, pp.combination_indices
HAVING COUNT(*) > 1
)
SELECT pp.id, pp.product_tmpl_id
FROM product_product pp
JOIN product_template pt ON pt.id = pp.product_tmpl_id
JOIN templates ON (templates.id = pt.id
AND templates.combination_indices = pp.combination_indices)"""
)
templates = {}
for product_id, template_id in env.cr.fetchall():
templates.setdefault(template_id, []).append(product_id)
for template_id in templates:
openupgrade_merge_records.merge_records(
env, 'product.product', templates[template_id][1:],
templates[template_id][0], field_spec=None, method='sql',

@MiquelRForgeFlow MiquelRForgeFlow Dec 16, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question now is: should I put a field_spec?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends if the standard fields require it or default merge values are enough

@MiquelRForgeFlow MiquelRForgeFlow Dec 17, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my best option is {'default_code': 'first_not_null', 'active': 'or', 'barcode': 'first_not_null', 'volume': 'max', 'weight': 'max'}. My concern is for custom modules that add float/char/boolean fields, which would need to add more things in this dict. Maybe we could add in openupgradelib a functionality like if the dict contains 'other_fields': 'preserve', then every other field is excluded and preserved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete=True, exclude_columns=None, model_table='product_product')


def calculate_product_product_combination_indices(env):
"""Avoid product_product_combination_unique constraint + pre-compute
combination_indices field.
Expand All @@ -187,6 +214,7 @@ def calculate_product_product_combination_indices(env):
) grouped_pvc
WHERE grouped_pvc.product_product_id = pp.id""",
)
merge_variants_with_same_attributes(env)


def fill_product_template_attribute_value_attribute_line_id(env):
Expand Down