Skip to content

Commit

Permalink
[FIX] hr_holidays: finish migration
Browse files Browse the repository at this point in the history
  • Loading branch information
MiquelRForgeFlow committed Feb 24, 2023
1 parent a148644 commit f9c7449
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 138 deletions.
60 changes: 26 additions & 34 deletions openupgrade_scripts/scripts/hr_holidays/15.0.1.5/post-migration.py
Original file line number Diff line number Diff line change
@@ -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")
164 changes: 73 additions & 91 deletions openupgrade_scripts/scripts/hr_holidays/15.0.1.5/pre-migration.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -39,70 +24,67 @@ 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""",
ADD COLUMN IF NOT EXISTS accrual_plan_id integer""",
)


def refill_hr_leave_type_allocation_validation_type(env):
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""",
UPDATE hr_leave_type
SET leave_validation_type = COALESCE(
leave_validation_type, allocation_validation_type)
WHERE allocation_validation_type IS NOT NULL""",
)


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')
""",
SET allocation_validation_type = CASE
WHEN allocation_type = 'fixed_allocation' THEN 'officer'
WHEN allocation_type = 'fixed' THEN 'set'
ELSE 'no' END""",
)


Expand All @@ -118,7 +100,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""",
)


Expand All @@ -138,7 +120,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
""",
)

Expand All @@ -148,18 +130,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""",
)


Expand All @@ -179,7 +150,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
""",
)

Expand All @@ -191,42 +162,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)
Loading

0 comments on commit f9c7449

Please sign in to comment.