Skip to content

Commit

Permalink
[FIX] base_comment_template: Incorrect m2m column names + script + av…
Browse files Browse the repository at this point in the history
…oid 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.
  • Loading branch information
pedrobaeza authored and tafaRU committed Aug 31, 2023
1 parent e82f354 commit a0e7e57
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base_comment_template/migrations/13.0.1.0.0/post-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
AND ip.res_id IS NOT NULL
Expand Down
21 changes: 21 additions & 0 deletions base_comment_template/migrations/13.0.3.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit a0e7e57

Please sign in to comment.