From 5f58e1378e872d09cb766fa227f142894df42185 Mon Sep 17 00:00:00 2001 From: tranngocson1996 Date: Thu, 23 Jun 2022 14:05:03 +0700 Subject: [PATCH 1/2] [MIG] hr_holidays: migration script --- docsource/modules140-150.rst | 2 +- .../hr_holidays/15.0.1.5/noupdate_changes.xml | 6 +- .../hr_holidays/15.0.1.5/post-migration.py | 47 ++++ .../hr_holidays/15.0.1.5/pre-migration.py | 232 ++++++++++++++++++ .../15.0.1.5/upgrade_analysis_work.txt | 192 +++++++++++++++ 5 files changed, 475 insertions(+), 4 deletions(-) create mode 100644 openupgrade_scripts/scripts/hr_holidays/15.0.1.5/post-migration.py create mode 100644 openupgrade_scripts/scripts/hr_holidays/15.0.1.5/pre-migration.py create mode 100644 openupgrade_scripts/scripts/hr_holidays/15.0.1.5/upgrade_analysis_work.txt diff --git a/docsource/modules140-150.rst b/docsource/modules140-150.rst index 6c35a861d467..202397d88a8e 100644 --- a/docsource/modules140-150.rst +++ b/docsource/modules140-150.rst @@ -182,7 +182,7 @@ Module coverage 14.0 -> 15.0 +-------------------------------------------------+----------------------+-------------------------------------------------+ | hr_gamification |Nothing to do |No DB layout changes. | +-------------------------------------------------+----------------------+-------------------------------------------------+ -| hr_holidays | | | +| hr_holidays | Done | | +-------------------------------------------------+----------------------+-------------------------------------------------+ | hr_holidays_attendance | | | +-------------------------------------------------+----------------------+-------------------------------------------------+ diff --git a/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/noupdate_changes.xml b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/noupdate_changes.xml index 15cd67479438..f18b0175a92e 100644 --- a/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/noupdate_changes.xml +++ b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/noupdate_changes.xml @@ -2,7 +2,7 @@ officer - + 2 no @@ -10,7 +10,7 @@ officer - + 4 yes @@ -24,7 +24,7 @@ officer - + 5 no diff --git a/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/post-migration.py b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/post-migration.py new file mode 100644 index 000000000000..f88713619628 --- /dev/null +++ b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/post-migration.py @@ -0,0 +1,47 @@ +import logging + +from openupgradelib import openupgrade + +from odoo import api +from odoo.exceptions import ValidationError + +from odoo.addons.hr_holidays.models.hr_leave import HolidaysRequest + +_logger = logging.getLogger(__name__) + + +@api.constrains("holiday_allocation_id") +def _check_allocation_id(self): + """Don't raise ValidationError in _check_allocation_id method.""" + try: + return HolidaysRequest._check_allocation_id._original_method(self) + except ValidationError: + _logger.warning( + "Could not find an allocation of type %s for the time off with ID %s." + "\nRequires allocation of this type is now set to 'No Limit'." + "\nPlease review requires allocation of this type manually " + "after the migration." + % (self.holiday_status_id.mapped("display_name"), self.ids) + ) + self.holiday_status_id.write({"requires_allocation": "no"}) + + +_check_allocation_id._original_method = HolidaysRequest._check_allocation_id +HolidaysRequest._check_allocation_id = _check_allocation_id + + +def _fill_hr_leave_holiday_allocation_id(env): + leaves = env["hr.leave"].search( + [ + ("holiday_status_id.requires_allocation", "=", "yes"), + ("date_from", "!=", False), + ("date_to", "!=", False), + ] + ) + leaves._compute_from_holiday_status_id() + + +@openupgrade.migrate() +def migrate(env, version): + _fill_hr_leave_holiday_allocation_id(env) + openupgrade.load_data(env.cr, "hr_holidays", "15.0.1.5/noupdate_changes.xml") diff --git a/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/pre-migration.py b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/pre-migration.py new file mode 100644 index 000000000000..a916840078df --- /dev/null +++ b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/pre-migration.py @@ -0,0 +1,232 @@ +from openupgradelib import openupgrade + + +def _fast_fill_hr_leave_employee_company_id(env): + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE hr_leave + ADD COLUMN IF NOT EXISTS employee_company_id integer""", + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_leave hl + SET employee_company_id = empl.company_id + FROM hr_employee empl + WHERE hl.employee_id IS NOT NULL AND hl.employee_id = empl.id""", + ) + + +def _map_hr_leave_state(env): + openupgrade.map_values( + env.cr, + "state", + "state", + [("cancel", "refuse")], + table="hr_leave", + ) + + +def _map_hr_leave_allocation_approver_id(env): + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE hr_leave_allocation + ADD COLUMN IF NOT EXISTS approver_id integer""", + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_leave_allocation + SET approver_id = CASE + WHEN second_approver_id IS NOT NULL THEN second_approver_id + ELSE first_approver_id END + WHERE state in ('refuse', 'validate')""", + ) + + +def _map_hr_leave_allocation_state(env): + openupgrade.logged_query( + env.cr, + """UPDATE hr_leave_allocation + SET state = 'confirm' + WHERE state = 'validate1'""", + ) + + +def _convert_datetime_to_date_hr_leave_allocation_date_from(env): + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_leave_allocation + SET date_from = CASE + WHEN date_from IS NOT NULL THEN date_from::date + ELSE create_date::date END""", + ) + + +def _convert_datetime_to_date_hr_leave_allocation_date_to(env): + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_leave_allocation + SET date_to = date_to::date + WHERE date_to IS NOT NULL""", + ) + + +def _fast_fill_hr_leave_allocation_employee_company_id(env): + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE hr_leave_allocation + ADD COLUMN IF NOT EXISTS employee_company_id integer""", + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_leave hl + SET employee_company_id = empl.company_id + FROM hr_employee empl + WHERE hl.employee_id IS NOT NULL AND hl.employee_id = empl.id""", + ) + + +def _map_hr_leave_type_allocation_validation_type(env): + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_leave_type + SET allocation_validation_type = + CASE WHEN allocation_validation_type = 'hr' THEN 'set' + ELSE 'officer' END + WHERE allocation_validation_type IN ('hr', 'both', 'manager') + """, + ) + + +def _fast_fill_hr_leave_type_employee_requests(env): + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE hr_leave_type + ADD COLUMN IF NOT EXISTS employee_requests varchar""", + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_leave_type + SET employee_requests = + CASE WHEN allocation_type = 'no' THEN 'no' ELSE 'yes' END""", + ) + + +def _fast_fill_hr_leave_employee_ids(env): + # Manually create tables for avoiding the automatic launch of the compute or default + # FK constraints and indexes will be added by ORM + openupgrade.logged_query( + env.cr, + """ + CREATE TABLE IF NOT EXISTS hr_employee_hr_leave_rel + (hr_leave_id integer, hr_employee_id integer) + """, + ) + openupgrade.logged_query( + env.cr, + """ + INSERT INTO hr_employee_hr_leave_rel (hr_leave_id, hr_employee_id) + SELECT hl.id, hl.employee_id + FROM hr_leave hl + WHERE hl.holiday_type = 'employee' + """, + ) + + +def _fast_fill_hr_leave_multi_employee(env): + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE hr_leave + ADD COLUMN IF NOT EXISTS multi_employee boolean""", + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_leave + SET multi_employee = + (SELECT COUNT(rel.hr_employee_id) > 1 + FROM hr_employee_hr_leave_rel rel + WHERE hr_leave.id = rel.hr_leave_id + AND hr_leave.employee_id = rel.hr_employee_id + )""", + ) + + +def _fast_fill_hr_leave_allocation_employee_ids(env): + # Manually create tables for avoiding the automatic launch of the compute or default + # FK constraints and indexes will be added by ORM + openupgrade.logged_query( + env.cr, + """ + CREATE TABLE IF NOT EXISTS hr_employee_hr_leave_allocation_rel + (hr_leave_allocation_id integer, hr_employee_id integer)""", + ) + openupgrade.logged_query( + env.cr, + """ + INSERT INTO hr_employee_hr_leave_allocation_rel ( + hr_leave_allocation_id, hr_employee_id) + SELECT hla.id, hla.employee_id + FROM hr_leave_allocation hla + WHERE hla.holiday_type = 'employee' + """, + ) + + +def _fast_fill_hr_leave_allocation_multi_employee(env): + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE hr_leave_allocation + ADD COLUMN IF NOT EXISTS multi_employee boolean""", + ) + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_leave_allocation + SET multi_employee = + (SELECT COUNT(rel.hr_employee_id) > 1 + FROM hr_employee_hr_leave_allocation_rel rel + WHERE hr_leave_allocation.id = rel.hr_leave_allocation_id + AND hr_leave_allocation.employee_id = rel.hr_employee_id + )""", + ) + + +def _create_column_hr_leave_holiday_allocation_id(env): + # Manually create column for avoiding the automatic launch of the compute or default + openupgrade.logged_query( + env.cr, + """ + ALTER TABLE hr_leave + ADD COLUMN IF NOT EXISTS holiday_allocation_id integer""", + ) + + +@openupgrade.migrate() +def migrate(env, version): + _fast_fill_hr_leave_employee_company_id(env) + _map_hr_leave_state(env) + _map_hr_leave_allocation_approver_id(env) + _map_hr_leave_allocation_state(env) + _convert_datetime_to_date_hr_leave_allocation_date_from(env) + _convert_datetime_to_date_hr_leave_allocation_date_to(env) + _fast_fill_hr_leave_allocation_employee_company_id(env) + _map_hr_leave_type_allocation_validation_type(env) + _fast_fill_hr_leave_type_employee_requests(env) + _fast_fill_hr_leave_employee_ids(env) + _fast_fill_hr_leave_multi_employee(env) + _fast_fill_hr_leave_allocation_employee_ids(env) + _fast_fill_hr_leave_allocation_multi_employee(env) + _create_column_hr_leave_holiday_allocation_id(env) diff --git a/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/upgrade_analysis_work.txt new file mode 100644 index 000000000000..8f9f50663dcf --- /dev/null +++ b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/upgrade_analysis_work.txt @@ -0,0 +1,192 @@ +---Models in module 'hr_holidays'--- +new model hr.leave.accrual.level +new model hr.leave.accrual.plan +new model hr.leave.employee.type.report [sql_view] +# NOTHING TO DO: new model + +---Fields in module 'hr_holidays'--- +hr_holidays / hr.department / total_employee (integer) : module is now 'hr' ('hr_holidays') +hr_holidays / hr.leave / attachment_ids (one2many) : NEW relation: ir.attachment +# NOTHING TO DO: non-stored field + +hr_holidays / hr.leave / employee_company_id (many2one): NEW relation: res.company, isrelated: related, stored +# DONE: pre-migration: fast fill employee_company_id's value from employee_id.company_id on hr.leave model + +hr_holidays / hr.leave / employee_ids (many2many) : NEW relation: hr.employee, isfunction: function, stored +# DONE: pre-migration: create table hr_employee_hr_leave_rel and fill data + +hr_holidays / hr.leave / holiday_allocation_id (many2one): NEW relation: hr.leave.allocation, isfunction: function, stored +# DONE: pre-migration: create column; post-migration: fill data + +hr_holidays / hr.leave / multi_employee (boolean) : NEW isfunction: function, stored +# DONE: pre-migration: create column and fill data + +hr_holidays / hr.leave / payslip_status (boolean) : DEL +# NOTHING TO DO + +hr_holidays / hr.leave / state (selection) : selection_keys is now '['confirm', 'draft', 'refuse', 'validate', 'validate1']' ('['cancel', 'confirm', 'draft', 'refuse', 'validate', 'validate1']') +# DONE: pre-migration: map cancel -> refuse + +hr_holidays / hr.leave.accrual.level / accrual_plan_id (many2one) : NEW relation: hr.leave.accrual.plan, required +hr_holidays / hr.leave.accrual.level / action_with_unused_accruals (selection): NEW required, selection_keys: ['lost', 'postponed'], req_default: function, hasdefault +hr_holidays / hr.leave.accrual.level / added_value (float) : NEW required +hr_holidays / hr.leave.accrual.level / added_value_type (selection) : NEW required, selection_keys: ['days', 'hours'], req_default: function, hasdefault +hr_holidays / hr.leave.accrual.level / first_day (integer) : NEW hasdefault +hr_holidays / hr.leave.accrual.level / first_month (selection) : NEW selection_keys: ['apr', 'feb', 'jan', 'jun', 'mar', 'may'], hasdefault +hr_holidays / hr.leave.accrual.level / first_month_day (integer) : NEW hasdefault +hr_holidays / hr.leave.accrual.level / frequency (selection) : NEW required, selection_keys: ['bimonthly', 'biyearly', 'daily', 'monthly', 'weekly', 'yearly'], req_default: function, hasdefault +hr_holidays / hr.leave.accrual.level / is_based_on_worked_time (boolean): NEW +hr_holidays / hr.leave.accrual.level / maximum_leave (float) : NEW hasdefault +hr_holidays / hr.leave.accrual.level / parent_id (many2one) : NEW relation: hr.leave.accrual.level +hr_holidays / hr.leave.accrual.level / second_day (integer) : NEW hasdefault +hr_holidays / hr.leave.accrual.level / second_month (selection) : NEW selection_keys: ['aug', 'dec', 'jul', 'nov', 'oct', 'sep'], hasdefault +hr_holidays / hr.leave.accrual.level / second_month_day (integer) : NEW hasdefault +hr_holidays / hr.leave.accrual.level / sequence (integer) : NEW isfunction: function, stored +hr_holidays / hr.leave.accrual.level / start_count (integer) : NEW hasdefault +hr_holidays / hr.leave.accrual.level / start_type (selection) : NEW required, selection_keys: ['day', 'month', 'year'], req_default: function, hasdefault +hr_holidays / hr.leave.accrual.level / week_day (selection) : NEW required, selection_keys: ['fri', 'mon', 'sat', 'sun', 'thu', 'tue', 'wed'], req_default: function, hasdefault +hr_holidays / hr.leave.accrual.level / yearly_day (integer) : NEW hasdefault +hr_holidays / hr.leave.accrual.level / yearly_month (selection) : NEW selection_keys: ['apr', 'aug', 'dec', 'feb', 'jan', 'jul', 'jun', 'mar', 'may', 'nov', 'oct', 'sep'], hasdefault +# NOTHING TO DO: new model + +hr_holidays / hr.leave.accrual.plan / allocation_ids (one2many) : NEW relation: hr.leave.allocation +hr_holidays / hr.leave.accrual.plan / level_ids (one2many) : NEW relation: hr.leave.accrual.level +hr_holidays / hr.leave.accrual.plan / name (char) : NEW required +hr_holidays / hr.leave.accrual.plan / time_off_type_id (many2one) : NEW relation: hr.leave.type +hr_holidays / hr.leave.accrual.plan / transition_mode (selection) : NEW required, selection_keys: ['end_of_accrual', 'immediately'], req_default: function, hasdefault +# NOTHING TO DO: new model + +hr_holidays / hr.leave.allocation / accrual_limit (integer) : DEL +hr_holidays / hr.leave.allocation / active (boolean) : NEW hasdefault +# NOTHING TO DO + +hr_holidays / hr.leave.allocation / accrual_plan_id (many2one) : NEW relation: hr.leave.accrual.plan, isfunction: function, stored +# NOTHING TO DO: handled by ORM + +hr_holidays / hr.leave.allocation / first_approver_id (many2one) : DEL relation: hr.employee +hr_holidays / hr.leave.allocation / second_approver_id (many2one) : DEL relation: hr.employee +hr_holidays / hr.leave.allocation / approver_id (many2one) : NEW relation: hr.employee +# DONE: pre-migration: map first_approver_id / second_approver_id to approver_id + +hr_holidays / hr.leave.allocation / date_from (datetime) : now required, req_default: function +hr_holidays / hr.leave.allocation / date_from (datetime) : type is now 'date' ('datetime') +hr_holidays / hr.leave.allocation / date_to (datetime) : not a function anymore +hr_holidays / hr.leave.allocation / date_to (datetime) : type is now 'date' ('datetime') +# DONE: pre-migration: convert datetime to date + +hr_holidays / hr.leave.allocation / employee_company_id (many2one): NEW relation: res.company, isrelated: related, stored +# DONE: pre-migration: fast fill employee_company_id's value from employee_id.company_id on hr.leave.allocation model + +hr_holidays / hr.leave.allocation / employee_ids (many2many) : NEW relation: hr.employee, isfunction: function, stored +# DONE: pre-migration: create hr_employee_hr_leave_allocation_rel table and fill data + +hr_holidays / hr.leave.allocation / interval_number (integer) : DEL +hr_holidays / hr.leave.allocation / interval_unit (selection) : DEL selection_keys: ['days', 'months', 'weeks', 'years'] +hr_holidays / hr.leave.allocation / lastcall (date) : NEW hasdefault +hr_holidays / hr.leave.allocation / number_per_interval (float) : DEL +# NOTHING TO DO: handled by ORM + +hr_holidays / hr.leave.allocation / multi_employee (boolean) : NEW isfunction: function, stored +# DONE: pre-migration: create column and fill data + +hr_holidays / hr.leave.allocation / state (selection) : selection_keys is now '['cancel', 'confirm', 'draft', 'refuse', 'validate']' ('['cancel', 'confirm', 'draft', 'refuse', 'validate', 'validate1']') +# DONE: map validate1 -> confirm + +hr_holidays / hr.leave.allocation / taken_leave_ids (one2many) : NEW relation: hr.leave +hr_holidays / hr.leave.allocation / unit_per_interval (selection) : DEL selection_keys: ['days', 'hours'] +# NOTHING TO DO + +hr_holidays / hr.leave.type / _order : _order is now 'sequence' ('id') +hr_holidays / hr.leave.type / accruals_ids (one2many) : NEW relation: hr.leave.accrual.plan +hr_holidays / hr.leave.type / allocation_type (selection) : DEL selection_keys: ['fixed', 'fixed_allocation', 'no'] +# NOTHING TO DO + +hr_holidays / hr.leave.type / allocation_validation_type (selection): selection_keys is now '['no', 'officer', 'set']' ('['both', 'hr', 'manager']') +# DONE: pre-migration: map hr -> officer, both / manager -> set + +hr_holidays / hr.leave.type / employee_requests (selection) : NEW required, selection_keys: ['no', 'yes'], req_default: function, hasdefault +# DONE: pre-migration: set employee_requests as 'no' if allocation_type = 'no' else 'yes' + +hr_holidays / hr.leave.type / requires_allocation (selection): NEW required, selection_keys: ['no', 'yes'], req_default: function, hasdefault +# DONE: default='yes', but set to 'no' if raise exception in _check_allocation_id + +hr_holidays / hr.leave.type / code (char) : DEL +hr_holidays / hr.leave.type / color (integer) : NEW +hr_holidays / hr.leave.type / icon_id (many2one) : NEW relation: ir.attachment +hr_holidays / hr.leave.type / support_document (boolean) : NEW +hr_holidays / hr.leave.type / validity_start (date) : DEL +hr_holidays / hr.leave.type / validity_stop (date) : DEL +# NOTHING TO DO + +---XML records in module 'hr_holidays'--- +NEW ir.actions.act_window: hr_holidays.hr_leave_action_holiday_allocation_id +NEW ir.actions.act_window: hr_holidays.open_view_accrual_plans +NEW ir.actions.act_window: hr_holidays.open_view_public_holiday +NEW ir.actions.act_window: hr_holidays.resource_calendar_global_leaves_action_from_calendar +NEW ir.actions.server: hr_holidays.action_hr_holidays_by_employee_and_type_report +DEL ir.actions.server: hr_holidays.action_report_to_payslip +NEW ir.attachment: hr_holidays.icon_1 +NEW ir.attachment: hr_holidays.icon_10 +NEW ir.attachment: hr_holidays.icon_11 +NEW ir.attachment: hr_holidays.icon_12 +NEW ir.attachment: hr_holidays.icon_13 +NEW ir.attachment: hr_holidays.icon_14 +NEW ir.attachment: hr_holidays.icon_15 +NEW ir.attachment: hr_holidays.icon_16 +NEW ir.attachment: hr_holidays.icon_17 +NEW ir.attachment: hr_holidays.icon_18 +NEW ir.attachment: hr_holidays.icon_19 +NEW ir.attachment: hr_holidays.icon_2 +NEW ir.attachment: hr_holidays.icon_20 +NEW ir.attachment: hr_holidays.icon_21 +NEW ir.attachment: hr_holidays.icon_22 +NEW ir.attachment: hr_holidays.icon_23 +NEW ir.attachment: hr_holidays.icon_24 +NEW ir.attachment: hr_holidays.icon_25 +NEW ir.attachment: hr_holidays.icon_26 +NEW ir.attachment: hr_holidays.icon_27 +NEW ir.attachment: hr_holidays.icon_28 +NEW ir.attachment: hr_holidays.icon_29 +NEW ir.attachment: hr_holidays.icon_3 +NEW ir.attachment: hr_holidays.icon_30 +NEW ir.attachment: hr_holidays.icon_31 +NEW ir.attachment: hr_holidays.icon_4 +NEW ir.attachment: hr_holidays.icon_5 +NEW ir.attachment: hr_holidays.icon_6 +NEW ir.attachment: hr_holidays.icon_7 +NEW ir.attachment: hr_holidays.icon_8 +NEW ir.attachment: hr_holidays.icon_9 +NEW ir.model.access: hr_holidays.access_hr_leave_accrual_level_manager +NEW ir.model.access: hr_holidays.access_hr_leave_accrual_level_user +NEW ir.model.access: hr_holidays.access_hr_leave_accrual_plan_manager +NEW ir.model.access: hr_holidays.access_hr_leave_accrual_plan_user +NEW ir.model.access: hr_holidays.access_hr_leave_employee_type_report +NEW ir.model.constraint: hr_holidays.constraint_hr_leave_accrual_level_added_value_greater_than_zero +NEW ir.model.constraint: hr_holidays.constraint_hr_leave_accrual_level_check_dates +NEW ir.model.constraint: hr_holidays.constraint_hr_leave_accrual_level_start_count_check +ir.model.constraint: hr_holidays.constraint_hr_leave_allocation_duration_check (changed definition: is now 'check(( number_of_days > 0 and allocation_type='regular') or(allocation_type != 'regular'))' ('check( number_of_days >= 0 )')) +ir.model.constraint: hr_holidays.constraint_hr_leave_allocation_type_value (changed definition: is now 'check((holiday_type='employee' and(employee_id is not null or multi_employee is true)) or(holiday_type='category' and category_id is not null) or(holiday_type='department' and department_id is not null) or(holiday_type='company' and mode_company_id is not null))' ('check((holiday_type='employee' and employee_id is not null) or(holiday_type='category' and category_id is not null) or(holiday_type='department' and department_id is not null) or(holiday_type='company' and mode_company_id is not null))')) +ir.model.constraint: hr_holidays.constraint_hr_leave_type_value (changed definition: is now 'check((holiday_type='employee' and(employee_id is not null or multi_employee is true)) or(holiday_type='company' and mode_company_id is not null) or(holiday_type='category' and category_id is not null) or(holiday_type='department' and department_id is not null) )' ('check((holiday_type='employee' and employee_id is not null) or(holiday_type='company' and mode_company_id is not null) or(holiday_type='category' and category_id is not null) or(holiday_type='department' and department_id is not null) )')) +DEL ir.model.constraint: hr_holidays.constraint_hr_leave_allocation_interval_number_check +DEL ir.model.constraint: hr_holidays.constraint_hr_leave_allocation_number_per_interval_check +NEW ir.rule: hr_holidays.hr_leave_report_rule_group_holiday_user (noupdate) +NEW ir.rule: hr_holidays.hr_leave_report_rule_group_user (noupdate) +NEW ir.ui.menu: hr_holidays.hr_holidays_accrual_menu_configuration +NEW ir.ui.menu: hr_holidays.hr_holidays_public_time_off_menu_configuration +NEW ir.ui.menu: hr_holidays.menu_hr_holidays_report_employee_time_off +NEW ir.ui.view: hr_holidays.hr_accrual_level_view_form +NEW ir.ui.view: hr_holidays.hr_accrual_plan_view_form +NEW ir.ui.view: hr_holidays.hr_accrual_plan_view_tree +NEW ir.ui.view: hr_holidays.hr_leave_allocation_view_form_manager_dashboard +NEW ir.ui.view: hr_holidays.hr_leave_employee_type_report +NEW ir.ui.view: hr_holidays.hr_leave_employee_view_dashboard +NEW ir.ui.view: hr_holidays.hr_leave_report_calendar_view_search +NEW ir.ui.view: hr_holidays.hr_leave_report_pivot +NEW ir.ui.view: hr_holidays.resource_calendar_form_inherit +NEW ir.ui.view: hr_holidays.resource_calendar_leaves_tree_inherit +NEW ir.ui.view: hr_holidays.resource_calendar_leaves_view_search_inherit +NEW ir.ui.view: hr_holidays.view_search_hr_holidays_employee_type_report +DEL ir.ui.view: hr_holidays.assets_backend +DEL ir.ui.view: hr_holidays.hr_leave_report_kanban +DEL ir.ui.view: hr_holidays.qunit_suite +# NOTHING TO DO From a172883ddd9eb85e61ec7920e83fd01847fd073d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Ra=C3=AFch?= Date: Thu, 25 Aug 2022 10:53:45 +0200 Subject: [PATCH 2/2] [FIX] hr_holidays: finish migration --- .../hr_holidays/15.0.1.5/post-migration.py | 60 +++---- .../hr_holidays/15.0.1.5/pre-migration.py | 161 ++++++++---------- .../15.0.1.5/upgrade_analysis_work.txt | 34 ++-- 3 files changed, 115 insertions(+), 140 deletions(-) diff --git a/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/post-migration.py b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/post-migration.py index f88713619628..b65bfcef4bac 100644 --- a/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/post-migration.py +++ b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/post-migration.py @@ -1,47 +1,39 @@ -import logging - from openupgradelib import openupgrade -from odoo import api -from odoo.exceptions import ValidationError - -from odoo.addons.hr_holidays.models.hr_leave import HolidaysRequest - -_logger = logging.getLogger(__name__) - -@api.constrains("holiday_allocation_id") -def _check_allocation_id(self): - """Don't raise ValidationError in _check_allocation_id method.""" - try: - return HolidaysRequest._check_allocation_id._original_method(self) - except ValidationError: - _logger.warning( - "Could not find an allocation of type %s for the time off with ID %s." - "\nRequires allocation of this type is now set to 'No Limit'." - "\nPlease review requires allocation of this type manually " - "after the migration." - % (self.holiday_status_id.mapped("display_name"), self.ids) - ) - self.holiday_status_id.write({"requires_allocation": "no"}) +def fill_hr_leave_type_requires_allocation(env): + openupgrade.logged_query( + env.cr, + """ + UPDATE hr_leave_type + SET requires_allocation = 'no' + WHERE allocation_type = 'no'""", + ) -_check_allocation_id._original_method = HolidaysRequest._check_allocation_id -HolidaysRequest._check_allocation_id = _check_allocation_id +def _map_hr_leave_state(env): + openupgrade.map_values( + env.cr, + openupgrade.get_legacy_name("state"), + "state", + [("cancel", "refuse")], + table="hr_leave", + ) -def _fill_hr_leave_holiday_allocation_id(env): - leaves = env["hr.leave"].search( - [ - ("holiday_status_id.requires_allocation", "=", "yes"), - ("date_from", "!=", False), - ("date_to", "!=", False), - ] +def _map_hr_leave_allocation_state(env): + env["hr.leave.allocation"].search([("state", "=", "validate1")]).activity_update() + openupgrade.map_values( + env.cr, + openupgrade.get_legacy_name("state"), + "state", + [("validate1", "confirm")], + table="hr_leave_allocation", ) - leaves._compute_from_holiday_status_id() @openupgrade.migrate() def migrate(env, version): - _fill_hr_leave_holiday_allocation_id(env) + _map_hr_leave_state(env) + _map_hr_leave_allocation_state(env) openupgrade.load_data(env.cr, "hr_holidays", "15.0.1.5/noupdate_changes.xml") diff --git a/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/pre-migration.py b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/pre-migration.py index a916840078df..0f6e550b71f6 100644 --- a/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/pre-migration.py +++ b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/pre-migration.py @@ -1,31 +1,16 @@ from openupgradelib import openupgrade - -def _fast_fill_hr_leave_employee_company_id(env): - openupgrade.logged_query( - env.cr, - """ - ALTER TABLE hr_leave - ADD COLUMN IF NOT EXISTS employee_company_id integer""", - ) - openupgrade.logged_query( - env.cr, - """ - UPDATE hr_leave hl - SET employee_company_id = empl.company_id - FROM hr_employee empl - WHERE hl.employee_id IS NOT NULL AND hl.employee_id = empl.id""", - ) - - -def _map_hr_leave_state(env): - openupgrade.map_values( - env.cr, - "state", - "state", - [("cancel", "refuse")], - table="hr_leave", - ) +_columns_copy = { + "hr_leave_type": [ + ("allocation_validation_type", None, None), + ], + "hr_leave": [ + ("state", None, None), + ], + "hr_leave_allocation": [ + ("state", None, None), + ], +} def _map_hr_leave_allocation_approver_id(env): @@ -39,70 +24,60 @@ def _map_hr_leave_allocation_approver_id(env): env.cr, """ UPDATE hr_leave_allocation - SET approver_id = CASE - WHEN second_approver_id IS NOT NULL THEN second_approver_id - ELSE first_approver_id END + SET approver_id = COALESCE(second_approver_id, first_approver_id) WHERE state in ('refuse', 'validate')""", ) -def _map_hr_leave_allocation_state(env): +def _convert_datetime_to_date_hr_leave_allocation_date_from(env): + openupgrade.rename_columns(env.cr, {"hr_leave_allocation": [("date_from", None)]}) openupgrade.logged_query( env.cr, - """UPDATE hr_leave_allocation - SET state = 'confirm' - WHERE state = 'validate1'""", + """ALTER TABLE hr_leave_allocation ADD COLUMN date_from date""", ) - - -def _convert_datetime_to_date_hr_leave_allocation_date_from(env): openupgrade.logged_query( env.cr, - """ + f""" UPDATE hr_leave_allocation - SET date_from = CASE - WHEN date_from IS NOT NULL THEN date_from::date - ELSE create_date::date END""", + SET date_from = COALESCE({ + openupgrade.get_legacy_name("date_from")}, create_date)::date""", ) def _convert_datetime_to_date_hr_leave_allocation_date_to(env): + openupgrade.rename_columns(env.cr, {"hr_leave_allocation": [("date_to", None)]}) openupgrade.logged_query( env.cr, - """ + """ALTER TABLE hr_leave_allocation ADD COLUMN date_to date""", + ) + openupgrade.logged_query( + env.cr, + f""" UPDATE hr_leave_allocation - SET date_to = date_to::date - WHERE date_to IS NOT NULL""", + SET date_to = {openupgrade.get_legacy_name("date_to")}::date + WHERE {openupgrade.get_legacy_name("date_to")} IS NOT NULL""", ) -def _fast_fill_hr_leave_allocation_employee_company_id(env): +def _fast_fill_hr_leave_allocation_accrual_plan_id(env): openupgrade.logged_query( env.cr, """ ALTER TABLE hr_leave_allocation - ADD COLUMN IF NOT EXISTS employee_company_id integer""", - ) - openupgrade.logged_query( - env.cr, - """ - UPDATE hr_leave hl - SET employee_company_id = empl.company_id - FROM hr_employee empl - WHERE hl.employee_id IS NOT NULL AND hl.employee_id = empl.id""", + ADD COLUMN IF NOT EXISTS accrual_plan_id integer""", ) -def _map_hr_leave_type_allocation_validation_type(env): +def refill_hr_leave_type_allocation_validation_type(env): openupgrade.logged_query( env.cr, """ UPDATE hr_leave_type - SET allocation_validation_type = - CASE WHEN allocation_validation_type = 'hr' THEN 'set' - ELSE 'officer' END - WHERE allocation_validation_type IN ('hr', 'both', 'manager') - """, + SET allocation_validation_type = CASE + WHEN allocation_type = 'fixed_allocation' THEN 'officer' + WHEN allocation_type = 'fixed' THEN 'set' + ELSE 'no' END + WHERE allocation_validation_type IS NOT NULL""", ) @@ -118,7 +93,7 @@ def _fast_fill_hr_leave_type_employee_requests(env): """ UPDATE hr_leave_type SET employee_requests = - CASE WHEN allocation_type = 'no' THEN 'no' ELSE 'yes' END""", + CASE WHEN allocation_type = 'fixed_allocation' THEN 'yes' ELSE 'no' END""", ) @@ -138,7 +113,7 @@ def _fast_fill_hr_leave_employee_ids(env): INSERT INTO hr_employee_hr_leave_rel (hr_leave_id, hr_employee_id) SELECT hl.id, hl.employee_id FROM hr_leave hl - WHERE hl.holiday_type = 'employee' + WHERE hl.holiday_type = 'employee' AND hl.employee_id IS NOT NULL """, ) @@ -148,18 +123,7 @@ def _fast_fill_hr_leave_multi_employee(env): env.cr, """ ALTER TABLE hr_leave - ADD COLUMN IF NOT EXISTS multi_employee boolean""", - ) - openupgrade.logged_query( - env.cr, - """ - UPDATE hr_leave - SET multi_employee = - (SELECT COUNT(rel.hr_employee_id) > 1 - FROM hr_employee_hr_leave_rel rel - WHERE hr_leave.id = rel.hr_leave_id - AND hr_leave.employee_id = rel.hr_employee_id - )""", + ADD COLUMN IF NOT EXISTS multi_employee bool""", ) @@ -179,7 +143,7 @@ def _fast_fill_hr_leave_allocation_employee_ids(env): hr_leave_allocation_id, hr_employee_id) SELECT hla.id, hla.employee_id FROM hr_leave_allocation hla - WHERE hla.holiday_type = 'employee' + WHERE hla.holiday_type = 'employee' AND hla.employee_id IS NOT NULL """, ) @@ -191,42 +155,53 @@ def _fast_fill_hr_leave_allocation_multi_employee(env): ALTER TABLE hr_leave_allocation ADD COLUMN IF NOT EXISTS multi_employee boolean""", ) + + +def fill_hr_leave_allocation_lastcall(env): openupgrade.logged_query( env.cr, """ - UPDATE hr_leave_allocation - SET multi_employee = - (SELECT COUNT(rel.hr_employee_id) > 1 - FROM hr_employee_hr_leave_allocation_rel rel - WHERE hr_leave_allocation.id = rel.hr_leave_allocation_id - AND hr_leave_allocation.employee_id = rel.hr_employee_id - )""", + ALTER TABLE hr_leave_allocation + ADD COLUMN IF NOT EXISTS lastcall date""", ) - - -def _create_column_hr_leave_holiday_allocation_id(env): - # Manually create column for avoiding the automatic launch of the compute or default openupgrade.logged_query( env.cr, """ - ALTER TABLE hr_leave - ADD COLUMN IF NOT EXISTS holiday_allocation_id integer""", + UPDATE hr_leave_allocation + SET lastcall = write_date::date""", + ) + + +def delete_sql_constraints(env): + openupgrade.delete_sql_constraint_safely( + env, "hr_holidays", "hr_leave_allocation", "duration_check" + ) + openupgrade.delete_sql_constraint_safely( + env, "hr_holidays", "hr_leave_allocation", "type_value" + ) + openupgrade.delete_sql_constraint_safely( + env, "hr_holidays", "hr_leave", "type_value" + ) + openupgrade.delete_sql_constraint_safely( + env, "hr_holidays", "hr_leave_allocation", "interval_number_check" + ) + openupgrade.delete_sql_constraint_safely( + env, "hr_holidays", "hr_leave_allocation", "number_per_interval_check" ) @openupgrade.migrate() def migrate(env, version): - _fast_fill_hr_leave_employee_company_id(env) - _map_hr_leave_state(env) + openupgrade.copy_columns(env.cr, _columns_copy) _map_hr_leave_allocation_approver_id(env) - _map_hr_leave_allocation_state(env) _convert_datetime_to_date_hr_leave_allocation_date_from(env) _convert_datetime_to_date_hr_leave_allocation_date_to(env) - _fast_fill_hr_leave_allocation_employee_company_id(env) - _map_hr_leave_type_allocation_validation_type(env) + _fast_fill_hr_leave_allocation_accrual_plan_id(env) + refill_hr_leave_type_allocation_validation_type(env) _fast_fill_hr_leave_type_employee_requests(env) _fast_fill_hr_leave_employee_ids(env) _fast_fill_hr_leave_multi_employee(env) _fast_fill_hr_leave_allocation_employee_ids(env) _fast_fill_hr_leave_allocation_multi_employee(env) - _create_column_hr_leave_holiday_allocation_id(env) + fill_hr_leave_allocation_lastcall(env) + delete_sql_constraints(env) diff --git a/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/upgrade_analysis_work.txt index 8f9f50663dcf..5d5c70972150 100644 --- a/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/upgrade_analysis_work.txt +++ b/openupgrade_scripts/scripts/hr_holidays/15.0.1.5/upgrade_analysis_work.txt @@ -10,22 +10,22 @@ hr_holidays / hr.leave / attachment_ids (one2many) : NEW re # NOTHING TO DO: non-stored field hr_holidays / hr.leave / employee_company_id (many2one): NEW relation: res.company, isrelated: related, stored -# DONE: pre-migration: fast fill employee_company_id's value from employee_id.company_id on hr.leave model +# NOTHING TO DO: odoo already fills it using queries hr_holidays / hr.leave / employee_ids (many2many) : NEW relation: hr.employee, isfunction: function, stored # DONE: pre-migration: create table hr_employee_hr_leave_rel and fill data hr_holidays / hr.leave / holiday_allocation_id (many2one): NEW relation: hr.leave.allocation, isfunction: function, stored -# DONE: pre-migration: create column; post-migration: fill data +# NOTHING TO DO (due to https://github.com/odoo/odoo/pull/96545) hr_holidays / hr.leave / multi_employee (boolean) : NEW isfunction: function, stored -# DONE: pre-migration: create column and fill data +# DONE: pre-migration: created column hr_holidays / hr.leave / payslip_status (boolean) : DEL # NOTHING TO DO hr_holidays / hr.leave / state (selection) : selection_keys is now '['confirm', 'draft', 'refuse', 'validate', 'validate1']' ('['cancel', 'confirm', 'draft', 'refuse', 'validate', 'validate1']') -# DONE: pre-migration: map cancel -> refuse +# DONE: post-migration: map cancel -> refuse hr_holidays / hr.leave.accrual.level / accrual_plan_id (many2one) : NEW relation: hr.leave.accrual.plan, required hr_holidays / hr.leave.accrual.level / action_with_unused_accruals (selection): NEW required, selection_keys: ['lost', 'postponed'], req_default: function, hasdefault @@ -75,25 +75,27 @@ hr_holidays / hr.leave.allocation / date_to (datetime) : type i # DONE: pre-migration: convert datetime to date hr_holidays / hr.leave.allocation / employee_company_id (many2one): NEW relation: res.company, isrelated: related, stored -# DONE: pre-migration: fast fill employee_company_id's value from employee_id.company_id on hr.leave.allocation model +# NOTHING TO DO: odoo already fills it using queries hr_holidays / hr.leave.allocation / employee_ids (many2many) : NEW relation: hr.employee, isfunction: function, stored # DONE: pre-migration: create hr_employee_hr_leave_allocation_rel table and fill data hr_holidays / hr.leave.allocation / interval_number (integer) : DEL hr_holidays / hr.leave.allocation / interval_unit (selection) : DEL selection_keys: ['days', 'months', 'weeks', 'years'] -hr_holidays / hr.leave.allocation / lastcall (date) : NEW hasdefault hr_holidays / hr.leave.allocation / number_per_interval (float) : DEL -# NOTHING TO DO: handled by ORM +hr_holidays / hr.leave.allocation / unit_per_interval (selection) : DEL selection_keys: ['days', 'hours'] +# ??: they should be used to create accrual plans/levels + +hr_holidays / hr.leave.allocation / lastcall (date) : NEW hasdefault +# DONE: pre-migration: set as write_date::date hr_holidays / hr.leave.allocation / multi_employee (boolean) : NEW isfunction: function, stored -# DONE: pre-migration: create column and fill data +# DONE: pre-migration: created column hr_holidays / hr.leave.allocation / state (selection) : selection_keys is now '['cancel', 'confirm', 'draft', 'refuse', 'validate']' ('['cancel', 'confirm', 'draft', 'refuse', 'validate', 'validate1']') -# DONE: map validate1 -> confirm +# DONE: post-migration: map validate1 -> confirm, and call to activity_update() hr_holidays / hr.leave.allocation / taken_leave_ids (one2many) : NEW relation: hr.leave -hr_holidays / hr.leave.allocation / unit_per_interval (selection) : DEL selection_keys: ['days', 'hours'] # NOTHING TO DO hr_holidays / hr.leave.type / _order : _order is now 'sequence' ('id') @@ -102,13 +104,13 @@ hr_holidays / hr.leave.type / allocation_type (selection) : DEL se # NOTHING TO DO hr_holidays / hr.leave.type / allocation_validation_type (selection): selection_keys is now '['no', 'officer', 'set']' ('['both', 'hr', 'manager']') -# DONE: pre-migration: map hr -> officer, both / manager -> set +# DONE: pre-migration: old values been saved. 'officer' if allocation_type = 'fixed_allocation', 'set' if allocation_type = 'fixed', else 'no' hr_holidays / hr.leave.type / employee_requests (selection) : NEW required, selection_keys: ['no', 'yes'], req_default: function, hasdefault -# DONE: pre-migration: set employee_requests as 'no' if allocation_type = 'no' else 'yes' +# DONE: pre-migration: set employee_requests as 'yes' if allocation_type = 'fixed_allocation' else 'no' hr_holidays / hr.leave.type / requires_allocation (selection): NEW required, selection_keys: ['no', 'yes'], req_default: function, hasdefault -# DONE: default='yes', but set to 'no' if raise exception in _check_allocation_id +# DONE: post-migration: set to 'no' if old allocation_type = 'no' hr_holidays / hr.leave.type / code (char) : DEL hr_holidays / hr.leave.type / color (integer) : NEW @@ -164,11 +166,17 @@ NEW ir.model.access: hr_holidays.access_hr_leave_employee_type_report NEW ir.model.constraint: hr_holidays.constraint_hr_leave_accrual_level_added_value_greater_than_zero NEW ir.model.constraint: hr_holidays.constraint_hr_leave_accrual_level_check_dates NEW ir.model.constraint: hr_holidays.constraint_hr_leave_accrual_level_start_count_check +# NOTHING TO DO + ir.model.constraint: hr_holidays.constraint_hr_leave_allocation_duration_check (changed definition: is now 'check(( number_of_days > 0 and allocation_type='regular') or(allocation_type != 'regular'))' ('check( number_of_days >= 0 )')) ir.model.constraint: hr_holidays.constraint_hr_leave_allocation_type_value (changed definition: is now 'check((holiday_type='employee' and(employee_id is not null or multi_employee is true)) or(holiday_type='category' and category_id is not null) or(holiday_type='department' and department_id is not null) or(holiday_type='company' and mode_company_id is not null))' ('check((holiday_type='employee' and employee_id is not null) or(holiday_type='category' and category_id is not null) or(holiday_type='department' and department_id is not null) or(holiday_type='company' and mode_company_id is not null))')) ir.model.constraint: hr_holidays.constraint_hr_leave_type_value (changed definition: is now 'check((holiday_type='employee' and(employee_id is not null or multi_employee is true)) or(holiday_type='company' and mode_company_id is not null) or(holiday_type='category' and category_id is not null) or(holiday_type='department' and department_id is not null) )' ('check((holiday_type='employee' and employee_id is not null) or(holiday_type='company' and mode_company_id is not null) or(holiday_type='category' and category_id is not null) or(holiday_type='department' and department_id is not null) )')) +# DONE: pre-migration: delete constraints and let ORM add them again + DEL ir.model.constraint: hr_holidays.constraint_hr_leave_allocation_interval_number_check DEL ir.model.constraint: hr_holidays.constraint_hr_leave_allocation_number_per_interval_check +# DONE: pre-migration: safely delete constraints + NEW ir.rule: hr_holidays.hr_leave_report_rule_group_holiday_user (noupdate) NEW ir.rule: hr_holidays.hr_leave_report_rule_group_user (noupdate) NEW ir.ui.menu: hr_holidays.hr_holidays_accrual_menu_configuration