From 34333c421641c39397b03bd49889e8fca3131484 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Sat, 15 May 2021 16:58:39 +0200 Subject: [PATCH] [FIX] base_comment_template: Incorrect m2m column names + script + avoid FK constraint - The name of the columns were swapped, so better to fix it for avoiding mistakes due to this. - Migration script for detecting the condition and swap column names. - On v12 > v13 migration, include JOINs for avoiding FK constraint, which `ON CONFLICT` clause doesn't protect. --- .../migrations/13.0.1.0.0/pre-migration.py | 3 +++ .../migrations/13.0.3.0.0/pre-migration.py | 21 +++++++++++++++ .../migrations/14.0.1.0.0/pre-migration.py | 26 +++++++++++++++++++ .../models/base_comment_template.py | 4 +-- base_comment_template/models/res_partner.py | 4 +-- 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 base_comment_template/migrations/13.0.3.0.0/pre-migration.py create mode 100644 base_comment_template/migrations/14.0.1.0.0/pre-migration.py diff --git a/base_comment_template/migrations/13.0.1.0.0/pre-migration.py b/base_comment_template/migrations/13.0.1.0.0/pre-migration.py index edac6891..3a875e8d 100644 --- a/base_comment_template/migrations/13.0.1.0.0/pre-migration.py +++ b/base_comment_template/migrations/13.0.1.0.0/pre-migration.py @@ -16,6 +16,9 @@ def migrate(env, version): SPLIT_PART(ip.value_reference, ',', 2)::int AS base_comment_template_id FROM ir_property ip JOIN ir_model_fields imf ON ip.fields_id = imf.id + JOIN res_partner rp ON rp.id = SPLIT_PART(ip.res_id, ',', 2)::int + JOIN base_comment_template bct + ON bct.id = SPLIT_PART(ip.value_reference, ',', 2)::int WHERE imf.name = 'property_comment_template_id' AND imf.model = 'res.partner' """, diff --git a/base_comment_template/migrations/13.0.3.0.0/pre-migration.py b/base_comment_template/migrations/13.0.3.0.0/pre-migration.py new file mode 100644 index 00000000..2ed21069 --- /dev/null +++ b/base_comment_template/migrations/13.0.3.0.0/pre-migration.py @@ -0,0 +1,21 @@ +# Copyright 2021 Tecnativa - Pedro M: Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from openupgradelib import openupgrade # pylint: disable=W7936 + + +@openupgrade.migrate() +def migrate(env, version): + if openupgrade.table_exists(env.cr, "base_comment_template_res_partner_rel"): + # Swap column names, as they were incorrect + env.cr.execute( + "ALTER TABLE base_comment_template_res_partner_rel " + "RENAME base_comment_template_id TO temp" + ) + env.cr.execute( + "ALTER TABLE base_comment_template_res_partner_rel " + "RENAME res_partner_id TO base_comment_template_id" + ) + env.cr.execute( + "ALTER TABLE base_comment_template_res_partner_rel " + "RENAME temp TO res_partner_id" + ) diff --git a/base_comment_template/migrations/14.0.1.0.0/pre-migration.py b/base_comment_template/migrations/14.0.1.0.0/pre-migration.py new file mode 100644 index 00000000..8775e5dd --- /dev/null +++ b/base_comment_template/migrations/14.0.1.0.0/pre-migration.py @@ -0,0 +1,26 @@ +# Copyright 2021 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from openupgradelib import openupgrade + +field_renames = [ + ("base.comment.template", "base_comment_template", "priority", "sequence"), +] + + +@openupgrade.migrate() +def migrate(env, version): + # Not tested + openupgrade.logged_query( + env.cr, + """ + INSERT INTO base_comment_template_res_partner_rel + (res_partner_id, base_comment_template_id) + SELECT SPLIT_PART(ip.res_id, ',', 2)::int AS res_partner_id, + SPLIT_PART(ip.value_reference, ',', 2)::int AS base_comment_template_id + FROM ir_property ip + JOIN ir_model_fields imf ON ip.fields_id = imf.id + WHERE imf.name = 'property_comment_template_id' + AND imf.model = 'res.partner' + """, + ) + openupgrade.rename_fields(env, field_renames) diff --git a/base_comment_template/models/base_comment_template.py b/base_comment_template/models/base_comment_template.py index 92d86807..1d6905af 100644 --- a/base_comment_template/models/base_comment_template.py +++ b/base_comment_template/models/base_comment_template.py @@ -45,8 +45,8 @@ class BaseCommentTemplate(models.Model): partner_ids = fields.Many2many( comodel_name="res.partner", relation="base_comment_template_res_partner_rel", - column1="res_partner_id", - column2="base_comment_template_id", + column1="base_comment_template_id", + column2="res_partner_id", string="Partner", readonly=True, help="If set, the comment template will be available only for the selected " diff --git a/base_comment_template/models/res_partner.py b/base_comment_template/models/res_partner.py index 9d9b1c60..cc06cbdc 100644 --- a/base_comment_template/models/res_partner.py +++ b/base_comment_template/models/res_partner.py @@ -11,8 +11,8 @@ class ResPartner(models.Model): base_comment_template_ids = fields.Many2many( comodel_name="base.comment.template", relation="base_comment_template_res_partner_rel", - column1="base_comment_template_id", - column2="res_partner_id", + column1="res_partner_id", + column2="base_comment_template_id", string="Comment Templates", help="Specific partner comments that can be included in reports", )