diff --git a/loyalty_criteria_multi_product/migrations/16.0.1.0.0/post-migration.py b/loyalty_criteria_multi_product/migrations/16.0.1.0.0/post-migration.py index 09f4bfef..ab894d0c 100644 --- a/loyalty_criteria_multi_product/migrations/16.0.1.0.0/post-migration.py +++ b/loyalty_criteria_multi_product/migrations/16.0.1.0.0/post-migration.py @@ -3,6 +3,22 @@ from openupgradelib import openupgrade +def _copy_multi_product_criteria(env): + """Copy multi-product criteria to the new m2m table. Done this way for avoiding to + rename columns, indexes, FKs, etc on the old m2m table, being this table not very + big for sure. + """ + openupgrade.logged_query( + env.cr, + """ + INSERT INTO loyalty_criteria_product_product_rel + (loyalty_criteria_id, product_product_id) + SELECT coupon_criteria_id, product_product_id + FROM coupon_criteria_product_product_rel + """, + ) + + def adapt_rules_with_repeat_product(env): """Adapt rules with the 'Repeat' option to new promotion rules. The functionality of the 'Repeat' option has been integrated into the rules @@ -11,17 +27,33 @@ def adapt_rules_with_repeat_product(env): For each loyalty criterion with 'Repeat' enabled, the corresponding loyalty rule is updated with the minimum quantity and product information. After the update, the loyalty criterion is unlinked.""" - env.cr.execute("SELECT id FROM loyalty_criteria WHERE repeat_product = true") - results = env.cr.fetchall() - loyalty_criteria_ids = [result[0] for result in results] - for loyalty_criteria in env["loyalty.criteria"].browse(loyalty_criteria_ids): - rule = env["loyalty.rule"].browse(loyalty_criteria.rule_id) - rule.minimum_qty = loyalty_criteria.rule_min_quantity - rule.product_ids = rule.product_ids - loyalty_criteria.unlink() - env.cr.commit() + openupgrade.logged_query( + env.cr, + """ + UPDATE loyalty_rule lr + SET minimum_qty = lc.rule_min_quantity + FROM loyalty_criteria lc + WHERE lc.repeat_product + AND lc.rule_id = lr.id + """, + ) + openupgrade.logged_query( + env.cr, + """ + INSERT INTO loyalty_rule_product_product_rel + (loyalty_rule_id, product_product_id) + SELECT lc.rule_id, rel2.product_product_id + FROM loyalty_criteria_product_product_rel rel2 + JOIN loyalty_criteria lc ON lc.id = rel2.loyalty_criteria_id + WHERE lc.repeat_product + """, + ) + openupgrade.logged_query( + env.cr, "DELETE FROM loyalty_criteria WHERE repeat_product" + ) @openupgrade.migrate() def migrate(env, version): + _copy_multi_product_criteria(env) adapt_rules_with_repeat_product(env) diff --git a/loyalty_criteria_multi_product/migrations/16.0.1.0.0/pre-migration.py b/loyalty_criteria_multi_product/migrations/16.0.1.0.0/pre-migration.py index 9ba5b894..6efc6d15 100644 --- a/loyalty_criteria_multi_product/migrations/16.0.1.0.0/pre-migration.py +++ b/loyalty_criteria_multi_product/migrations/16.0.1.0.0/pre-migration.py @@ -10,7 +10,7 @@ def move_coupon_criteria_to_rule(env): env.cr, """ ALTER TABLE loyalty_rule - ADD COLUMN IF NOT EXISTS loyalty_criteria CHAR + ADD COLUMN IF NOT EXISTS loyalty_criteria VARCHAR """, ) # Set the value of "lotalty_rule.loyalty_criteria" previously defined in @@ -39,9 +39,7 @@ def move_coupon_criteria_to_rule(env): UPDATE loyalty_criteria SET rule_id = loyalty_rule.id FROM loyalty_rule - WHERE - loyalty_rule.program_id = loyalty_criteria.program_id - AND loyalty_rule.loyalty_criteria = 'multi_gift' + WHERE loyalty_rule.program_id = loyalty_criteria.program_id """, )