Skip to content

Commit

Permalink
[MIG] website_sale (continuation)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiquelRForgeFlow authored and hieu1211 committed Jan 25, 2022
1 parent 60b404c commit f4f950b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
---Models in module 'website_sale'---
obsolete model product.style (renamed to product.ribbon)
new model product.ribbon (renamed from product.style)
# DONE: pre-migration: renamed models and tables

new model website.sale.extra.field
# NOTHING TO DO

---Fields in module 'website_sale'---
website_sale / product.public.category / _order : _order is now 'sequence, name, id' ('sequence, name')
website_sale / product.public.category / seo_name (char) : NEW
website_sale / product.ribbon / bg_color (char) : NEW
# NOTHING TO DO

website_sale / product.style / name (char) : DEL required
website_sale / product.ribbon / html (char) : NEW required
website_sale / product.ribbon / text_color (char) : NEW
# DONE: pre-migration: renamed fields

website_sale / product.style / html_class (char) : now required, req_default: function
website_sale / product.style / name (char) : DEL required
# DONE: pre-migration: set default '' in case is NULL

website_sale / product.ribbon / bg_color (char) : NEW
website_sale / product.ribbon / text_color (char) : NEW
website_sale / product.template / seo_name (char) : NEW
# NOTHING TO DO

website_sale / product.template / website_ribbon_id (many2one) : NEW relation: product.ribbon
website_sale / product.template / website_style_ids (many2many) : DEL relation: product.style
# Done in pre-migration
# DONE: post-migration: if there is more than one valid website_style_ids, then create a fused ribbon from those styles

website_sale / website / shop_extra_field_ids (one2many): NEW relation: website.sale.extra.field
website_sale / website.sale.extra.field / field_id (many2one) : NEW relation: ir.model.fields
website_sale / website.sale.extra.field / sequence (integer) : NEW hasdefault
website_sale / website.sale.extra.field / website_id (many2one) : NEW relation: website
# NOTHING TO DO: new features

---XML records in module 'website_sale'---
DEL ir.actions.act_window: website_sale.action_abandoned_orders_ecommerce
NEW ir.actions.server: website_sale.dynamic_snippet_products_action
NEW ir.model.access: website_sale.access_ecom_extra_fields_public
NEW ir.model.access: website_sale.access_ecom_extra_fields_publisher
NEW ir.model.access: website_sale.access_product_ribbon_public
NEW ir.model.access: website_sale.access_product_ribbon_sale_manager
NEW ir.model.access: website_sale.access_website_sale_payment_acquirer_onboarding_wizard
DEL ir.model.access: website_sale.access_product_style
DEL ir.model.access: website_sale.access_product_supplierinfo
DEL ir.model.access: website_sale.access_product_supplierinfo_user
# NOTHING TO DO

DEL ir.model.access: website_sale.access_product_style
NEW ir.model.access: website_sale.access_product_ribbon_public
# DONE: pre-migration: renamed xmlids

DEL ir.rule: website_sale.product_supplierinfo_public (noupdate)
NEW ir.ui.view: website_sale.assets_snippet_s_dynamic_snippet_products_js_000
NEW ir.ui.view: website_sale.assets_snippet_s_products_searchbar_js_000
Expand Down Expand Up @@ -58,8 +77,13 @@ DEL ir.ui.view: website_sale.header
DEL ir.ui.view: website_sale.remove_external_snippets
DEL ir.ui.view: website_sale.website_sale_pricelist_form_view2
NEW product.ribbon: website_sale.new_ribbon (noupdate)
NEW product.ribbon: website_sale.sale_ribbon (noupdate)
NEW product.ribbon: website_sale.sold_out_ribbon (noupdate)
DEL product.style: website_sale.image_full (noupdate)
DEL product.style: website_sale.image_promo (noupdate)
NEW website.snippet.filter: website_sale.dynamic_filter_demo_products
# NOTHING TO DO

DEL product.style: website_sale.image_promo (noupdate)
NEW product.ribbon: website_sale.sale_ribbon (noupdate)
# DONE: pre-migration: renamed xmlid (and update values)

DEL product.style: website_sale.image_full (noupdate)
# DONE: post-migration: deleted safely
58 changes: 44 additions & 14 deletions openupgrade_scripts/scripts/website_sale/14.0.1.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
# Copyright (C) 2021 Open Source Integrators <https://www.opensourceintegrators.com/>
# Copyright 2021 ForgeFlow S.L. <https://www.forgeflow.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
env.cr.execute(
""" UPDATE
product_template
SET
website_ribbon_id =
( SELECT
product_style_id
FROM
product_style_product_template_rel
WHERE
product_template_id=product_template.id
ORDER BY id DESC
LIMIT 1)
"""
# set simple ribbons (simple case):
openupgrade.logged_query(
env.cr,
"""
UPDATE product_template pt
SET website_ribbon_id = sub.id
FROM (
SELECT min(rel.product_style_id) as id, rel.product_template_id
FROM product_style_product_template_rel rel
JOIN product_ribbon pr ON pr.id = rel.product_style_id
WHERE pr.html_class NOT IN ('oe_image_full', '')
GROUP BY rel.product_template_id
HAVING count(pr.id) = 1
) as sub
WHERE sub.product_template_id = pt.id""",
)
# set new fused ribbons (complex case):
openupgrade.logged_query(
env.cr,
"""
WITH sub AS (
SELECT STRING_AGG(pr.html, ' ' ORDER BY pr.html) as new_name,
STRING_AGG(pr.html_class, ' ' ORDER BY pr.html_class) as group_class,
rel.product_template_id
FROM product_style_product_template_rel rel
JOIN product_ribbon pr ON pr.id = rel.product_style_id
WHERE pr.html_class NOT IN ('oe_image_full', '')
GROUP BY rel.product_template_id
HAVING count(pr.id) > 1
), new_ribbon AS (
INSERT INTO product_ribbon (html, html_class)
SELECT DISTINCT new_name, group_class
FROM sub
RETURNING id, html, html_class
)
UPDATE product_template pt
SET website_ribbon_id = pr.id
FROM sub
JOIN new_ribbon pr ON (
sub.new_name = pr.html AND sub.group_class = pr.html_class)
WHERE pt.id = sub.product_template_id AND pt.website_ribbon_id IS NULL""",
)
openupgrade.load_data(env.cr, "website_sale", "14.0.1.0/noupdate_changes.xml")
openupgrade.delete_records_safely_by_xml_id(env, ["website_sale.image_full"])
40 changes: 40 additions & 0 deletions openupgrade_scripts/scripts/website_sale/14.0.1.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (C) 2021 Open Source Integrators <https://www.opensourceintegrators.com/>
# Copyright 2021 ForgeFlow S.L. <https://www.forgeflow.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from openupgradelib import openupgrade
Expand All @@ -15,9 +16,48 @@
("product_style", "product_ribbon"),
]

_xmlid_renames = [
("website_sale.image_promo", "website_sale.sale_ribbon"),
("website_sale.access_product_style", "website_sale.access_product_ribbon_public"),
]


def update_product_ribbon_sale_ribbon(env):
# it is noupdate data, not in noupdate_changes.xml
openupgrade.logged_query(
env.cr,
"""
UPDATE product_ribbon pr
SET html_class = 'bg-success o_ribbon_left', html = 'Sale'
FROM ir_model_data imd
WHERE imd.model = 'product.ribbon' AND imd.res_id = pr.id
AND imd.module = 'website_sale' AND imd.name = 'sale_ribbon'""",
)


def set_product_ribbon_html_class_default(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE product_ribbon
SET html_class = trim(both from html_class)
WHERE NULLIF(html_class, '') IS NOT NULL""",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE product_ribbon
SET html_class = ''
WHERE html_class IS NULL""",
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_models(env.cr, _model_renames)
openupgrade.rename_tables(env.cr, _table_renames)
openupgrade.rename_fields(env, _field_renames)
openupgrade.rename_xmlids(env.cr, _xmlid_renames)
update_product_ribbon_sale_ribbon(env)
set_product_ribbon_html_class_default(env)
openupgrade.remove_tables_fks(env.cr, ["product_style_product_template_rel"])

0 comments on commit f4f950b

Please sign in to comment.