From 47f275aa3e2197af94ec2ed32c226eebf654d283 Mon Sep 17 00:00:00 2001 From: duongnguyen Date: Fri, 21 Jul 2023 11:40:25 +0700 Subject: [PATCH] [OU-ADD] hr: migrarion done --- .../scripts/hr/16.0.1.1/post-migration.py | 35 ++++ .../scripts/hr/16.0.1.1/pre-migration.py | 159 ++++++++++++++++++ .../hr/16.0.1.1/upgrade_analysis_work.txt | 74 ++++++++ 3 files changed, 268 insertions(+) create mode 100644 openupgrade_scripts/scripts/hr/16.0.1.1/post-migration.py create mode 100644 openupgrade_scripts/scripts/hr/16.0.1.1/pre-migration.py create mode 100644 openupgrade_scripts/scripts/hr/16.0.1.1/upgrade_analysis_work.txt diff --git a/openupgrade_scripts/scripts/hr/16.0.1.1/post-migration.py b/openupgrade_scripts/scripts/hr/16.0.1.1/post-migration.py new file mode 100644 index 000000000000..948a24cc80ad --- /dev/null +++ b/openupgrade_scripts/scripts/hr/16.0.1.1/post-migration.py @@ -0,0 +1,35 @@ +import logging + +from openupgradelib import openupgrade + +_logger = logging.getLogger(__name__) + + +def _m2m_to_o2m_plan_activity_type_ids(env): + """ + The field 'plan_activity_type_ids' has changed + from m2m to o2m, so we need to check the rel table (m2m table) + between them then fill value for 'plan_id' at hr.plan.activity.type + and after that ORM will do the rest for us + """ + openupgrade.logged_query( + env.cr, + """ + WITH tmp AS( + SELECT hp.id as hr_plan_id, hpat.id as hr_plan_activity_type_id + FROM hr_plan hp JOIN hr_plan_hr_plan_activity_type_rel rel + ON hp.id = rel.hr_plan_id JOIN hr_plan_activity_type hpat + ON hpat.id = rel.hr_plan_activity_type_id + ) + UPDATE hr_plan_activity_type hpat + SET plan_id = tmp.hr_plan_id + FROM tmp + WHERE hpat.id = tmp.hr_plan_activity_type_id + """, + ) + + +@openupgrade.migrate() +def migrate(env, version): + openupgrade.load_data(env.cr, "hr", "16.0.1.1/noupdate_changes.xml") + _m2m_to_o2m_plan_activity_type_ids(env) diff --git a/openupgrade_scripts/scripts/hr/16.0.1.1/pre-migration.py b/openupgrade_scripts/scripts/hr/16.0.1.1/pre-migration.py new file mode 100644 index 000000000000..f75f02d5a6f3 --- /dev/null +++ b/openupgrade_scripts/scripts/hr/16.0.1.1/pre-migration.py @@ -0,0 +1,159 @@ +from openupgradelib import openupgrade + +_xmlid_renames = [ + ( + "hr_contract.access_hr_contract_type_manager", + "hr.access_hr_contract_type_manager", + ), +] + + +def _hr_employee_fast_fill_work_contact_info(env): + """ + First we create some column then + if employee has user_id then set work_contact_id = user.partner_id + then update mobile_phone, work_email from work_contact_id.mobile + and work_contact_id.email for it + """ + if not openupgrade.column_exists( + env.cr, "hr_employee", "work_contact_id" + ): + openupgrade.add_fields( + env, + [ + ( + "work_contact_id", + "hr.employee", + "hr_employee", + "many2one", + False, + "hr", + ) + ], + ) + if not openupgrade.column_exists( + env.cr, "hr_employee", "mobile_phone" + ): + openupgrade.add_fields( + env, + [ + ( + "mobile_phone", + "hr.employee", + "hr_employee", + "char", + False, + "hr", + ), + ], + ) + if not openupgrade.column_exists( + env.cr, "hr_employee", "work_email" + ): + openupgrade.add_fields( + env, + [ + ( + "work_email", + "hr.employee", + "hr_employee", + "char", + False, + "hr", + ), + ], + ) + # Start filling for work_contact_id + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_employee he + SET work_contact_id = ru.partner_id + FROM res_users ru + WHERE ru.id = he.user_id AND he.user_id IS NOT NULL + """ + ) + # Start filling for mobile_phone + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_employee he + SET mobile_phone = rp.mobile + FROM res_partner rp + WHERE rp.id = he.work_contact_id AND he.work_contact_id IS NOT NULL + """ + ) + # Start filling for work_email + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_employee he + SET work_email = rp.email + FROM res_partner rp + WHERE rp.id = he.work_contact_id AND he.work_contact_id IS NOT NULL + """ + ) + + +def _hr_plan_fast_fill_company_id(env): + if not openupgrade.column_exists( + env.cr, "hr_plan", "company_id" + ): + openupgrade.add_fields( + env, + [ + ( + "company_id", + "hr.plan", + "hr_plan", + "many2one", + False, + "hr", + ) + ], + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_plan hp + SET company_id = ru.company_id + FROM res_users ru + WHERE ru.id = hp.create_uid + """ + ) + + +def _hr_plan_activity_type_fast_fill_company_id(env): + if not openupgrade.column_exists( + env.cr, "hr_plan_activity_type", "company_id" + ): + openupgrade.add_fields( + env, + [ + ( + "company_id", + "hr.plan.activity.type", + "hr_plan_activity_type", + "many2one", + False, + "hr", + ) + ], + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_plan_activity_type hpat + SET company_id = ru.company_id + FROM res_users ru + WHERE ru.id = hpat.create_uid + """ + ) + + +@openupgrade.migrate() +def migrate(env, version): + openupgrade.rename_xmlids(env.cr, _xmlid_renames) + _hr_employee_fast_fill_work_contact_info(env) + _hr_plan_fast_fill_company_id(env) + _hr_plan_activity_type_fast_fill_company_id(env) diff --git a/openupgrade_scripts/scripts/hr/16.0.1.1/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/hr/16.0.1.1/upgrade_analysis_work.txt new file mode 100644 index 000000000000..53838b09fd32 --- /dev/null +++ b/openupgrade_scripts/scripts/hr/16.0.1.1/upgrade_analysis_work.txt @@ -0,0 +1,74 @@ +---Models in module 'hr'--- +model hr.contract.type (moved from hr_contract) +# DONE: pre-migration: moved from hr_contract to hr + +---Fields in module 'hr'--- +hr / hr.contract.type / __last_update (datetime) : previously in module hr_contract +hr / hr.contract.type / _order : previously in module hr_contract +hr / hr.contract.type / display_name (char) : previously in module hr_contract +hr / hr.contract.type / name (char) : previously in module hr_contract +hr / hr.contract.type / sequence (integer) : NEW +# NOTHING TO DO + +hr / hr.department / master_department_id (many2one): NEW relation: hr.department, isfunction: function, stored +hr / hr.department / parent_path (char) : NEW +hr / hr.department / plan_ids (one2many) : NEW relation: hr.plan +hr / hr.employee / currency_id (many2one) : previously in module hr_timesheet +# NOTHING TO DO + +hr / hr.employee / mobile_phone (char) : now a function +hr / hr.employee / work_contact_id (many2one) : NEW relation: res.partner +hr / hr.employee / work_email (char) : now a function +# TODO: pre-migration: create column and fill value for work_contact_id, mobile_phone, work_email + +hr / hr.job / active (boolean) : NEW hasdefault: default +hr / hr.job / contract_type_id (many2one) : NEW relation: hr.contract.type +hr / hr.job / state (selection) : DEL required, selection_keys: ['open', 'recruit'] +# NOTHING TO DO: hr.job does not work with state anymore. + +hr / hr.plan / company_id (many2one) : NEW relation: res.company, hasdefault: default +# DONE: pre-migration create column and fill company_id base on company of create_uid + +hr / hr.plan / department_id (many2one) : NEW relation: hr.department +# NOTHING TO DO + +hr / hr.plan / plan_activity_type_ids (many2many): table is now 'False' ('hr_plan_hr_plan_activity_type_rel') +hr / hr.plan / plan_activity_type_ids (many2many): type is now 'one2many' ('many2many') +hr / hr.plan.activity.type / plan_id (many2one) : NEW relation: hr.plan +# DONE: pre-migration and post-migration: move data from many2many table to plan_id column in hr.plan.activity.type + +hr / hr.plan.activity.type / company_id (many2one) : NEW relation: res.company, hasdefault: default +# DONE: pre-migration create column and fill company_id base on company of create_uid + +hr / res.users / create_employee (boolean) : NEW hasdefault: default +hr / res.users / create_employee_id (many2one) : NEW relation: hr.employee +# NOTHING TO DO + +hr / resource.resource / employee_id (one2many) : NEW relation: hr.employee +# NOTHING TO DO + +---XML records in module 'hr'--- +NEW ir.actions.act_window: hr.hr_contract_type_action +# NOTHING TO DO + +NEW ir.actions.server: hr.action_hr_employee_create_user +# NOTHING TO DO + +NEW ir.model.access: hr.access_hr_contract_type_manager [renamed from hr_contract module] +# DONE: pre-migration: renamed + +NEW ir.rule: hr.hr_plan_activity_type_company_rule (noupdate) +NEW ir.rule: hr.hr_plan_company_rule (noupdate) +# NOTHING TO DO + +NEW ir.ui.menu: hr.menu_config_employee +NEW ir.ui.menu: hr.menu_config_recruitment +NEW ir.ui.menu: hr.menu_view_hr_contract_type +DEL ir.ui.menu: hr.menu_config_plan_types +DEL ir.ui.menu: hr.menu_human_resources_configuration_employee +NEW ir.ui.view: hr.hr_contract_type_view_form +NEW ir.ui.view: hr.hr_contract_type_view_tree +NEW ir.ui.view: hr.view_employee_form_smartbutton +NEW ir.ui.view: hr.view_users_simple_form +NEW ir.ui.view: hr.view_users_simple_form_inherit_hr +# NOTHING TO DO