Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions openupgrade_scripts/scripts/hr/19.0.1.1/pre-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ def res_partner_employee(env):
)


def fill_hr_employee_audit_dates(env):
env.cr.execute(
"""
UPDATE hr_employee
SET create_date = COALESCE(create_date, write_date, TIMESTAMP '2000-01-01')
WHERE create_date IS NULL
"""
)
env.cr.execute(
"""
UPDATE hr_employee
SET write_date = COALESCE(write_date, create_date, TIMESTAMP '2000-01-01')
WHERE write_date IS NULL
"""
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_fields(env, renamed_fields)
Expand All @@ -78,4 +95,5 @@ def migrate(env, version):
openupgrade.rename_fields(env, renamed_fields_hr_contract)
openupgrade.rename_tables(env.cr, renamed_tables_hr_contract)
openupgrade.rename_models(env.cr, renamed_models)
fill_hr_employee_audit_dates(env)
res_partner_employee(env)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from odoo.tests import TransactionCase

from odoo.addons.openupgrade_framework import openupgrade_test


@openupgrade_test
class TestHrAuditDatesBackfill(TransactionCase):
def test_no_null_create_date(self):
"""fill_hr_employee_audit_dates() must leave no hr_employee rows
with NULL create_date after pre-migration. 19.0 hr_org_chart calls
employee.write_date.timestamp() unconditionally — NULL crashes it.
"""
self.env.cr.execute(
"SELECT COUNT(*) FROM hr_employee WHERE create_date IS NULL"
)
self.assertEqual(self.env.cr.fetchone()[0], 0)

def test_no_null_write_date(self):
"""Same for write_date — the field actually consumed by hr_org_chart."""
self.env.cr.execute("SELECT COUNT(*) FROM hr_employee WHERE write_date IS NULL")
self.assertEqual(self.env.cr.fetchone()[0], 0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
env = locals().get("env")

# Force at least one hr.employee row to have NULL create_date/write_date so the
# pre-migration backfill in 19.0.1.1 has something to act on; without this,
# the test asserts pass vacuously on a clean seed.
employee = env["hr.employee"].search([], limit=1)
if not employee:
employee = env["hr.employee"].create({"name": "ou18-audit-dates-probe"})

env.cr.execute(
"UPDATE hr_employee SET create_date = NULL, write_date = NULL WHERE id = %s",
(employee.id,),
)
env.cr.commit()
Loading