From 94c5d227fb6ba12779484498bcccc065b3ee95ce Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Thu, 23 May 2019 15:17:15 +0200 Subject: [PATCH] [IMP] partner_industry_secondary: Better handling of transition to this module (#741) --- .../migrations/11.0.1.0.0/post-migration.py | 67 ------------------- .../migrations/11.0.1.0.0/pre-migration.py | 32 +++++---- 2 files changed, 15 insertions(+), 84 deletions(-) delete mode 100644 partner_industry_secondary/migrations/11.0.1.0.0/post-migration.py diff --git a/partner_industry_secondary/migrations/11.0.1.0.0/post-migration.py b/partner_industry_secondary/migrations/11.0.1.0.0/post-migration.py deleted file mode 100644 index 304132ddd..000000000 --- a/partner_industry_secondary/migrations/11.0.1.0.0/post-migration.py +++ /dev/null @@ -1,67 +0,0 @@ -# Copyright 2018 Eficent Business and IT Consulting Services, S.L. -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from openupgradelib import openupgrade -from psycopg2.extensions import AsIs - - -def fill_res_partner_industry(cr): - cr.execute( - """ - INSERT INTO res_partner_industry (name, active, create_uid, - create_date, write_uid, write_date, %s) - SELECT name, TRUE as active, create_uid, create_date, write_uid, - write_date, id - FROM res_partner_sector - """, (AsIs(openupgrade.get_legacy_name('old_sector_id')), ), - ) - # corrected parent_id here: - cr.execute( - """ - UPDATE res_partner_industry rpi - SET parent_id = rpi2.id - FROM res_partner_sector rps - INNER JOIN res_partner_industry rpi2 ON rps.parent_id = rpi2.%s - WHERE rpi.%s = rps.id - """, ( - AsIs(openupgrade.get_legacy_name('old_sector_id')), - AsIs(openupgrade.get_legacy_name('old_sector_id')), - ), - ) - - -def update_res_partner_industry(cr): - cr.execute( - """ - UPDATE res_partner rp - SET industry_id = rpi.id - FROM res_partner_industry rpi - WHERE rp.%s IS NOT NULL AND rp.%s = rpi.%s - """, ( - AsIs(openupgrade.get_legacy_name('sector_id')), - AsIs(openupgrade.get_legacy_name('sector_id')), - AsIs(openupgrade.get_legacy_name('old_sector_id')), - ), - ) - - -def fill_res_partner_industry_secondary(cr): - cr.execute( - """ - INSERT INTO res_partner_res_partner_industry_rel (res_partner_id, - res_partner_industry_id) - SELECT rel.res_partner_id, rpi.id - FROM res_partner_res_partner_sector_rel rel - LEFT JOIN res_partner_industry rpi - ON rel.res_partner_sector_id = rpi.%s - """, (AsIs(openupgrade.get_legacy_name('old_sector_id')), ), - ) - - -@openupgrade.migrate(use_env=True) -def migrate(env, version): - cr = env.cr - if openupgrade.table_exists(cr, 'res_partner_sector'): - fill_res_partner_industry(cr) - env['res.partner.industry']._parent_store_compute() - update_res_partner_industry(cr) - fill_res_partner_industry_secondary(cr) diff --git a/partner_industry_secondary/migrations/11.0.1.0.0/pre-migration.py b/partner_industry_secondary/migrations/11.0.1.0.0/pre-migration.py index bd105b53a..a24df56a5 100644 --- a/partner_industry_secondary/migrations/11.0.1.0.0/pre-migration.py +++ b/partner_industry_secondary/migrations/11.0.1.0.0/pre-migration.py @@ -1,27 +1,25 @@ # Copyright 2018 Eficent Business and IT Consulting Services, S.L. +# Copyright 2019 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from openupgradelib import openupgrade -from psycopg2.extensions import AsIs -_column_renames = { - 'res_partner': [('sector_id', None)], -} +from openupgradelib import openupgrade _xmlid_renames = [ - ('partner_sector.group_use_partner_sector_for_person', + ('partner_industry_secondary.group_use_partner_sector_for_person', 'partner_industry_secondary.group_use_partner_industry_for_person'), ] -@openupgrade.migrate(use_env=True) +@openupgrade.migrate() def migrate(env, version): - cr = env.cr - if openupgrade.table_exists(cr, 'res_partner_sector'): - openupgrade.rename_columns(cr, _column_renames) - openupgrade.rename_xmlids(cr, _xmlid_renames) - cr.execute( - """ - ALTER TABLE res_partner_industry - ADD COLUMN %s integer; - """, (AsIs(openupgrade.get_legacy_name('old_sector_id')), ), - ) + openupgrade.rename_xmlids(env.cr, _xmlid_renames) + # No need to check existence, as if getting to this, it will exist + openupgrade.rename_tables( + env.cr, [('res_partner_res_partner_sector_rel', + 'res_partner_res_partner_industry_rel')], + ) + openupgrade.rename_columns(env.cr, { + 'res_partner_res_partner_industry_rel': [ + ('res_partner_sector_id', 'res_partner_industry_id'), + ] + })