From e7e27b806ff61329aba7825cd27f818eb292d22d Mon Sep 17 00:00:00 2001 From: Vimal-OSI Date: Mon, 16 May 2022 18:46:02 +0530 Subject: [PATCH] [14.0][IMP]init hooks for avoiding data loss for state field. --- partner_stage/__init__.py | 2 +- partner_stage/__manifest__.py | 1 + partner_stage/init_hook.py | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/partner_stage/__init__.py b/partner_stage/__init__.py index d71877a496b..eb922e7adb1 100644 --- a/partner_stage/__init__.py +++ b/partner_stage/__init__.py @@ -1,2 +1,2 @@ from . import models -from .init_hook import post_init_hook +from .init_hook import post_init_hook, pre_init_hook diff --git a/partner_stage/__manifest__.py b/partner_stage/__manifest__.py index 9f9f873ccad..f39e166cfdf 100644 --- a/partner_stage/__manifest__.py +++ b/partner_stage/__manifest__.py @@ -17,6 +17,7 @@ "views/res_partner_views.xml", ], "post_init_hook": "post_init_hook", + "pre_init_hook": "pre_init_hook", "installable": True, "maintainers": ["dreispt"], } diff --git a/partner_stage/init_hook.py b/partner_stage/init_hook.py index 02f5b8c0c64..48425c12347 100644 --- a/partner_stage/init_hook.py +++ b/partner_stage/init_hook.py @@ -8,12 +8,30 @@ _logger = logging.getLogger(__name__) +def pre_init_hook(cr): + api.Environment(cr, SUPERUSER_ID, {}) + query = """ + ALTER TABLE res_partner + ADD COLUMN state_old VARCHAR + """ + cr.execute(query) + cr.execute("UPDATE res_partner SET state_old = state") + + def post_init_hook(cr, registry): """Set default Stage on partners""" env = api.Environment(cr, SUPERUSER_ID, {}) Partner = env["res.partner"] default_stage = Partner._get_default_stage_id() missing_stages = Partner.search([("stage_id", "=", False)]) + + query = """ + UPDATE res_partner as prt SET stage_id = ( + SELECT id FROM res_partner_stage as ps WHERE ps.state = prt.state_old + ) + """ + cr.execute(query) + cr.execute("ALTER TABLE res_partner DROP COLUMN state_old") if default_stage and missing_stages: _logger.info("Init stage_id for %d partner records...", len(missing_stages)) cr.execute(